Comment Notification in Django
January 1, 2008
Django’s signal dispatcher is a solid way to interact with your models without mucking up the code. This is especially useful for the contrib applications, namely django.contrib.comments. All Django models, by default, have several dispatches: class_prepared, pre_init, post_init, pre_save, post_save, pre_delete, post_delete. This allows us to attach functionality at several points in the model instance’s life.
After receiving a number of spam comments on my blog (not a significant number, but annoying nonetheless), I decided I wanted to be notified when a comment was created. Here is a bit of code I created for a simple email, placed in my project’s __init__.py:
from django.contrib.comments.models import Comment, FreeComment
from django.core.mail import send_mail
from django.dispatch import dispatcher
from django.db.models import signals
def comment_notification(sender, instance):
subject = 'New Comment on %s' % instance.get_content_object().title
msg = 'Comment text:\n\n%s' % instance.comment
send_mail(subject, msg, 'noreply@example.com', ['email@example.com'])
dispatcher.connect(comment_notification, sender=FreeComment, signal=signals.post_save)
dispatcher.connect(comment_notification, sender=Comment, signal=signals.post_save)
This type of dispatch system enhances granularity and modularity, thereby allowing developers to modify the default actions of Django’s models without having to modify Django itself. Signals aren’t in the main documentation yet, but if you would like to read more check out this wiki page.
P.S., If you want real comment utilities I recommend Ubernostrum’s django-comment-utils package, which includes “template tags for common operations, a custom manager with useful querying functionality for models which use comments, and a generic comment-moderation system”.
Add your comment
No HTML; Only URLs and line breaks are converted.
Comments
Darn I just did added simple comments to my blog tonight using the vanilla django.contrib.comments stuff. I totally forgot about the comment-utils.
I did however, use James' akismet filtering snippet, and integrated my own emailing bits like you did. I wrote it up here:
http://blog.clintecker.com/2008/jan/1...
Posted by Clint Ecker
Thanks for this.
A while back, I did something similar but rather than using signals simply stuck it in the save method of my comments model. It also twittered rather than send an email:
http://breakfastdinnertea.co.uk/blog/...
in retrospect, there's a chance of DRY abuse and coupling - but it really was just a one-off view for my blog so implementing signals would feel like overkill. I am however, considering the use of signals in updating some sidebar api-links to things such as twitter and last.fm, which may be triggered by a Cron batch script. We'll see though.
Posted by Simon Scarfe
I just installed the contrib.comments on one of my sites and was just trying to think of an easy way to get the email alerts on successful posts. Then I stumbled on this.
This worked like a charm on the first try. Thanks!!
Posted by Nolan
I adjusted the code a little to work with Django 1.0, you can look at it here:
http://aaronfay.ca/content/post/comme...
Regards,
Aaron
Posted by Aaron Fay
Thanks a lot, dude :)
Posted by Diggo
Thanks a lot, dude :)
Posted by Diggo