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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
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.#
Please login first before commenting.