-6

I want to create a notification to django admin whenever a category is added in database. The admin should then click the boolean field and publish the category.

  • 1
    Hi. What have you tried so far? What didn't work? Also, you only told what you want. What's your problem? – zubergu Jun 15 '15 at 13:47
  • I have tried using messages in django but it is only valid till a particular session and i do not want that. I simply want a notification panel on django admin page (which is just like the facebook user notification.) – Faizan Shaikh Jun 16 '15 at 06:40

1 Answers1

2

You could override the save() method on the Categry model - here is some sample basic code...

class Category(models.Model):

    def save(self, *args, **kwargs):
            if not self.pk:
                #no pk so it is new
                try:
                    send_mail('Subject here', 'Here is the message.', 'from@example.com',
    ['superuser@mail.com'], fail_silently=True)
                except:
                    # do something a bit more elaborate here, like logging
                    pass

            else:
                #do something if it is an update or...
                pass
            super(Category, self).save(*args, **kwargs)

T go this route, just remember to import the send_mail functionality...

from django.core.mail import send_mail

Also, you could get the super users on the fly from the db - or import from the settings file - I have it hardcoded as an example.

EDIT: See Brandon's comment regarding post_save. That is probably a better solution, albeit slightly more advanced. If you don't want to go that route, please wrap the email logic in a try/except block to avoid something secondary like the email failing from blowing up your save.

picus
  • 1,507
  • 2
  • 13
  • 23
  • 2
    I would recommend doing this in a `post_save` signal instead to the UI isn't blocked. – Brandon Taylor Jun 15 '15 at 13:57
  • 1
    @Brandon moving it to a `post_save` signal doesn't make it asynchronous. To send the email asynchronously, you'd need to use something like celery (or fork the process, which I wouldn't recommend) – Alasdair Jun 15 '15 at 15:17
  • @Alasdair I know it's not async, but the post_save won't block the view processing... or am I completely wrong? – Brandon Taylor Jun 15 '15 at 15:37
  • 2
    It's my understanding that the post_save will fire once the save is complete - that is a benefit, in that it separates extra logic like this from the save() but it will still need to process before a response is generated. However, to your point, Brandon it probably is a better place to put it :) – picus Jun 15 '15 at 15:44
  • I actuallly want a notification panel on the django admin page instead of send an email. I want the admin to be notifited just like the facebook user notification. – Faizan Shaikh Jun 16 '15 at 06:38
  • I know about the email and post_save signal but i do not want to use email for notifications. I want the notifications to appear on the admin page instead sending continuous mails to the admin. I hope you understand :) – Faizan Shaikh Jun 16 '15 at 06:43
  • In that case you will need to override the admin templates... You could create a context processor that looks for new (or unapproved) categories if the logged in user is admin. Then Override the main template to show the new html as you need. Not easy for a beginner (not sure if you are or not), but not extremely hard either. http://stackoverflow.com/questions/6583877/how-to-override-and-extend-basic-django-admin-templates – picus Jun 16 '15 at 19:42