From ced86a14dc387abbfa053991e5dd015d5a0bb31c Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 16 Mar 2018 16:54:43 +0100 Subject: [PATCH 7/8] notifications: do not return created from .notify() (to be rebased) --- combo/apps/notifications/api_views.py | 4 ++-- combo/apps/notifications/models.py | 5 ++--- tests/test_notification.py | 20 +++++++++----------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/combo/apps/notifications/api_views.py b/combo/apps/notifications/api_views.py index b026af1..8737241 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 try: - notification, created = Notification.notify( + notification = Notification.notify( user=request.user, summary=data['summary'], id=data.get('id'), @@ -58,7 +58,7 @@ class Add(GenericAPIView): response = {'err': 1, 'err_desc': {'id': [unicode(e)]}} return Response(response, status.HTTP_400_BAD_REQUEST) else: - response = {'err': 0, 'data': {'id': notification.public_id, 'created': created}} + response = {'err': 0, 'data': {'id': notification.public_id}} return Response(response) add = Add.as_view() diff --git a/combo/apps/notifications/models.py b/combo/apps/notifications/models.py index e4611e1..5422b05 100644 --- a/combo/apps/notifications/models.py +++ b/combo/apps/notifications/models.py @@ -121,7 +121,7 @@ class Notification(models.Model): # id is maybe an implicit id notification = Notification.objects.get(pk=pk) Notification.objects.filter(pk=pk).update(**defaults) - return notification, False + return notification except (ValueError, TypeError, Notification.DoesNotExist): pass @@ -137,8 +137,7 @@ class Notification(models.Model): defaults=defaults) else: notification = Notification.objects.create(user=user, **defaults) - created = True - return notification, created + return notification @property def visible(self): diff --git a/tests/test_notification.py b/tests/test_notification.py index b14f41c..f19ce45 100644 --- a/tests/test_notification.py +++ b/tests/test_notification.py @@ -43,8 +43,7 @@ def login(username='admin', password='admin'): def test_notification_api(user, user2): - notification, created = Notification.notify(user, 'notifoo') - assert created + notification = Notification.notify(user, 'notifoo') assert Notification.objects.count() == 1 assert notification.summary == 'notifoo' assert notification.body == '' @@ -66,13 +65,12 @@ def test_notification_api(user, user2): noti = Notification.objects.get() assert noti.end_timestamp - noti.start_timestamp == timedelta(seconds=3600) - notification, created = Notification.notify(user, 'notibar', id='ns:notibar') + notification = Notification.notify(user, 'notibar', id='ns:notibar') assert Notification.objects.count() == 2 - notification, created = Notification.notify(user, 'notirebar', id='ns:notibar') - assert not created + notification = Notification.notify(user, 'notirebar', id='ns:notibar') assert Notification.objects.count() == 2 - notification, created = Notification.notify(user2, 'notiother') + notification = Notification.notify(user2, 'notiother') notification.forget() assert Notification.objects.filter(user=user2).count() == 1 notification = Notification.objects.filter(user=user2).get() @@ -94,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 - notification1, created = Notification.notify(user, 'notibar') - notification2, created = Notification.notify(user, 'notifoo') + notification1 = Notification.notify(user, 'notibar') + notification2 = Notification.notify(user, 'notifoo') content = cell.render(context) assert 'notibar' in content assert 'notifoo' in content @@ -117,7 +115,7 @@ def test_notification_cell(user, user2): assert 'notiurl' in content assert 'https://www.example.net/' in content - notification3, created = Notification.notify(user, 'ackme') + notification3 = Notification.notify(user, 'ackme') notification3.ack() content = cell.render(context) assert 'acked' in content @@ -146,7 +144,7 @@ def test_notification_ws(user): content_type='application/json') assert resp.status_code == 200 result = json.loads(resp.content) - assert result == {'data': {'id': check_id, 'created': True}, 'err': 0} + assert result == {'data': {'id': check_id}, 'err': 0} assert Notification.objects.filter(user=user).count() == count return Notification.objects.find(user, check_id).get() @@ -247,7 +245,7 @@ def test_notification_id_and_origin(user): result = notify({'summary': 'foo', 'id': '1'}) assert result['err'] == 1 - notification, created = Notification.notify(user, 'foo') + notification = Notification.notify(user, 'foo') result = notify({'summary': 'foo', 'id': str(notification.id)}) assert result['err'] == 0 -- 2.14.2