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!