forked from shubham7saxena/Event-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
154 lines (111 loc) · 4.11 KB
/
views.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from django import get_version
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.forms import model_to_dict
from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator
from django.views.generic import ListView
from .utils import slug2id
from .models import Notification
from distutils.version import StrictVersion
if StrictVersion(get_version()) >= StrictVersion('1.7.0'):
from django.http import JsonResponse
else:
# Django 1.6 doesn't have a proper JsonResponse
import json
from django.http import HttpResponse
def date_handler(obj):
return obj.isoformat() if hasattr(obj, 'isoformat') else obj
def JsonResponse(data):
return HttpResponse(
json.dumps(data, default=date_handler),
content_type="application/json")
class NotificationViewList(ListView):
template_name = 'notifications/list.html'
context_object_name = 'notifications'
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(NotificationViewList, self).dispatch(
request, *args, **kwargs)
class AllNotificationsList(NotificationViewList):
"""
Index page for authenticated user
"""
def get_queryset(self):
if getattr(settings, 'NOTIFICATIONS_SOFT_DELETE', False):
qs = self.request.user.notifications.active()
else:
qs = self.request.user.notifications.all()
return qs
class UnreadNotificationsList(NotificationViewList):
def get_queryset(self):
return self.request.user.notifications.unread()
@login_required
def mark_all_as_read(request):
request.user.notifications.mark_all_as_read()
_next = request.GET.get('next')
if _next:
return redirect(_next)
return redirect('notifications:all')
@login_required
def mark_as_read(request, slug=None):
id = slug2id(slug)
notification = get_object_or_404(
Notification, recipient=request.user, id=id)
notification.mark_as_read()
_next = request.GET.get('next')
if _next:
return redirect(_next)
return redirect('notifications:all')
@login_required
def mark_as_unread(request, slug=None):
id = slug2id(slug)
notification = get_object_or_404(
Notification, recipient=request.user, id=id)
notification.mark_as_unread()
_next = request.GET.get('next')
if _next:
return redirect(_next)
return redirect('notifications:all')
@login_required
def delete(request, slug=None):
_id = slug2id(slug)
notification = get_object_or_404(
Notification, recipient=request.user, id=_id)
if getattr(settings, 'NOTIFICATIONS_SOFT_DELETE', False):
notification.deleted = True
notification.save()
else:
notification.delete()
_next = request.GET.get('next')
if _next:
return redirect(_next)
return redirect('notifications:all')
def live_unread_notification_count(request):
data = {
'unread_count': request.user.notifications.unread().count(),
}
return JsonResponse(data)
def live_unread_notification_list(request):
try:
num_to_fetch = request.GET.get('max', 5) # If they don't specify, make it 5.
num_to_fetch = int(num_to_fetch)
num_to_fetch = max(1, num_to_fetch) # if num_to_fetch is negative, force at least one fetched notifications
num_to_fetch = min(num_to_fetch, 100) # put a sane ceiling on the number retrievable
except ValueError:
num_to_fetch = 5 # If casting to an int fails, just make it 5.
unread_list = []
for n in request.user.notifications.unread()[0:num_to_fetch]:
struct = model_to_dict(n)
if n.actor:
struct['actor'] = str(n.actor)
if n.target:
struct['target'] = str(n.target)
if n.action_object:
struct['action_object'] = str(n.action_object)
unread_list.append(struct)
data = {
'unread_count': request.user.notifications.unread().count(),
'unread_list': unread_list
}
return JsonResponse(data)