Email on new comments

 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. Unobtrusive comment moderation by ubernostrum 7 years, 1 month ago
  2. Render arbitrary models - template tag by jeffschenck 4 years, 6 months ago
  3. Email on new comments by ubernostrum 6 years, 8 months ago
  4. Auto-documenting Django Models with Sphinx by Voightkampff 2 years, 8 months ago
  5. duplicate model object merging script by nstrite 6 years, 8 months 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.

#

(Forgotten your password?)