notifications: precise if notification is created in notify api (#21189)
This commit is contained in:
parent
c4635be6bd
commit
92eff8df04
|
@ -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'),
|
||||
|
@ -54,7 +54,7 @@ class Add(GenericAPIView):
|
|||
end_timestamp=data.get('end_timestamp'),
|
||||
duration=data.get('duration')
|
||||
)
|
||||
response = {'err': 0, 'data': {'id': notification_id}}
|
||||
response = {'err': 0, 'data': {'id': notification_id, 'created': created}}
|
||||
return Response(response)
|
||||
|
||||
add = Add.as_view()
|
||||
|
|
|
@ -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):
|
||||
|
|
|
@ -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
|
||||
|
@ -140,7 +143,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}, 'err': 0}
|
||||
assert result == {'data': {'id': check_id, 'created': True}, 'err': 0}
|
||||
assert Notification.objects.filter(user=user).count() == count
|
||||
return Notification.objects.filter_by_id(check_id).last()
|
||||
|
||||
|
|
Loading…
Reference in New Issue