On Oct 7, 2008, at 11:38 PM, fitzage wrote:

>
> I want to add one additional piece of data to the comment form.
> Basically a checkbox that says whether or not the commenter wants to
> be notified of followup comments.

I just did exactly this, and found that what works best is not just to  
add an input field to the form template, but to actually subclass the  
builtin CommentForm and add the field there. That way the field will  
be a part of the form no matter how and where you use it, and will  
remain in the 'cleaned data' dictionary when the form is validated.  
Subclass the builtin form, add the field, and then do:

from django.contrib import comments

def my_get_form():
     return MyCommentForm

comments.get_form = my_get_form

> I know how to add the checkbox. I just created a comments/form.html
> template and added the checkbox to it. I know that the data is getting
> passed correctly.
>
> Now how do I grab that on the backend without having to modify the
> comment app directly? There is probably a simple, django-standard way
> to do this, but I'm at a loss. I already have a function that emails
> admin users when a comment is posted, but I'm not sure I understand it
> well enough to piggy-back off of that, and I'm not even sure that I
> can.

Signals are beautiful! I made Commenter (email field) and  
CommentNotification (commenter and entry foreign keys) models, and did  
this:

def make_notification(sender, comment, request, **kwargs):
     notify = request.POST.get("notify", None)
     email = request.POST.get("email", None)
     if notify and email:
         commenter, created = Commenter.get_or_create(email=email)
         notification, created =  
CommentNotification.get_or_create(commenter=commenter,  
entry=comment.content_object)

def notify_commenters(sender, comment, request, **kwargs):
     notifications =  
CommentNotification 
.objects.select_related().filter(entry=comment.content_object)
     if notifications:
         for n in notifications:
             msg = render_to_string('comments/email.txt', 
{'comment':comment,'notification':n})
             send_mail("There's a new comment!",msg,"[EMAIL PROTECTED] 
",recipient_list=[n.commenter.email],fail_silently=True)

comment_was_posted.connect(make_notification)
comment_was_posted.connect(notify_commenters)

The email contains a link with a security hash which they can use to  
un-register themselves for that particular blog entry.

I only just wrote all this and haven't tested it yet, but the basic  
principle ought to be sound...

Hope that helps,
Eric

>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to