Login

Email on new comments

Author:
nikolaj
Posted:
August 15, 2007
Language:
Python
Version:
.96
Score:
1 (after 1 ratings)

I know ubernostrum has the nice comment_utils, but I need something that would notify the owner of the comment's content object (where the model has a foreignkey field to django.contrib.auth.models.User), but I didn't need all the moderation stuff. I stuck this in my models.py, where YOURMODEL is the name of the model object with comments attached, and a user field.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# to install email notification hook
from django.contrib.contenttypes.models import ContentType
from django.contrib.comments.models import Comment
from django.dispatch import dispatcher

# Email hook 
def post_YOURMODEL_comment_save(sender,instance):
    if instance.content_type == ContentType.objects.get_for_model(YOURMODEL):
        ym=instance.get_content_object()
        from django.core.mail import send_mail
        from django.template import Context, loader
        subject = 'New Comment by %s on %s'%(instance.user.username,ym.title)
        message_template = loader.get_template('email/comment_notification_email.txt')
        message_context = Context({ 'comment':instance,"content_object":ym})
        message = message_template.render(message_context)
        send_mail(subject, message,settings.DEFAULT_FROM_EMAIL,[ym.user.email])
dispatcher.connect(post_YOURMODEL_comment_save, sender=Comment, signal=models.signals.post_save)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 3 weeks ago

Comments

nikolaj (on August 15, 2007):

Note: I have a little User extension object where I put a boolean field on whether or not the user wants to receive notifications that their YOURMODEL has a new comment, and i put that right after the assignment of ym. e.g. (if ym.user.userprofile_set.all()[0].wants_notifications: ).. althgouh probably good to do a bit more verification there :)

#

ubernostrum (on August 15, 2007):

See also this example of how to do it with comment_utils, with potentially a little bit less code.

#

Please login first before commenting.