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)
|
return Response(response, status.HTTP_400_BAD_REQUEST)
|
||||||
data = serializer.validated_data
|
data = serializer.validated_data
|
||||||
|
|
||||||
notification_id = Notification.notify(
|
notification_id, created = Notification.notify(
|
||||||
user=request.user,
|
user=request.user,
|
||||||
summary=data['summary'],
|
summary=data['summary'],
|
||||||
id=data.get('id'),
|
id=data.get('id'),
|
||||||
|
@ -54,7 +54,7 @@ class Add(GenericAPIView):
|
||||||
end_timestamp=data.get('end_timestamp'),
|
end_timestamp=data.get('end_timestamp'),
|
||||||
duration=data.get('duration')
|
duration=data.get('duration')
|
||||||
)
|
)
|
||||||
response = {'err': 0, 'data': {'id': notification_id}}
|
response = {'err': 0, 'data': {'id': notification_id, 'created': created}}
|
||||||
return Response(response)
|
return Response(response)
|
||||||
|
|
||||||
add = Add.as_view()
|
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:
|
Renew an existing notification, or create a new one, with an external_id:
|
||||||
Notification.notify(user, 'summary', id='id')
|
Notification.notify(user, 'summary', id='id')
|
||||||
'''
|
'''
|
||||||
|
created = False
|
||||||
# get ...
|
# get ...
|
||||||
notification = Notification.objects.filter_by_id(id).filter(user=user).first() if id else None
|
notification = Notification.objects.filter_by_id(id).filter(user=user).first() if id else None
|
||||||
if not notification: # ... or create
|
if not notification: # ... or create
|
||||||
notification = Notification(user=user, summary=summary,
|
notification = Notification(user=user, summary=summary,
|
||||||
body=body or '', url=url or '',
|
body=body or '', url=url or '',
|
||||||
external_id=id)
|
external_id=id)
|
||||||
|
created = True
|
||||||
notification.summary = summary
|
notification.summary = summary
|
||||||
notification.body = body or ''
|
notification.body = body or ''
|
||||||
notification.url = url or ''
|
notification.url = url or ''
|
||||||
|
@ -89,9 +91,10 @@ class Notification(models.Model):
|
||||||
notification.save()
|
notification.save()
|
||||||
|
|
||||||
if notification.external_id is None:
|
if notification.external_id is None:
|
||||||
return '%s' % notification.pk
|
notification_id = '%s' % notification.pk
|
||||||
else:
|
else:
|
||||||
return notification.external_id
|
notification_id = notification.external_id
|
||||||
|
return notification_id, created
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def ack(cls, user, id):
|
def ack(cls, user, id):
|
||||||
|
|
|
@ -40,7 +40,8 @@ def login(username='admin', password='admin'):
|
||||||
|
|
||||||
|
|
||||||
def test_notification_api(user, user2):
|
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
|
assert Notification.objects.all().count() == 1
|
||||||
noti = Notification.objects.filter_by_id(id_notifoo).filter(user=user).first()
|
noti = Notification.objects.filter_by_id(id_notifoo).filter(user=user).first()
|
||||||
assert noti.pk == int(id_notifoo)
|
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()
|
noti = Notification.objects.filter_by_id(id_notifoo).filter(user=user).first()
|
||||||
assert noti.end_timestamp - noti.start_timestamp == timedelta(seconds=3600)
|
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
|
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
|
assert Notification.objects.all().count() == 2
|
||||||
|
|
||||||
id2 = Notification.notify(user2, 'notiother')
|
id2, created = Notification.notify(user2, 'notiother')
|
||||||
Notification.forget(user2, id2)
|
Notification.forget(user2, id2)
|
||||||
noti = Notification.objects.filter_by_id(id2).filter(user=user2).first()
|
noti = Notification.objects.filter_by_id(id2).filter(user=user2).first()
|
||||||
assert noti.end_timestamp < now()
|
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.is_visible(context['request'].user) is True
|
||||||
assert cell.get_badge(context) is None
|
assert cell.get_badge(context) is None
|
||||||
|
|
||||||
id_noti1 = Notification.notify(user, 'notibar')
|
id_noti1, created = Notification.notify(user, 'notibar')
|
||||||
id_noti2 = Notification.notify(user, 'notifoo')
|
id_noti2, created = Notification.notify(user, 'notifoo')
|
||||||
content = cell.render(context)
|
content = cell.render(context)
|
||||||
assert 'notibar' in content
|
assert 'notibar' in content
|
||||||
assert 'notifoo' in content
|
assert 'notifoo' in content
|
||||||
|
@ -112,7 +115,7 @@ def test_notification_cell(user, user2):
|
||||||
assert 'notiurl' in content
|
assert 'notiurl' in content
|
||||||
assert 'https://www.example.net/' 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)
|
Notification.ack(user, id=ackme)
|
||||||
content = cell.render(context)
|
content = cell.render(context)
|
||||||
assert 'acked' in content
|
assert 'acked' in content
|
||||||
|
@ -140,7 +143,7 @@ def test_notification_ws(user):
|
||||||
content_type='application/json')
|
content_type='application/json')
|
||||||
assert resp.status_code == 200
|
assert resp.status_code == 200
|
||||||
result = json.loads(resp.content)
|
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
|
assert Notification.objects.filter(user=user).count() == count
|
||||||
return Notification.objects.filter_by_id(check_id).last()
|
return Notification.objects.filter_by_id(check_id).last()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue