0001-notifications-precise-if-notification-is-created-in-.patch
combo/apps/notifications/api_views.py | ||
---|---|---|
43 | 43 |
return Response(response, status.HTTP_400_BAD_REQUEST) |
44 | 44 |
data = serializer.validated_data |
45 | 45 | |
46 |
notification_id = Notification.notify( |
|
46 |
notification_id, created = Notification.notify(
|
|
47 | 47 |
user=request.user, |
48 | 48 |
summary=data['summary'], |
49 | 49 |
id=data.get('id'), |
... | ... | |
54 | 54 |
end_timestamp=data.get('end_timestamp'), |
55 | 55 |
duration=data.get('duration') |
56 | 56 |
) |
57 |
response = {'err': 0, 'data': {'id': notification_id}} |
|
57 |
response = {'err': 0, 'data': {'id': notification_id, 'created': created}}
|
|
58 | 58 |
return Response(response) |
59 | 59 | |
60 | 60 |
add = Add.as_view() |
combo/apps/notifications/models.py | ||
---|---|---|
68 | 68 |
Renew an existing notification, or create a new one, with an external_id: |
69 | 69 |
Notification.notify(user, 'summary', id='id') |
70 | 70 |
''' |
71 |
created = False |
|
71 | 72 |
# get ... |
72 | 73 |
notification = Notification.objects.filter_by_id(id).filter(user=user).first() if id else None |
73 | 74 |
if not notification: # ... or create |
74 | 75 |
notification = Notification(user=user, summary=summary, |
75 | 76 |
body=body or '', url=url or '', |
76 | 77 |
external_id=id) |
78 |
created = True |
|
77 | 79 |
notification.summary = summary |
78 | 80 |
notification.body = body or '' |
79 | 81 |
notification.url = url or '' |
... | ... | |
89 | 91 |
notification.save() |
90 | 92 | |
91 | 93 |
if notification.external_id is None: |
92 |
return '%s' % notification.pk
|
|
94 |
notification_id = '%s' % notification.pk
|
|
93 | 95 |
else: |
94 |
return notification.external_id |
|
96 |
notification_id = notification.external_id |
|
97 |
return notification_id, created |
|
95 | 98 | |
96 | 99 |
@classmethod |
97 | 100 |
def ack(cls, user, id): |
tests/test_notification.py | ||
---|---|---|
40 | 40 | |
41 | 41 | |
42 | 42 |
def test_notification_api(user, user2): |
43 |
id_notifoo = Notification.notify(user, 'notifoo') |
|
43 |
id_notifoo, created = Notification.notify(user, 'notifoo') |
|
44 |
assert created |
|
44 | 45 |
assert Notification.objects.all().count() == 1 |
45 | 46 |
noti = Notification.objects.filter_by_id(id_notifoo).filter(user=user).first() |
46 | 47 |
assert noti.pk == int(id_notifoo) |
... | ... | |
64 | 65 |
noti = Notification.objects.filter_by_id(id_notifoo).filter(user=user).first() |
65 | 66 |
assert noti.end_timestamp - noti.start_timestamp == timedelta(seconds=3600) |
66 | 67 | |
67 |
Notification.notify(user, 'notibar', id='notibar') |
|
68 |
notification, created = Notification.notify(user, 'notibar', id='notibar') |
|
69 |
assert created |
|
68 | 70 |
assert Notification.objects.all().count() == 2 |
69 |
Notification.notify(user, 'notirebar', id='notibar') |
|
71 |
notification, created = Notification.notify(user, 'notirebar', id='notibar') |
|
72 |
assert not created |
|
70 | 73 |
assert Notification.objects.all().count() == 2 |
71 | 74 | |
72 |
id2 = Notification.notify(user2, 'notiother') |
|
75 |
id2, created = Notification.notify(user2, 'notiother')
|
|
73 | 76 |
Notification.forget(user2, id2) |
74 | 77 |
noti = Notification.objects.filter_by_id(id2).filter(user=user2).first() |
75 | 78 |
assert noti.end_timestamp < now() |
... | ... | |
89 | 92 |
assert cell.is_visible(context['request'].user) is True |
90 | 93 |
assert cell.get_badge(context) is None |
91 | 94 | |
92 |
id_noti1 = Notification.notify(user, 'notibar') |
|
93 |
id_noti2 = Notification.notify(user, 'notifoo') |
|
95 |
id_noti1, created = Notification.notify(user, 'notibar')
|
|
96 |
id_noti2, created = Notification.notify(user, 'notifoo')
|
|
94 | 97 |
content = cell.render(context) |
95 | 98 |
assert 'notibar' in content |
96 | 99 |
assert 'notifoo' in content |
... | ... | |
112 | 115 |
assert 'notiurl' in content |
113 | 116 |
assert 'https://www.example.net/' in content |
114 | 117 | |
115 |
ackme = Notification.notify(user, 'ackme') |
|
118 |
ackme, created = Notification.notify(user, 'ackme')
|
|
116 | 119 |
Notification.ack(user, id=ackme) |
117 | 120 |
content = cell.render(context) |
118 | 121 |
assert 'acked' in content |
... | ... | |
140 | 143 |
content_type='application/json') |
141 | 144 |
assert resp.status_code == 200 |
142 | 145 |
result = json.loads(resp.content) |
143 |
assert result == {'data': {'id': check_id}, 'err': 0} |
|
146 |
assert result == {'data': {'id': check_id, 'created': True}, 'err': 0}
|
|
144 | 147 |
assert Notification.objects.filter(user=user).count() == count |
145 | 148 |
return Notification.objects.filter_by_id(check_id).last() |
146 | 149 | |
147 |
- |