From d5c6695f805c3600e9c08b5699741308a9ff2c5c Mon Sep 17 00:00:00 2001 From: Skia Date: Sun, 15 Oct 2017 11:59:41 +0200 Subject: [PATCH] core: improve notification callback system Signed-off-by: Skia --- core/models.py | 11 ++++++++--- core/views/site.py | 4 +++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/core/models.py b/core/models.py index 6c4997b0..6444cb87 100644 --- a/core/models.py +++ b/core/models.py @@ -1094,13 +1094,18 @@ class Notification(models.Model): return self.get_type_display() % self.param return self.get_type_display() + def callback(self): + # Get the callback defined in settings to update existing + # notifications + mod_name, func_name = settings.SITH_PERMANENT_NOTIFICATIONS[self.type].rsplit('.',1) + mod = importlib.import_module(mod_name) + getattr(mod, func_name)(self) + def save(self, *args, **kwargs): if not self.id and self.type in settings.SITH_PERMANENT_NOTIFICATIONS: old_notif = self.user.notifications.filter(type=self.type).last() if old_notif: - mod_name, func_name = settings.SITH_PERMANENT_NOTIFICATIONS[self.type].rsplit('.',1) - mod = importlib.import_module(mod_name) - getattr(mod, func_name)(old_notif) + old_notif.callback() old_notif.save() return super(Notification, self).save(*args, **kwargs) diff --git a/core/views/site.py b/core/views/site.py index a367a821..a4084e90 100644 --- a/core/views/site.py +++ b/core/views/site.py @@ -62,7 +62,9 @@ def notification(request, notif_id): if notif: if notif.type not in settings.SITH_PERMANENT_NOTIFICATIONS: notif.viewed = True - notif.save() + else: + notif.callback() + notif.save() return redirect(notif.url) return redirect("/")