Fix #309 - Extract extra attributes from signal kwargs

This commit is contained in:
Alvaro Mariano 2023-05-25 20:36:25 -03:00
parent b3d961f719
commit fdf5c1d222
2 changed files with 29 additions and 1 deletions

View file

@ -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)

View file

@ -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"