We're currently developing a system for a certain organization. One of the requirement is to inform users whenever the admin creates an event. All the site users should be notified not through e-mail but something similar to Facebook notification. How do I implement such feature in Django without using Django-notifications, Django-activity streams, etc. We're newbies to Django and we wanted to implement such feature as simple as it can be. I hope you can help us with this matter. Thank you all!
-
One approach is to poll for changes via ajax on the public side of the site. It's simple, and easy to implement. – Brandon Taylor Mar 02 '13 at 18:05
-
Talk about the requirements bit more. Does each player need to be notified just once or until the admin disables the notification? – Odif Yltsaeb Mar 02 '13 at 19:18
-
@Brandon can you teach us how to do that? We're quite new on this. Thanks in advance. :) – Mark Eugene Mar 02 '13 at 22:57
-
@Zayatzz we're planning on notifying the users once. But after the user viewed the notification, he/she can still view it later on. – Mark Eugene Mar 02 '13 at 23:00
2 Answers
So, I can really only give you some general direction, as I don't know the particulars, but basically you could do something like this:
First, I would add an override to the save_model() method of your admin class. This is where you can check to see if your object has a primary key yet. If not, you can set a flag to create the event (not sure if you need to also associate your event to the notification via a foreign key.
If you don't need to have an association between your Event and the notification, you could skip setting a flag and just create the notification object whenever you want.
Second, create a view that returns JSON for your events. I'm not sure how you would need to filter these objects. Is it only for logged in users, etc, but you would only want to return ones that have not been previously viewed.
Third, call the view via ajax on an interval, passing in whatever criteria you need to filter the results and record the notification has been viewed.
Hope that gets you going.

- 33,823
- 15
- 104
- 144
@Zayatzz we're planning on notifying the users once. But after the user viewed the notification, he/she can still view it later on.
In this case you probably need to create notification objects in the admin. Object probably needs to be something like this:
class Notification(models.Model):
message = models.TextField()
type = models.IntegerField()
relevancy_end_date = models.DateField()
notified_users = models.ManyToManyField(django.contrib.auth.User, null = True)
In your admin form you need to override that models.IntegerField to have widget with choices. The choices you can get from :
https://docs.djangoproject.com/en/dev/ref/contrib/messages/#creating-custom-message-levels
They are basically numbers, 10, 20, 30, 40, 50.
In view you can check if there are notifications that user should be notified of:
notes = Notification.objects.filter(notified_users__id = request.user.id).filter(relevancy_end_date__gte = datetime.datetime.now())
And if there are any notifications, create messages of those using djanogs own message framework:
from django.contrib import messages
if notes:
for note in notes:
messages.add_message(request, note.type, 'Hello world.')
note.notified_users.add(request.user)
what type of message it is (info, error, success) is saved in your notifications type column. so yes - you need to add some kind of check there that varies message type based on notification type.
Its not very DRY to write that block into the beginning of each view where you want to display messages though. So you should create your own view decorator for that.
These 2 links should help you do that: https://docs.djangoproject.com/en/dev/topics/http/decorators/ How create decorator in Django for decorating view
Now at this point. If there is message that user needs to be notified of - it is in request handled by messages framework. And to display it in view you need to do this: https://docs.djangoproject.com/en/dev/ref/contrib/messages/#displaying-messages
OR. Like Brandon says- if you want messages to delivered by ajax/json calls also, then you need to "manually" handle them and insert them into json responses. Something like this:
https://docs.djangoproject.com/en/dev/ref/contrib/messages/#expiration-of-messages
from django.utils import simplejson
feedback = messages.get_messages(request)
for message in feedback:
response[message.tags] = response.get(message.tags, [])
message_text = smart_str(message.message, encoding='utf-8', strings_only=True)
response[message.tags].append(message_text)
return simplejson.dumps(response)
After you have Notification object, have your view decorator then you only need to create a view for displaying user the notifications he has been shown. I'm sure you can figure out how to create that query.
You can also tweak this Notification class to have extra boolean field instead or with relevancy_end_date field. To make turning it on or off easyer.
NB! This code is completely untested and will probably give out some errors, but its something to get you going in the direction, that i thought might be best for you judging by your input.
Alan

- 1
- 1

- 5,575
- 12
- 49
- 80