From 3e4a2ffadd2dd7618aab3796bd20feefc98f47f9 Mon Sep 17 00:00:00 2001 From: Christian Glatthard Date: Wed, 12 Aug 2015 19:18:50 +0200 Subject: [PATCH] new feature: mark all notificationlogs as read, also fix bug in notification creation form --- ipynbsrv/api/urls.py | 1 + ipynbsrv/api/views.py | 21 ++++++++++++++-- .../templates/web/notifications/index.html | 14 ++++++++--- .../web/notifications/modal_create.html | 12 +++++----- ipynbsrv/web/urls.py | 2 ++ ipynbsrv/web/views/notifications.py | 24 +++++++++++++++++++ 6 files changed, 63 insertions(+), 11 deletions(-) diff --git a/ipynbsrv/api/urls.py b/ipynbsrv/api/urls.py index 4980342..373ba42 100644 --- a/ipynbsrv/api/urls.py +++ b/ipynbsrv/api/urls.py @@ -76,6 +76,7 @@ # /api/notificationlogs(/)... url(r'^notificationlogs/?$', views.NotificationLogList.as_view(), name="notificationlogs"), url(r'^notificationlogs/unread$', views.NotificationLogUnreadList.as_view(), name="notificationlogs_unread"), + url(r'^notificationlogs/mark_all_as_read$', views.notificationlogs_mark_all_as_read, name="notificationlogs_mark_all_as_read"), url(r'^notificationlogs/(?P[0-9]+)$', views.NotificationLogDetail.as_view(), name="notificationlog_detail"), ) diff --git a/ipynbsrv/api/views.py b/ipynbsrv/api/views.py index b5110b6..f4ad5b2 100644 --- a/ipynbsrv/api/views.py +++ b/ipynbsrv/api/views.py @@ -83,7 +83,9 @@ def api_root(request, format=None): } available_endpoints['notificationlogs'] = { '': 'Get a list of all available notificationlogs.', - '{id}': 'Get details about a notificationlog.' + '{id}': 'Get details about a notificationlog.', + 'unread': 'Get all new notificationlogs.', + 'mark_all_as_read': 'Mark all your notificationlogs as read.' } available_endpoints['notificationtypes'] = 'Get a list of all available notificationtypes.' @@ -1044,8 +1046,23 @@ class NotificationLogDetail(generics.RetrieveUpdateAPIView): queryset = NotificationLog.objects.all() +@api_view(('POST',)) +def notificationlogs_mark_all_as_read(request): + """ + Mark all notificationlogs of a user as read. + """ + notifications = NotificationLog.objects.filter(user=request.user.backend_user) + count = 0 + for n in notifications: + if not n.read: + n.read=True + n.save() + count += 1 + return Response({"detail": "{} NotificationLog objects marked as read.".format(count), "count": count}) + + @api_view(('GET',)) -def notification_types(request, format=None): +def notification_types(request): """ Notification types. """ diff --git a/ipynbsrv/web/templates/web/notifications/index.html b/ipynbsrv/web/templates/web/notifications/index.html index ca2ad89..58e3357 100644 --- a/ipynbsrv/web/templates/web/notifications/index.html +++ b/ipynbsrv/web/templates/web/notifications/index.html @@ -14,11 +14,19 @@ {% include 'web/snippets/messages.html' with messages=messages only %}

Notifications

-
- -
+
+
+ +
+ +
+ {% csrf_token %} + +
+
{% if notifications %} + diff --git a/ipynbsrv/web/templates/web/notifications/modal_create.html b/ipynbsrv/web/templates/web/notifications/modal_create.html index e8a7811..c07222c 100644 --- a/ipynbsrv/web/templates/web/notifications/modal_create.html +++ b/ipynbsrv/web/templates/web/notifications/modal_create.html @@ -14,7 +14,7 @@
- + {% for n1, n2 in notification_types %} @@ -40,7 +40,7 @@
- + {% for ci in container_images %} @@ -69,7 +69,7 @@
- + {% for s in shares %} diff --git a/ipynbsrv/web/urls.py b/ipynbsrv/web/urls.py index d98de51..2703722 100644 --- a/ipynbsrv/web/urls.py +++ b/ipynbsrv/web/urls.py @@ -59,6 +59,8 @@ url(r'^notifications/$', 'ipynbsrv.web.views.notifications.index', name='notifications'), url(r'^notifications/create$', 'ipynbsrv.web.views.notifications.create', name='notification_create'), url(r'^notifications/mark_as_read$', 'ipynbsrv.web.views.notifications.mark_as_read', name='notification_mark_as_read'), + url(r'^notifications/mark_all_as_read$', 'ipynbsrv.web.views.notifications.mark_all_as_read', name='notifications_mark_all_as_read'), + # internal url(r'^_workspace_auth_check$', 'ipynbsrv.core.auth.checks.workspace_auth_access'), diff --git a/ipynbsrv/web/views/notifications.py b/ipynbsrv/web/views/notifications.py index b1e2a69..d010198 100644 --- a/ipynbsrv/web/views/notifications.py +++ b/ipynbsrv/web/views/notifications.py @@ -95,3 +95,27 @@ def mark_as_read(request): messages.error(request, api_error_message(e, params)) return redirect('notifications') + + +@user_passes_test(login_allowed) +def mark_all_as_read(request): + if request.method != "POST": + messages.error(request, "Invalid request method.") + return redirect('notifications') + + client = get_httpclient_instance(request) + + try: + response = client.notificationlogs.mark_all_as_read.post() + count = response.get('count') + if count == 0: + messages.info(request, "All Notifications already marked as read.") + elif count == 1: + messages.success(request, "{} Notification marked as read.".format(count)) + else: + messages.success(request, "{} Notifications marked as read.".format(count)) + + except Exception as e: + messages.error(request, api_error_message(e, "")) + + return redirect('notifications') \ No newline at end of file