Reputation: 13
I am kinda stuck while I am working on a Django project (creating and scheduling a meeting). I need your help!
For the project, I want to send notification (likely pop-up one since I am not implementing email when registering) to users that were selected by the meeting creator. They will receive the notification when they first log in. I have looked at messaging frameworks of Django but it seems like they don't have the method that I am looking for. I am also very new to Django so I really need your help! Any suggestions would be appreciated!
Upvotes: 0
Views: 1810
Reputation: 13731
You'll need a model that identifies the notification.
class Notification(models.Model):
created = models.DateTimeField(auto_add_now=True)
updated = models.DateTimeField(auto_now=True)
text = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
read = models.DateTimeField(null=True, blank=True)
You can create however many notifications for a user where ever in your application.
If you need to show them on any page, it'd be best to pull them in a context processor. If it's only on a specific page, I'd add them to the template rendering context for that view.
As for marking them read you have a decision to make. You can elect to mark them read when they are rendered to the user which would happen at the same time as you pull them in either of the above situations. Or you can force the user to dismiss them and then mark it as read. If you do that you'll need an ajax view that takes a notification id or some other identifier, verifies the requesting user matches the notification's user and set read=django.utils.timezone.now()
Edit:
Re-reading the question, here's a more appropriate answer:
class Notification(models.Model):
created = models.DateTimeField(auto_add_now=True)
text = models.TextField()
users = models.ManyToManyField(User, through='NotificationUser')
class NotificationUser(models.Model):
created = models.DateTimeField(auto_add_now=True)
updated = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
notification = models.ForeignKey(User, on_delete=models.CASCADE)
read = models.DateTimeField(null=True, blank=True)
This way you can have a single notification instance with it's properties like text (or maybe creator). And then have the notification-user specific properties like whether that particular user has read/seen the notification. Edit: I re-read your issue and it says to send a notification to multiple users. In that case something like this may be better.
Upvotes: 2