From fdf5c1d222b8ac1f0a594b8ce97ff98346c0c37c Mon Sep 17 00:00:00 2001 From: Alvaro Mariano Date: Thu, 25 May 2023 20:36:25 -0300 Subject: [PATCH] Fix #309 - Extract extra attributes from signal kwargs --- notifications/base/models.py | 6 ++++- .../tests/sample_notifications/tests.py | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/notifications/base/models.py b/notifications/base/models.py index 5c2658b..a5cde7a 100644 --- a/notifications/base/models.py +++ b/notifications/base/models.py @@ -357,7 +357,11 @@ def notify_handler(verb, **kwargs): ContentType.objects.get_for_model(obj, for_concrete_model=for_concrete_model)) if kwargs and EXTRA_DATA: - newnotify.data = kwargs.copy() + # set kwargs as model column if available + for key in list(kwargs.keys()): + if hasattr(newnotify, key): + setattr(newnotify, key, kwargs.pop(key)) + newnotify.data = kwargs newnotify.save() new_notifications.append(newnotify) diff --git a/notifications/tests/sample_notifications/tests.py b/notifications/tests/sample_notifications/tests.py index 87529bf..fbc2a2f 100644 --- a/notifications/tests/sample_notifications/tests.py +++ b/notifications/tests/sample_notifications/tests.py @@ -1,7 +1,10 @@ import os from unittest import skipUnless +from django.contrib.auth.models import User import swapper + +from notifications.signals import notify from notifications.tests.tests import AdminTest as BaseAdminTest from notifications.tests.tests import NotificationTest as BaseNotificationTest @@ -20,3 +23,24 @@ class AdminTest(BaseAdminTest): @skipUnless(os.environ.get('SAMPLE_APP', False), 'Running tests on standard django-notifications models') class NotificationTest(BaseNotificationTest): pass + + +class TestExtraDataCustomAccessor(NotificationTest): + def setUp(self): + self.from_user = User.objects.create_user(username="from_extra", password="pwd", email="example@example.com") + self.to_user = User.objects.create_user(username="to_extra", password="pwd", email="example@example.com") + notify.send( + self.from_user, + recipient=self.to_user, + verb='commented', + action_object=self.from_user, + url="/learn/ask-a-pro/q/test-question-9/299/", + other_content="Hello my 'world'", + details="test detail" + ) + + def test_extra_data(self): + notification = Notification.objects.get(details="test detail") + assert notification, "Expected a notification retrieved by custom extra data accessor" + assert notification.details == "test detail", "Custom accessor should return set value" + assert "details" not in notification.data, "Custom accessor should not be in json data"