How to use django-notification to inform a user when somebody comments on their post
Asked Answered
A

1

11

I have been developing in django for sometime now, and have developed a neat website having functionality such as writing blogs, posting questions, sharing content etc. However there is still one thing that is missing and i.e. creating notification for users.

What I want to do is to inform users in their profiles, whenever somebody comments on their posts, or if they are following a particular post and there is an update on it, then inform the user of that update. I have looked around many applications but I am still very confused about how to do it.

In case of using django-notification I seem to have an impression(which can be wrong) that I can use this only to inform the user via email, i.e. I cannot show these notifications in the user profile, just like we have on facebook.

Firstly I would like to know if I am wrong, and then I really need some proper tutorial or guidance on how to go about doing it. I know how to register a notification and send it on proper signal but there is no documentation on how to show these notices in a template, if this can be done.

Any guidance/tutorial/getting started doc will be deeply appreciated.

Abduce answered 22/12, 2011 at 11:49 Comment(0)
B
13

Yes django-notifications is only designed for email notifications.

Here is a signal slot that you can add to your models.py and tweak to your own needs:

from django.db import models
from django.contrib.sites.models import Site
from django.db.models import signals
from notification import models as notification

def create_notice_types(app, created_models, verbosity, **kwargs):
    notification.create_notice_type("new_comment", "Comment posted", "A comment has been posted")
signals.post_syncdb.connect(create_notice_types, sender=notification)

def new_comment(sender, instance, created, **kwargs):
    # remove this if-block if you want notifications for comment edit too
    if not created:
        return None

    context = {
        'comment': instance,
        'site': Site.objects.get_current(),
    }
    recipients = []

    # add all users who commented the same object to recipients
    for comment in instance.__class__.objects.for_model(instance.content_object):
        if comment.user not in recipients and comment.user != instance.user:
            recipients.append(comment.user)

    # if the commented object is a user then notify him as well
    if isinstance(instance.content_object, models.get_model('auth', 'User')):
        # if he his the one who posts the comment then don't add him to recipients
        if instance.content_object != instance.user and instance.content_object not in recipients:
            recipients.append(instance.content_object)

    notification.send(recipients, 'new_comment', context)

signals.post_save.connect(new_comment, sender=models.get_model('comments', 'Comment'))

Now for templates, pretty easy.

templates/notification/new_comment/short.txt

{{ comment.user }} commented on {{ comment.object }}

templates/notification/new_comment/notice.html

<a href="{{ comment.user.get_absolute_url }}">{{ comment.user }}</a> commented <a href="{{ comment.content_object.get_absolute_url }}">{{ comment.content_object }}</a>

templates/notification/new_comment/full.txt

{{ comment.user }} commented on {{ comment.content_object }}

Comment:
{{ comment.comment }}

Reply on: 
http://{{ site.domain }}{{ comment.content_object.get_absolute_url }}

Warning: it's a very simplified, untested adaptation of our production code.

Note : Django-1.7 deprecated the post_syncdb signal

Here are some more information:

Bathrobe answered 22/12, 2011 at 12:15 Comment(15)
That is how I can generate signals that can be sent, that part is clear but what about the views. How can I receive this notification in the view where I display the user profile. I want to display all these notifications in the user profileAbduce
This new_comment will this work for inbuilt django-comments framework or I need to have my custom framework. Where is the linking being done to my comments frameworkAbduce
django-notifications not going to make a great user profile activity list, as you can see in the source of the Notification model: github.com/jtauber/django-notification/blob/master/notification/… the message of a notification has to be pre-rendered for one person .... Also: new_comment is meant to work with django.contrib.comments. django-notifications: imho great for email notifications. Nothing more, nothing less.Bathrobe
Then I am seriously waiting for you to update the documentation so that I can use django-subscription as I really need the feature of sending updates to user profileAbduce
I used the code that you have put up above and I get this error Manager isn't accessible via Comment instancesAbduce
probablyl i fixed it by changing instance.objects to instance.__class__.objects Thanks for your feedback !Bathrobe
Sorry to bug you again but now I am getting this error NoticeType matching query does not exist. Could you please help me in knowing what does this mean?Abduce
Did you run syncdb ? it should call this crucial line: notification.create_notice_type("new_comment", "Comment posted", "A comment has been posted")Bathrobe
Yes I ran syncdb but is there any problems If I am using south for my model, because when I do syncdb I do not see any thing about any table being createdAbduce
It's not supposed to create a table. If your NoticeType does not exist, then this didn't happen: notification.create_notice_type("new_comment", "Comment posted", "A comment has been posted").Bathrobe
let us continue this discussion in chatBathrobe
Hello jpic, I did not know how to contact you so I am trying here. I used your updated example project and everything is fine except that when I login with either of the users even then I am unable to access the user page. Somehow the user that is logged in does not change to steve or james even after I log inAbduce
You don't know how to contact me ? there is my IRC contact in the documentation, you opened an issue on github to which i replied, also there is a chat conversation here on stackoverflow and there is a link to the chat discussion right above your message. That said, I'm not going to do the project for you. So if you want to make a facebook clone you'd better get started reading code. I just tested the user detail view from a fresh checkout and it works. My guess is that you need to make some more effort and not just abandon and ask me all the time. You should also try to do things yourself.Bathrobe
Yes.. I posted that because I tried contacting you on all the above things but couldn't get a reply. I am sorry about the user_detail... I eventually figured it out.. Also I totally understand you are not going to make my project but just wanted to say a little better documentation would definitely help. Anyways I will not trouble you anymore, thanks for all the help so far.. Also django-subscription is a very handy app, but a little more explanation about how it can be customized will be good for anybody who wants to use it. ThanksAbduce
Hi jpic, I have tried the way you explained and it is working file for comment section but unable to function with custom comment model. #28091256 I have explained this above link. your comment required please.many thanksMeritocracy

© 2022 - 2024 — McMap. All rights reserved.