From 0f13db581df242d45f35b7cf639d89f6028e5d29 Mon Sep 17 00:00:00 2001 From: Serghei Mihai Date: Mon, 15 Jan 2018 15:22:25 +0100 Subject: [PATCH] notifications: precise if notification is created in notify api (#21189) --- combo/apps/notifications/api_views.py | 2 +- combo/apps/notifications/models.py | 7 +++++-- tests/test_notification.py | 17 ++++++++++------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/combo/apps/notifications/api_views.py b/combo/apps/notifications/api_views.py index 056c93e..808b15a 100644 --- a/combo/apps/notifications/api_views.py +++ b/combo/apps/notifications/api_views.py @@ -43,7 +43,7 @@ class Add(GenericAPIView): return Response(response, status.HTTP_400_BAD_REQUEST) data = serializer.validated_data - notification_id = Notification.notify( + notification_id, created = Notification.notify( user=request.user, summary=data['summary'], id=data.get('id'), diff --git a/combo/apps/notifications/models.py b/combo/apps/notifications/models.py index 0595dc7..50eec21 100644 --- a/combo/apps/notifications/models.py +++ b/combo/apps/notifications/models.py @@ -68,12 +68,14 @@ class Notification(models.Model): Renew an existing notification, or create a new one, with an external_id: Notification.notify(user, 'summary', id='id') ''' + created = False # get ... notification = Notification.objects.filter_by_id(id).filter(user=user).first() if id else None if not notification: # ... or create notification = Notification(user=user, summary=summary, body=body or '', url=url or '', external_id=id) + created = True notification.summary = summary notification.body = body or '' notification.url = url or '' @@ -89,9 +91,10 @@ class Notification(models.Model): notification.save() if notification.external_id is None: - return '%s' % notification.pk + notification_id = '%s' % notification.pk else: - return notification.external_id + notification_id = notification.external_id + return notification_id, created @classmethod def ack(cls, user, id): diff --git a/tests/test_notification.py b/tests/test_notification.py index 7b7b749..dca3179 100644 --- a/tests/test_notification.py +++ b/tests/test_notification.py @@ -40,7 +40,8 @@ def login(username='admin', password='admin'): def test_notification_api(user, user2): - id_notifoo = Notification.notify(user, 'notifoo') + id_notifoo, created = Notification.notify(user, 'notifoo') + assert created assert Notification.objects.all().count() == 1 noti = Notification.objects.filter_by_id(id_notifoo).filter(user=user).first() assert noti.pk == int(id_notifoo) @@ -64,12 +65,14 @@ def test_notification_api(user, user2): noti = Notification.objects.filter_by_id(id_notifoo).filter(user=user).first() assert noti.end_timestamp - noti.start_timestamp == timedelta(seconds=3600) - Notification.notify(user, 'notibar', id='notibar') + notification, created = Notification.notify(user, 'notibar', id='notibar') + assert created assert Notification.objects.all().count() == 2 - Notification.notify(user, 'notirebar', id='notibar') + notification, created = Notification.notify(user, 'notirebar', id='notibar') + assert not created assert Notification.objects.all().count() == 2 - id2 = Notification.notify(user2, 'notiother') + id2, created = Notification.notify(user2, 'notiother') Notification.forget(user2, id2) noti = Notification.objects.filter_by_id(id2).filter(user=user2).first() assert noti.end_timestamp < now() @@ -89,8 +92,8 @@ def test_notification_cell(user, user2): assert cell.is_visible(context['request'].user) is True assert cell.get_badge(context) is None - id_noti1 = Notification.notify(user, 'notibar') - id_noti2 = Notification.notify(user, 'notifoo') + id_noti1, created = Notification.notify(user, 'notibar') + id_noti2, created = Notification.notify(user, 'notifoo') content = cell.render(context) assert 'notibar' in content assert 'notifoo' in content @@ -112,7 +115,7 @@ def test_notification_cell(user, user2): assert 'notiurl' in content assert 'https://www.example.net/' in content - ackme = Notification.notify(user, 'ackme') + ackme, created = Notification.notify(user, 'ackme') Notification.ack(user, id=ackme) content = cell.render(context) assert 'acked' in content -- 2.15.1