Skip to content
Browse files

Add FLUENT_COMMENTS_EXCLUDE_FIELDS setting to hide fields on request.

Avoids code hacking to hide the title/URL fields, etc..
Requires to set `COMMENTS_APP = "fluent_comments"`.

Closes #3
  • Loading branch information...
1 parent 1fbf15f commit 33f91c82977206f08903d3d53267c75d78c62ad4 @vdboor vdboor committed Jan 6, 2013
Showing with 66 additions and 0 deletions.
  1. +24 −0 fluent_comments/__init__.py
  2. +8 −0 fluent_comments/appsettings.py
  3. +34 −0 fluent_comments/forms.py
View
24 fluent_comments/__init__.py
@@ -0,0 +1,24 @@
+"""
+API for :ref:`custom-comment-app-api`
+"""
+from django.contrib.comments import Comment
+from fluent_comments import appsettings
+from fluent_comments.forms import FluentCommentForm
+
+
+def get_model():
+ """
+ Return the model to use for commenting.
+ """
+ if appsettings.USE_THREADEDCOMMENTS:
+ from threadedcomments.models import ThreadedComment
+ return ThreadedComment
+ else:
+ return Comment
+
+
+def get_form():
+ """
+ Return the form to use for commenting.
+ """
+ return FluentCommentForm
View
8 fluent_comments/appsettings.py
@@ -17,5 +17,13 @@
FLUENT_COMMENTS_MODERATE_AFTER_DAYS = getattr(settings, 'FLUENT_COMMENTS_MODERATE_AFTER_DAYS', None)
FLUENT_COMMENTS_AKISMET_ACTION = getattr(settings, 'FLUENT_COMMENTS_AKISMET_ACTION', 'moderate') # or 'delete'
+FLUENT_COMMENTS_EXCLUDE_FIELDS = getattr(settings, 'FLUENT_COMMENTS_EXCLUDE_FIELDS', ()) or ()
+
if FLUENT_COMMENTS_AKISMET_ACTION not in ('moderate', 'delete'):
raise ImproperlyConfigured("FLUENT_COMMENTS_AKISMET_ACTION can be 'moderate' or 'delete'")
+
+if FLUENT_COMMENTS_EXCLUDE_FIELDS:
+ # The exclude option only works when our form is used.
+ # Allow derived packages to inherit our form class too.
+ if not hasattr(settings, 'COMMENTS_APP') or settings.COMMENTS_APP == 'comments':
+ raise ImproperlyConfigured("To use 'FLUENT_COMMENTS_EXCLUDE_FIELDS', also specify: COMMENTS_APP = 'fluent_comments'")
View
34 fluent_comments/forms.py
@@ -0,0 +1,34 @@
+from django.contrib.comments import CommentForm
+from django.core.exceptions import ImproperlyConfigured
+from fluent_comments import appsettings
+
+
+if appsettings.USE_THREADEDCOMMENTS:
+ from threadedcomments.forms import ThreadedCommentForm as base_class
+else:
+ base_class = CommentForm
+
+
+class FluentCommentForm(base_class):
+ """
+ The comment form, applies various settings.
+ """
+ def __init__(self, *args, **kwargs):
+ super(FluentCommentForm, self).__init__(*args, **kwargs)
+
+ # Remove fields from the form.
+ # This has to be done in the constructor, because the ThreadedCommentForm
+ # inserts the title field in the __init__, instead of the static form definition.
+ for name in appsettings.FLUENT_COMMENTS_EXCLUDE_FIELDS:
+ try:
+ self.fields.pop(name)
+ except KeyError:
+ raise ImproperlyConfigured("Field name '{0}' in FLUENT_COMMENTS_EXCLUDE_FIELDS is invalid, it does not exist in the comment form.")
+
+
+ def get_comment_create_data(self):
+ # Fake form data for excluded fields, so there are no KeyError exceptions
+ for name in appsettings.FLUENT_COMMENTS_EXCLUDE_FIELDS:
+ self.cleaned_data[name] = ""
+
+ return super(FluentCommentForm, self).get_comment_create_data()

0 comments on commit 33f91c8

Please sign in to comment.
Something went wrong with that request. Please try again.