notifications: add API to get count of new notifications (#29365)

This commit is contained in:
Frédéric Péters 2018-12-27 20:12:48 +01:00
parent 9e3fd0a1f2
commit fcd1a6115b
3 changed files with 29 additions and 1 deletions

View File

@ -86,3 +86,15 @@ class Forget(GenericAPIView):
return Response({'err': 0})
forget = Forget.as_view()
class Count(GenericAPIView):
authentication_classes = (authentication.SessionAuthentication,)
permission_classes = (permissions.IsAuthenticated,)
def get(self, request, *args, **kwargs):
total = Notification.objects.visible(request.user).count()
new = Notification.objects.visible(request.user).new().count()
return Response({'err': 0, 'total': total, 'new': new})
count = Count.as_view()

View File

@ -16,7 +16,7 @@
from django.conf.urls import url
from .api_views import add, ack, forget
from .api_views import add, ack, forget, count
urlpatterns = [
url('^api/notification/add/$', add,
@ -25,4 +25,6 @@ urlpatterns = [
name='api-notification-ack'),
url(r'^api/notification/forget/(?P<notification_id>[\w-]+)/$', forget,
name='api-notification-forget'),
url('^api/notification/count/$', count,
name='api-notification-count'),
]

View File

@ -189,6 +189,20 @@ def test_notification_ws(john_doe):
assert notif.acked is True
assert notif.end_timestamp < now()
resp = client.get(reverse('api-notification-count'))
assert resp.status_code == 200
assert resp.json()['new'] == 3
assert resp.json()['total'] == 3
resp = client.get(reverse('api-notification-ack', kwargs={'notification_id': '1'}))
resp = client.get(reverse('api-notification-count'))
assert resp.status_code == 200
assert resp.json()['new'] == 2
assert resp.json()['total'] == 3
resp = client.get(reverse('api-notification-forget', kwargs={'notification_id': '1'}))
resp = client.get(reverse('api-notification-count'))
assert resp.status_code == 200
assert resp.json()['new'] == 2
assert resp.json()['total'] == 2
def test_notification_ws_badrequest(john_doe):