3a85edab3e2792dc1c4badb75aa1f2efa707aa95
[laas.git] / src / notifier / views.py
1 ##############################################################################
2 # Copyright (c) 2018 Sawyer Bergeron and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 from django.shortcuts import render
11 from notifier.models import Notification
12 from django.db.models import Q
13
14
15 def InboxView(request):
16     if request.user.is_authenticated:
17         user = request.user
18     else:
19         return render(request, "dashboard/login.html",
20                       {'title': 'Authentication Required'})
21
22     return render(request,
23                   "notifier/inbox.html",
24                   {'unread_notifications': Notification.objects.filter(recipients=user.userprofile).order_by('-id').filter(~Q(read_by=user.userprofile)),
25                       'read_notifications': Notification.objects.filter(recipients=user.userprofile).order_by('-id').filter(read_by=user.userprofile)})
26
27
28 def NotificationView(request, notification_id):
29
30     if request.user.is_authenticated:
31         user = request.user
32     else:
33         return render(request,
34                       "dashboard/login.html",
35                       {'title': 'Authentication Required'})
36
37     notification = Notification.objects.get(id=notification_id)
38     if user.userprofile not in notification.recipients.all():
39         return render(request,
40                       "dashboard/login.html", {'title': 'Access Denied'})
41
42     notification.read_by.add(user.userprofile)
43     notification.save()
44     if request.method == 'POST':
45         if 'delete' in request.POST:
46             # handle deleting
47             notification.recipients.remove(user.userprofile)
48             if not notification.recipients.exists():
49                 notification.delete()
50             else:
51                 notification.save()
52
53         if 'unread' in request.POST:
54             notification.read_by.remove(user.userprofile)
55             notification.save()
56
57     return render(request,
58                   "notifier/notification.html", {'notification': notification})