0

I'm writing a Django blog site where blog posts need to be approved by an admin (superuser) before they can be posted. In my BlogPost model, there is a Boolean field "approved".

I've managed to implement all of this. However, I also want the user to be notified (via a django message) when their post is approved. (The user can go and do whatever else they want in the interim, before the admin gets around to approving their post, and so the message must appear on whatever page the user is currently on. Furthermore, if the user has multiple pages open at the same time (say, on different tabs), the message should appear on all of them.) This is what I am having some issues with implementing. I've drafted a small code snippet which encapsulates what I want to do.

@receiver(post_save, sender=BlogPost)
def message_on_approve(sender, instance, **kwargs):
    if instance.user != (the current user who is logged in): return
    if instance.approved:
        messages.success((request, to be sent to the page user is currently on), 'Your post has been approved!')
    else:
        messages.error((the same request), 'Your post has not been approved.')

There are two issues that I am facing now. Firstly, due to the fact that I need to know which user is logged in, the code snippet seems like it should go in my views.py. However this means that I do not have access to which user is currently logged in, unless I make it a local function. The second is that I also do not know what request to put in for the message calls.

Any advice on how I can go about making this work would be much appreciated. Thanks!

1 Answers1

0

At first, you should know that many users may be logged in and using your page. if you want to found the user that causes this change, you should add a field in your model to record the user id in your view function.

If you want to send a notification to a writer user, you should add a new table to your model and record the user's notification, and add start_time and expire_time. for example, if your user sees any page you can show unexpired notifications on this page. Although the better solution is using a socket and communicates with the server on all pages.

mrash
  • 883
  • 7
  • 18