Skip to content

Commit

Permalink
Code style and comment cleanups
Browse files Browse the repository at this point in the history
Removed Python 2.4-isms, use new str.format() syntax.
Cleaned up comments and import styles.
  • Loading branch information
vdboor committed Nov 9, 2012
1 parent a844971 commit e7a7830
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
10 changes: 4 additions & 6 deletions fluent_comments/admin.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from django.conf import settings
from django.contrib import admin
from django.contrib.comments import get_model
from django.contrib import comments
from django.contrib.admin.widgets import AdminTextInputWidget
from django.core.exceptions import ImproperlyConfigured
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _
from fluent_comments import appsettings

USE_THREADEDCOMMENTS = 'threadedcomments' in settings.INSTALLED_APPS


# Ensure the admin app is loaded,
# so the model is unregistered here, and not loaded twice.
if USE_THREADEDCOMMENTS:
if appsettings.USE_THREADEDCOMMENTS:
# Avoid getting weird situations where both comment apps are loaded in the admin.
if not hasattr(settings, 'COMMENTS_APP') or settings.COMMENTS_APP == 'comments':
raise ImproperlyConfigured("To use 'threadedcomments', specify the COMMENTS_APP as well")
Expand Down Expand Up @@ -49,7 +47,7 @@ class FluentCommentsAdmin(CommentsAdminBase):
readonly_fields = ('object_link', 'user', 'ip_address', 'submit_date',)

# Adjust the fieldsets for threaded comments
if USE_THREADEDCOMMENTS:
if appsettings.USE_THREADEDCOMMENTS:
fieldsets[0][1]['fields'] = ('object_link', 'user_name', 'user_email', 'user_url', 'title', 'comment', 'submit_date',) # add title field.
fieldsets.insert(2, (_('Hierarchy'), {'fields': ('parent',)}))
raw_id_fields = ('parent',)
Expand All @@ -74,7 +72,7 @@ def formfield_for_dbfield(self, db_field, **kwargs):

# Replace the old admin screen.
if appsettings.FLUENT_COMMENTS_REPLACE_ADMIN:
CommentModel = get_model()
CommentModel = comments.get_model()
try:
admin.site.unregister(CommentModel)
except admin.sites.NotRegistered as e:
Expand Down
2 changes: 2 additions & 0 deletions fluent_comments/appsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
FLUENT_COMMENTS_REPLACE_ADMIN = getattr(settings, "FLUENT_COMMENTS_REPLACE_ADMIN", True)

CRISPY_TEMPLATE_PACK = getattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap')

USE_THREADEDCOMMENTS = 'threadedcomments' in settings.INSTALLED_APPS
34 changes: 12 additions & 22 deletions fluent_comments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ def post_comment_ajax(request, using=None):
return HttpResponseBadRequest("Expecting Ajax call")

# This is copied from django.contrib.comments.
# Basically this view does too much, and doesn't offer a hook to change the rendering
# (request is not passed to next_redirect for example)
# Basically that view does too much, and doesn't offer a hook to change the rendering.
# The request object is not passed to next_redirect for example.
#
# This is a separate view, unlike the approach that django-ajaxcomments takes.
# However, the django-ajaxcomments solution is not thread safe, as it changes the comment view per request.
# This is a separate view to integrate both features. Previously this used django-ajaxcomments
# which is unfortunately not thread-safe (it it changes the comment view per request).


# Fill out some initial data fields from an authenticated user, if present
Expand All @@ -47,20 +47,13 @@ def post_comment_ajax(request, using=None):
model = models.get_model(*ctype.split(".", 1))
target = model._default_manager.using(using).get(pk=object_pk)
except TypeError:
return CommentPostBadRequest(
"Invalid content_type value: %r" % escape(ctype))
return CommentPostBadRequest("Invalid content_type value: {0!r}".format)
except AttributeError:
return CommentPostBadRequest(
"The given content-type %r does not resolve to a valid model." %\
escape(ctype))
return CommentPostBadRequest("The given content-type {0} does not resolve to a valid model.".format)
except ObjectDoesNotExist:
return CommentPostBadRequest(
"No object matching content-type %r and object PK %r exists." %\
(escape(ctype), escape(object_pk)))
return CommentPostBadRequest("No object matching content-type {0} and object PK {1} exists.".format(escape(ctype), escape(object_pk)))
except (ValueError, ValidationError), e:
return CommentPostBadRequest(
"Attempting go get content-type %r and object PK %r exists raised %s" %\
(escape(ctype), escape(object_pk), e.__class__.__name__))
return CommentPostBadRequest("Attempting go get content-type {0!r} and object PK {1!r} exists raised {2}".format(escape(ctype), escape(object_pk), e.__class__.__name__))

# Do we want to preview the comment?
preview = "preview" in data
Expand All @@ -70,9 +63,7 @@ def post_comment_ajax(request, using=None):

# Check security information
if form.security_errors():
return CommentPostBadRequest(
"The comment form failed security verification: %s" %\
escape(str(form.security_errors())))
return CommentPostBadRequest("The comment form failed security verification: {0}".format)

# If there are errors or if we requested a preview show the comment
if preview:
Expand All @@ -96,9 +87,8 @@ def post_comment_ajax(request, using=None):
)

for (receiver, response) in responses:
if response == False:
return CommentPostBadRequest(
"comment_will_be_posted receiver %r killed the comment" % receiver.__name__)
if response is False:
return CommentPostBadRequest("comment_will_be_posted receiver {0} killed the comment".format(receiver.__name__))

# Save the comment and signal that it was saved
comment.save()
Expand All @@ -124,7 +114,7 @@ def _ajax_result(request, form, action, comment=None):
if form.errors:
for field_name in form.errors:
field = form[field_name]
json_errors.update({field_name: _render_errors(field)})
json_errors[field_name] = _render_errors(field)
success = False

comment_html = None
Expand Down

0 comments on commit e7a7830

Please sign in to comment.