Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grouped notifications #249

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 41 additions & 1 deletion notifications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
# pylint: disable=too-many-lines
from distutils.version import StrictVersion # pylint: disable=no-name-in-module,import-error
import copy

from django import get_version
from django.conf import settings
Expand All @@ -10,7 +11,7 @@
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 import timezone, six
from django.utils.six import text_type
from jsonfield.fields import JSONField
from model_utils import Choices
Expand Down Expand Up @@ -270,6 +271,8 @@ def notify_handler(verb, **kwargs):
timestamp = kwargs.pop('timestamp', timezone.now())
level = kwargs.pop('level', Notification.LEVELS.info)

local_kwargs = locals()

# Check if User or Group
if isinstance(recipient, Group):
recipients = recipient.user_set.all()
Expand All @@ -280,7 +283,44 @@ def notify_handler(verb, **kwargs):

new_notifications = []

GROUP_SIMILAR = notifications_settings.get_config()['GROUP_SIMILAR']
GROUP_SIMILAR_FIELDS = notifications_settings.get_config()['GROUP_SIMILAR_FIELDS']

for recipient in recipients:
if GROUP_SIMILAR:
copied_fields = copy.deepcopy(GROUP_SIMILAR_FIELDS)

for k, v in copied_fields.items():
if isinstance(v, six.string_types) and v.startswith('$'):
copied_fields[k] = local_kwargs[v[1:]]

# Set optional objects
for obj, opt in optional_objs:
if obj is not None:
copied_fields['%s_object_id' % opt] = obj.pk
copied_fields['%s_content_type' % opt] = ContentType.objects.get_for_model(obj)

oldnotify = Notification.objects.filter(
recipient=recipient,
actor_content_type=ContentType.objects.get_for_model(actor),
actor_object_id=actor.pk,
verb=text_type(verb)
).filter(**copied_fields)

for n in oldnotify:
if kwargs and EXTRA_DATA:
n.data = kwargs

n.timestamp = timestamp
n.description = description
n.emailed = False
n.save()

new_notifications.append(n)

if len(oldnotify) > 0:
continue

newnotify = Notification(
recipient=recipient,
actor_content_type=ContentType.objects.get_for_model(actor),
Expand Down
8 changes: 8 additions & 0 deletions notifications/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
'USE_JSONFIELD': False,
'SOFT_DELETE': False,
'NUM_TO_FETCH': 10,
'GROUP_SIMILAR': False, # False by default
'GROUP_SIMILAR_FIELDS': { # The fields that will determine uniqueness of the notification
'unread': True,
'public': '$public', # if starts with $, it is a variable
'emailed': False,
'level': '$level',
'deleted': False
}
}


Expand Down
57 changes: 57 additions & 0 deletions notifications/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,60 @@ def test_has_notification(self):
context = {"user":self.to_user}
output = u"True"
self.tag_test(template, context, output)


class NotificationTestGrouping(TestCase):
def setUp(self):
self.from_user = User.objects.create_user(username="from", password="pwd", email="example@example.com")
self.to_user = User.objects.create_user(username="to", password="pwd", email="example@example.com")

@override_settings(DJANGO_NOTIFICATIONS_CONFIG={
'GROUP_SIMILAR': True
})
def test_grouping(self):
# initial notification

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'"
)

notifications = Notification.objects.filter(recipient=self.to_user)
self.assertEqual(len(notifications), 1)

# second time

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'"
)

notifications = Notification.objects.filter(recipient=self.to_user)
self.assertEqual(len(notifications), 1)

# mark as read
notification = notifications.first()
notification.unread = False
notification.save()

# send again notification

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'"
)

notifications = Notification.objects.filter(recipient=self.to_user).count()
self.assertEqual(notifications, 2)