aboutsummaryrefslogtreecommitdiffstats
path: root/src/notifier/views.py
blob: 3a85edab3e2792dc1c4badb75aa1f2efa707aa95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
##############################################################################
# Copyright (c) 2018 Sawyer Bergeron and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

from django.shortcuts import render
from notifier.models import Notification
from django.db.models import Q


def InboxView(request):
    if request.user.is_authenticated:
        user = request.user
    else:
        return render(request, "dashboard/login.html",
                      {'title': 'Authentication Required'})

    return render(request,
                  "notifier/inbox.html",
                  {'unread_notifications': Notification.objects.filter(recipients=user.userprofile).order_by('-id').filter(~Q(read_by=user.userprofile)),
                      'read_notifications': Notification.objects.filter(recipients=user.userprofile).order_by('-id').filter(read_by=user.userprofile)})


def NotificationView(request, notification_id):

    if request.user.is_authenticated:
        user = request.user
    else:
        return render(request,
                      "dashboard/login.html",
                      {'title': 'Authentication Required'})

    notification = Notification.objects.get(id=notification_id)
    if user.userprofile not in notification.recipients.all():
        return render(request,
                      "dashboard/login.html", {'title': 'Access Denied'})

    notification.read_by.add(user.userprofile)
    notification.save()
    if request.method == 'POST':
        if 'delete' in request.POST:
            # handle deleting
            notification.recipients.remove(user.userprofile)
            if not notification.recipients.exists():
                notification.delete()
            else:
                notification.save()

        if 'unread' in request.POST:
            notification.read_by.remove(user.userprofile)
            notification.save()

    return render(request,
                  "notifier/notification.html", {'notification': notification})