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)
|
Comments
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 :)
#
See also this example of how to do it with
comment_utils, with potentially a little bit less code.#