Opened 15 years ago

Closed 15 years ago

Last modified 12 years ago

#9958 closed (fixed)

Split contrib.comments CommentForm class to allow easier customization

Reported by: arne Owned by: nobody
Component: contrib.comments Version: dev
Severity: Keywords: comments, customization
Cc: Carl Meyer Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The CommentForm class in contrib/comments/forms.py is very monolithic and contains all spam-checking code and the application-specific code.
Splitting the Form into a BaseCommentForm class which contains only the spam-checking code and a CommentForm class which only contains the application-specific code would allow much easier customization of the comments-app.

Using the hooks from #8630 and defining your own model and form (which for example don't include fields for email and url) would either need a copy of the whole form-class code including the modification (about 100 lines of code) or one must subclass the current CommentForm class and manually delete the fields (email and url in this example) before using the form.

The appended patch splits the current CommentForm class into two classes which would allow easier customization of the comments-app. When reviewing this ticket please also see #8630 for more details.

Attachments (4)

comments.diff (5.8 KB ) - added by arne 15 years ago.
9958.diff (4.6 KB ) - added by Thejaswi Puthraya 15 years ago.
git-patch against the latest checkout
9958_2.diff (8.0 KB ) - added by Thejaswi Puthraya 15 years ago.
a patch that hopefully solves arne's and my problem
9958_2.2.diff (8.0 KB ) - added by Thejaswi Puthraya 15 years ago.
a patch that hopefully solves arne's and my problem

Download all attachments as: .zip

Change History (13)

by arne, 15 years ago

Attachment: comments.diff added

comment:1 by Carl Meyer, 15 years ago

Cc: Carl Meyer added

comment:2 by Thejaswi Puthraya, 15 years ago

Triage Stage: UnreviewedDesign decision needed

I am a +1 on this feature because people might not want to have spam protection which might be redundant if they use an external spam detection facility.

I don't like the way the patch has been written because it gives a feeling to the user that a custom form should be inherited from BaseCommentForm. So i have made another patch.

But I would like to hear what a developer has to say about this feature.

by Thejaswi Puthraya, 15 years ago

Attachment: 9958.diff added

git-patch against the latest checkout

comment:3 by arne, 15 years ago

Using 9958.diff solves a different problem than my original patch comments.diff. With the approach in 9958.diff neither inheriting from BaseCommentForm nor inheriting from CommentForm allows for easy customization of the fields the form uses.

Please consider that not every projects needs a comment form with the fields "name", "email", "url" and "comment". Sometimes "name" and "comment" may be enough and 9958.diff gives no form-class from which I can inherit to cleanly build my own comment form with less fields than the original CommentForm. I feel like overriding __init__() and using del to delete form-fields is not a clean solution.

Using an approach like in comments.diff one can either subclass BaseCommentForm to customize the fields the form has and use all the existing Spam-protection logic or could use a completly other form (inheriting form forms.Form) to circumvent the Spam-protection as thejaswi_puthraya pointed out and the ability to customize the fields.

in reply to:  3 comment:4 by Thejaswi Puthraya, 15 years ago

Replying to arne:

Using 9958.diff solves a different problem than my original patch comments.diff. With the approach in 9958.diff neither inheriting from BaseCommentForm nor inheriting from CommentForm allows for easy customization of the fields the form uses.

Please consider that not every projects needs a comment form with the fields "name", "email", "url" and "comment". Sometimes "name" and "comment" may be enough and 9958.diff gives no form-class from which I can inherit to cleanly build my own comment form with less fields than the original CommentForm. I feel like overriding __init__() and using del to delete form-fields is not a clean solution.

Thank you for providing an insight into your problem.

Using an approach like in comments.diff one can either subclass BaseCommentForm to customize the fields the form has and use all the existing Spam-protection logic or could use a completly other form (inheriting form forms.Form) to circumvent the Spam-protection as thejaswi_puthraya pointed out and the ability to customize the fields.

I have seen a lot of requests in IRC from people who felt that the current honeypot mechanism provided by the comments was insufficient and/or was redundant with their home-grown/third-party solution. Hence I segregated the two forms to remove that redundancy.

Here are my observations regarding your patch:

  • I would prefer your patch provided it does not have the honeypot in the BaseCommentForm because it forces the spam protection on users who don't require one.
  • It still does not remove the pain of say having a form that removes the email field and I am sure no other patch can remove that pain. It is best to draw a line somewhere and let the user take a small pain (one-time effort) to write a custom form.

My personal opinion is that spam protection should be optional. Let me come up with another patch that tackles mixes both your problem and mine.

Let's leave it to the devels and see what is the best for such a scenario.

comment:5 by Thejaswi Puthraya, 15 years ago

Ok here is what is basically done. First there is a BaseCommentForm that holds the most vital fields of the form ie content_type, object_pk, timestamp and security hash. Then a second form called MetaCommentForm (that is a subclass of BaseCommentForm)(please change the name of MetaCommentForm if it sounds confusing) that holds the name, email, url etc fields. Then a third form called CommentForm (a subclass of MetaCommentForm) that holds the honeypot.

by Thejaswi Puthraya, 15 years ago

Attachment: 9958_2.diff added

a patch that hopefully solves arne's and my problem

by Thejaswi Puthraya, 15 years ago

Attachment: 9958_2.2.diff added

a patch that hopefully solves arne's and my problem

comment:6 by Thejaswi Puthraya, 15 years ago

milestone: 1.1 beta

Is a fairly simple and backwards-compatible fix that could make into 1.1beta

comment:7 by Thejaswi Puthraya, 15 years ago

Has patch: set

comment:8 by Jacob, 15 years ago

Resolution: fixed
Status: newclosed

(In [10110]) Fixed #9958: split the CommentForm into a set of smaller forms. This for better encapsulation, but also so that it's easier for subclasses to get at the pieces they might need. Thanks to Thejaswi Puthraya.

comment:9 by Jacob, 12 years ago

milestone: 1.1 beta

Milestone 1.1 beta deleted

Note: See TracTickets for help on using tickets.
Back to Top