Projet

Général

Profil

0007-notifications-do-not-return-created-from-.notify-to-.patch

Benjamin Dauvergne, 18 mars 2018 22:13

Télécharger (5,45 ko)

Voir les différences:

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(-)
combo/apps/notifications/api_views.py
43 43
            return Response(response, status.HTTP_400_BAD_REQUEST)
44 44
        data = serializer.validated_data
45 45
        try:
46
            notification, created = Notification.notify(
46
            notification = Notification.notify(
47 47
                user=request.user,
48 48
                summary=data['summary'],
49 49
                id=data.get('id'),
......
58 58
            response = {'err': 1, 'err_desc': {'id': [unicode(e)]}}
59 59
            return Response(response, status.HTTP_400_BAD_REQUEST)
60 60
        else:
61
            response = {'err': 0, 'data': {'id': notification.public_id, 'created': created}}
61
            response = {'err': 0, 'data': {'id': notification.public_id}}
62 62
            return Response(response)
63 63

  
64 64
add = Add.as_view()
combo/apps/notifications/models.py
121 121
            # id is maybe an implicit id
122 122
            notification = Notification.objects.get(pk=pk)
123 123
            Notification.objects.filter(pk=pk).update(**defaults)
124
            return notification, False
124
            return notification
125 125
        except (ValueError, TypeError, Notification.DoesNotExist):
126 126
            pass
127 127

  
......
137 137
                defaults=defaults)
138 138
        else:
139 139
            notification = Notification.objects.create(user=user, **defaults)
140
            created = True
141
        return notification, created
140
        return notification
142 141

  
143 142
    @property
144 143
    def visible(self):
tests/test_notification.py
43 43

  
44 44

  
45 45
def test_notification_api(user, user2):
46
    notification, created = Notification.notify(user, 'notifoo')
47
    assert created
46
    notification = Notification.notify(user, 'notifoo')
48 47
    assert Notification.objects.count() == 1
49 48
    assert notification.summary == 'notifoo'
50 49
    assert notification.body == ''
......
66 65
    noti = Notification.objects.get()
67 66
    assert noti.end_timestamp - noti.start_timestamp == timedelta(seconds=3600)
68 67

  
69
    notification, created = Notification.notify(user, 'notibar', id='ns:notibar')
68
    notification = Notification.notify(user, 'notibar', id='ns:notibar')
70 69
    assert Notification.objects.count() == 2
71
    notification, created = Notification.notify(user, 'notirebar', id='ns:notibar')
72
    assert not created
70
    notification = Notification.notify(user, 'notirebar', id='ns:notibar')
73 71
    assert Notification.objects.count() == 2
74 72

  
75
    notification, created = Notification.notify(user2, 'notiother')
73
    notification = Notification.notify(user2, 'notiother')
76 74
    notification.forget()
77 75
    assert Notification.objects.filter(user=user2).count() == 1
78 76
    notification = Notification.objects.filter(user=user2).get()
......
94 92
    assert cell.is_visible(context['request'].user) is True
95 93
    assert cell.get_badge(context) is None
96 94

  
97
    notification1, created = Notification.notify(user, 'notibar')
98
    notification2, created = Notification.notify(user, 'notifoo')
95
    notification1 = Notification.notify(user, 'notibar')
96
    notification2 = Notification.notify(user, 'notifoo')
99 97
    content = cell.render(context)
100 98
    assert 'notibar' in content
101 99
    assert 'notifoo' in content
......
117 115
    assert 'notiurl' in content
118 116
    assert 'https://www.example.net/' in content
119 117

  
120
    notification3, created = Notification.notify(user, 'ackme')
118
    notification3 = Notification.notify(user, 'ackme')
121 119
    notification3.ack()
122 120
    content = cell.render(context)
123 121
    assert 'acked' in content
......
146 144
                           content_type='application/json')
147 145
        assert resp.status_code == 200
148 146
        result = json.loads(resp.content)
149
        assert result == {'data': {'id': check_id, 'created': True}, 'err': 0}
147
        assert result == {'data': {'id': check_id}, 'err': 0}
150 148
        assert Notification.objects.filter(user=user).count() == count
151 149
        return Notification.objects.find(user, check_id).get()
152 150

  
......
247 245
    result = notify({'summary': 'foo', 'id': '1'})
248 246
    assert result['err'] == 1
249 247

  
250
    notification, created = Notification.notify(user, 'foo')
248
    notification = Notification.notify(user, 'foo')
251 249

  
252 250
    result = notify({'summary': 'foo', 'id': str(notification.id)})
253 251
    assert result['err'] == 0
254
-