from django.contrib.sites.models import Site from django.template import Context, loader from comment_utils.moderation import CommentModerator class EmailOwner(CommentModerator): """ Subclass of ``CommentModerator`` which emails the "owner" of an object whenever a new comment is posted on it. Assumes that the 'owner' is specified by a ``ForeignKey`` named 'user'; edit that if your field is named something else. """ email_notification = True def email(self, comment, content_object): t = loader.get_template('comment_email_owner.txt') c = Context({ 'comment': comment, 'content_object': content_object }) subject = '[%s] New comment posted on "%s"' % (Site.objects.get_current().name, content_object) message = t.render(c) send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, [content_object.user.email], fail_silently=True)