Skip to content

Commit

Permalink
Add custom column for extra data
Browse files Browse the repository at this point in the history
  • Loading branch information
eelkevdbos committed Mar 31, 2021
1 parent e8d2a25 commit 411901d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions notifications/base/models.py
Expand Up @@ -301,6 +301,10 @@ def notify_handler(verb, **kwargs):
ContentType.objects.get_for_model(obj))

if kwargs and EXTRA_DATA:
# 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()
Expand Down
24 changes: 24 additions & 0 deletions notifications/tests/sample_notifications/tests.py
Expand Up @@ -2,6 +2,9 @@
from unittest import skipUnless

import swapper
from django.contrib.auth.models import User

from notifications.signals import notify
from notifications.tests.tests import AdminTest as BaseAdminTest
from notifications.tests.tests import NotificationTest as BaseNotificationTest

Expand All @@ -20,3 +23,24 @@ def setUpClass(cls):
@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"

0 comments on commit 411901d

Please sign in to comment.