Skip to content

Commit

Permalink
new feature: mark all notificationlogs as read, also fix bug in notif…
Browse files Browse the repository at this point in the history
…ication creation form
  • Loading branch information
Christian Glatthard committed Aug 12, 2015
1 parent abd8870 commit 3e4a2ff
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
1 change: 1 addition & 0 deletions ipynbsrv/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<pk>[0-9]+)$', views.NotificationLogDetail.as_view(), name="notificationlog_detail"),

)
Expand Down
21 changes: 19 additions & 2 deletions ipynbsrv/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'

Expand Down Expand Up @@ -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.
"""
Expand Down
14 changes: 11 additions & 3 deletions ipynbsrv/web/templates/web/notifications/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@
{% include 'web/snippets/messages.html' with messages=messages only %}

<h1>Notifications</h1>
<form role="form">
<button type="button" class="btn btn-primary btn-create pull-right" data-toggle="modal" data-target="#modal-notification-create">Create new</button>
</form>
<fieldset class="pull-right btn-create">
<form role="form" style="display: inline-block;">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-notification-create">Create new</button>
</form>

<form role="form" method="POST" action="{% url 'notifications_mark_all_as_read' %}" style="display: inline-block;">
{% csrf_token %}
<button type="submit" class="button btn btn-primary btn-mark_all_as_read" {% if new_notifications_count == 0 %}disabled{% endif %}>Mark all as read</button>
</form>
</fieldset>

{% if notifications %}

<table id="notifications-table" class="table table-hover table-striped js-table-smart" cellspacing="0" width="100%">
<thead>
<tr>
Expand Down
12 changes: 6 additions & 6 deletions ipynbsrv/web/templates/web/notifications/modal_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h4 class="modal-title">New notification</h4>
<label for="receiver_groups">Receiving Group</label>
<div class="select">
<select class="form-control enableCaseInsensitiveFiltering" name="receiver_groups">
<option>None</option>
<option value="" selected disabled>None</option>
{% for group in groups %}
{% if not group.is_single_user_group %}
<option value="{{ group.id }}">{{ group.name }}</option>
Expand All @@ -27,7 +27,7 @@ <h4 class="modal-title">New notification</h4>
<label for="type">Notification Type</label>
<div class="select">
<select class="form-control enableCaseInsensitiveFiltering" name="notification_type">
<option>None</option>
<option value="" selected disabled>None</option>
{% for n1, n2 in notification_types %}
<option value="{{ n1 }}">{{ n2 }}</option>

Expand All @@ -40,7 +40,7 @@ <h4 class="modal-title">New notification</h4>
<label for="type">Container</label>
<div class="select">
<select class="form-control enableCaseInsensitiveFiltering" name="container">
<option>None</option>
<option value="" selected disabled>None</option>
{% for c in containers %}
<option value="{{ c.id }}">{{ c.name }}</option>

Expand All @@ -54,7 +54,7 @@ <h4 class="modal-title">New notification</h4>
<label for="type">Container Image</label>
<div class="select">
<select class="form-control enableCaseInsensitiveFiltering" name="container_image">
<option>None</option>
<option value="" selected disabled>None</option>
{% for ci in container_images %}
<option value="{{ ci.id }}">{{ ci.name }}</option>

Expand All @@ -69,7 +69,7 @@ <h4 class="modal-title">New notification</h4>
<label for="type">Group</label>
<div class="select">
<select class="form-control enableCaseInsensitiveFiltering" name="group">
<option>None</option>
<option value="" selected disabled>None</option>
{% for g in groups %}
{% if not group.is_single_user_group %}
<option value="{{ g.id }}">{{ g.name }}</option>
Expand All @@ -84,7 +84,7 @@ <h4 class="modal-title">New notification</h4>
<label for="type">Share</label>
<div class="select">
<select class="form-control enableCaseInsensitiveFiltering" name="share">
<option>None</option>
<option value="" selected disabled>None</option>
{% for s in shares %}
<option value="{{ s.id }}">{{ s.name }}</option>

Expand Down
2 changes: 2 additions & 0 deletions ipynbsrv/web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
24 changes: 24 additions & 0 deletions ipynbsrv/web/views/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

0 comments on commit 3e4a2ff

Please sign in to comment.