From 8d82857ff4acd918897db69b718fee40b4e678c0 Mon Sep 17 00:00:00 2001 From: TheOneAboveAllTitan Date: Fri, 17 Apr 2020 03:03:47 +0530 Subject: [PATCH 1/5] [models] Added method to return links to related objects. --- notifications/admin.py | 3 ++- notifications/base/models.py | 36 +++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/notifications/admin.py b/notifications/admin.py index 3d9c172..95fb2b0 100644 --- a/notifications/admin.py +++ b/notifications/admin.py @@ -9,10 +9,11 @@ Notification = load_model('notifications', 'Notification') class NotificationAdmin(AbstractNotificationAdmin): raw_id_fields = ('recipient',) + readonly_fields = ('action_object_url', 'actor_object_url', 'target_object_url') list_display = ('recipient', 'actor', 'level', 'target', 'unread', 'public') list_filter = ('level', 'unread', 'public', 'timestamp',) - + def get_queryset(self, request): qs = super(NotificationAdmin, self).get_queryset(request) return qs.prefetch_related('actor') diff --git a/notifications/base/models.py b/notifications/base/models.py index 43f9ee3..cf1d986 100644 --- a/notifications/base/models.py +++ b/notifications/base/models.py @@ -11,9 +11,10 @@ from django.core.exceptions import ImproperlyConfigured from django.db import models from django.db.models.query import QuerySet from django.utils import timezone +from django.utils.html import format_html + from jsonfield.fields import JSONField from model_utils import Choices - from notifications import settings as notifications_settings from notifications.signals import notify from notifications.utils import id2slug @@ -24,6 +25,12 @@ if StrictVersion(get_version()) >= StrictVersion('1.8.0'): else: from django.contrib.contenttypes.generic import GenericForeignKey # noqa +try: + # Django >= 1.7 + from django.urls import reverse, NoReverseMatch +except ImportError: + # Django <= 1.6 + from django.core.urlresolvers import reverse, NoReverseMatch # pylint: disable=no-name-in-module,import-error EXTRA_DATA = notifications_settings.get_config()['USE_JSONFIELD'] @@ -252,6 +259,33 @@ class AbstractNotification(models.Model): self.unread = True self.save() + def actor_object_url(self): + try: + url = reverse("admin:{0}_{1}_change".format(self.actor_content_type.app_label, + self.actor_content_type.model), + args=(self.actor_object_id,)) + return format_html("{id}", url=url, id=self.actor_object_id) + except NoReverseMatch: + return self.actor_object_id + + def action_object_url(self): + try: + url = reverse("admin:{0}_{1}_change".format(self.action_object_content_type.app_label, + self.action_content_type.model), + args=(self.action_object_id,)) + return format_html("{id}", url=url, id=self.action_object_object_id) + except NoReverseMatch: + return self.action_object_object_id + + def target_object_url(self): + try: + url = reverse("admin:{0}_{1}_change".format(self.target_content_type.app_label, + self.target_content_type.model), + args=(self.target_object_id,)) + return format_html("{id}", url=url, id=self.target_object_id) + except NoReverseMatch: + return self.target_object_id + def notify_handler(verb, **kwargs): """ From 00f0c8540e90d92e37fa8e081b33444c02bdfe15 Mon Sep 17 00:00:00 2001 From: Andrew Hall Date: Fri, 15 Jan 2021 14:41:43 +1100 Subject: [PATCH 2/5] integrate nonce argument into register_notify_callbacks --- notifications/templatetags/notifications_tags.py | 9 +++++++-- notifications/tests/templates/test_live.html | 2 +- notifications/tests/tests.py | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/notifications/templatetags/notifications_tags.py b/notifications/templatetags/notifications_tags.py index 8e9d425..bdbfa89 100644 --- a/notifications/templatetags/notifications_tags.py +++ b/notifications/templatetags/notifications_tags.py @@ -41,7 +41,9 @@ def register_notify_callbacks(badge_class='live_notify_badge', # pylint: disabl refresh_period=15, callbacks='', api_name='list', - fetch=5): + fetch=5, + nonce=None + ): refresh_period = int(refresh_period) * 1000 if api_name == 'list': @@ -68,7 +70,10 @@ def register_notify_callbacks(badge_class='live_notify_badge', # pylint: disabl fetch_count=fetch ) - script = "" diff --git a/notifications/tests/templates/test_live.html b/notifications/tests/templates/test_live.html index c8c92c5..eb66c1c 100644 --- a/notifications/tests/templates/test_live.html +++ b/notifications/tests/templates/test_live.html @@ -2,7 +2,7 @@ -{% register_notify_callbacks callbacks='fill_notification_list,fill_notification_badge' fetch=20 refresh_period=5 %} +{% register_notify_callbacks callbacks='fill_notification_list,fill_notification_badge' fetch=20 refresh_period=5 nonce='{{nonce}}' %} There are this many notifications pending: {% live_notify_badge %} diff --git a/notifications/tests/tests.py b/notifications/tests/tests.py index b9a8796..e6a07ae 100644 --- a/notifications/tests/tests.py +++ b/notifications/tests/tests.py @@ -436,7 +436,7 @@ class NotificationTestPages(TestCase): request = factory.get('/notification/live_updater') request.user = self.to_user - render(request, 'notifications/test_tags.html', {'request': request}) + render(request, 'notifications/test_tags.html', {'request': request, 'nonce': 'nonce-T5esDNXMnDe5lKMQ6ZzTUw=='}) # TODO: Add more tests to check what is being output. From a28c4b55f13a950d9720b51dba3b2aecf2987f2b Mon Sep 17 00:00:00 2001 From: Andrew Hall Date: Fri, 15 Jan 2021 14:59:52 +1100 Subject: [PATCH 3/5] use older format string for backward compatibility --- notifications/templatetags/notifications_tags.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/notifications/templatetags/notifications_tags.py b/notifications/templatetags/notifications_tags.py index bdbfa89..4727ec2 100644 --- a/notifications/templatetags/notifications_tags.py +++ b/notifications/templatetags/notifications_tags.py @@ -71,9 +71,9 @@ def register_notify_callbacks(badge_class='live_notify_badge', # pylint: disabl ) # add a nonce value to the script tag if one is provided - nonce_str = f' nonce="{nonce}"' if nonce is not None else "" + nonce_str = ' nonce="{nonce}"'.format(nonce=nonce) if nonce is not None else "" - script = f'" From 2a7353ee340b5217d32b56f9aed8460063d2f2cd Mon Sep 17 00:00:00 2001 From: Alvaro Leonel Date: Tue, 9 May 2023 19:11:54 -0300 Subject: [PATCH 4/5] Fix nonce attribute string compilation --- notifications/templatetags/notifications_tags.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/notifications/templatetags/notifications_tags.py b/notifications/templatetags/notifications_tags.py index 4727ec2..b589f71 100644 --- a/notifications/templatetags/notifications_tags.py +++ b/notifications/templatetags/notifications_tags.py @@ -71,7 +71,7 @@ def register_notify_callbacks(badge_class='live_notify_badge', # pylint: disabl ) # add a nonce value to the script tag if one is provided - nonce_str = ' nonce="{nonce}"'.format(nonce=nonce) if nonce is not None else "" + nonce_str = ' nonce="{nonce}"'.format(nonce=nonce) if nonce else "" script = '