forked from horilla-opensource/horilla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
261 lines (202 loc) · 7.72 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# -*- coding: utf-8 -*-
""" Django Notifications example views """
from distutils.version import ( # pylint: disable=no-name-in-module,import-error
StrictVersion,
)
from django import get_version
from django.contrib.auth.decorators import login_required
from django.forms import model_to_dict
from django.http import HttpResponse # noqa
from django.shortcuts import get_object_or_404, redirect
from django.utils.decorators import method_decorator
from django.views.decorators.cache import never_cache
from django.views.generic import ListView
from swapper import load_model
from base.models import NotificationSound
from notifications import settings
from notifications.settings import get_config
from notifications.utils import id2slug, slug2id
Notification = load_model("notifications", "Notification")
if StrictVersion(get_version()) >= StrictVersion("1.7.0"):
from django.http import JsonResponse # noqa
else:
# Django 1.6 doesn't have a proper JsonResponse
import json
def date_handler(obj):
return obj.isoformat() if hasattr(obj, "isoformat") else obj
def JsonResponse(data): # noqa
return HttpResponse(
json.dumps(data, default=date_handler), content_type="application/json"
)
class NotificationViewList(ListView):
template_name = "notifications/list.html"
context_object_name = "notifications"
paginate_by = settings.get_config()["PAGINATE_BY"]
@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 settings.get_config()["SOFT_DELETE"]:
qset = self.request.user.notifications.active()
else:
qset = self.request.user.notifications.all()
return qset
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:unread")
@login_required
def mark_as_read(request, slug=None):
notification_id = slug2id(slug)
notification = get_object_or_404(
Notification, recipient=request.user, id=notification_id
)
notification.mark_as_read()
_next = request.GET.get("next")
if _next:
return redirect(_next)
return redirect("notifications:unread")
@login_required
def mark_as_unread(request, slug=None):
notification_id = slug2id(slug)
notification = get_object_or_404(
Notification, recipient=request.user, id=notification_id
)
notification.mark_as_unread()
_next = request.GET.get("next")
if _next:
return redirect(_next)
return redirect("notifications:unread")
@login_required
def delete(request, slug=None):
notification_id = slug2id(slug)
notification = get_object_or_404(
Notification, recipient=request.user, id=notification_id
)
if settings.get_config()["SOFT_DELETE"]:
notification.deleted = True
notification.save()
else:
notification.delete()
_next = request.GET.get("next")
if _next:
return redirect(_next)
return redirect("notifications:all")
@never_cache
def live_unread_notification_count(request):
try:
user_is_authenticated = request.user.is_authenticated()
except TypeError: # Django >= 1.11
user_is_authenticated = request.user.is_authenticated
if not user_is_authenticated:
data = {"unread_count": 0}
else:
data = {
"unread_count": request.user.notifications.unread().count(),
}
return JsonResponse(data)
@never_cache
def live_unread_notification_list(request):
"""Return a json with a unread notification list"""
try:
user_is_authenticated = request.user.is_authenticated()
except TypeError: # Django >= 1.11
user_is_authenticated = request.user.is_authenticated
if not user_is_authenticated:
data = {"unread_count": 0, "unread_list": []}
return JsonResponse(data)
default_num_to_fetch = get_config()["NUM_TO_FETCH"]
try:
# If they don't specify, make it 5.
num_to_fetch = request.GET.get("max", default_num_to_fetch)
num_to_fetch = int(num_to_fetch)
if not (1 <= num_to_fetch <= 100):
num_to_fetch = default_num_to_fetch
except ValueError: # If casting to an int fails.
num_to_fetch = default_num_to_fetch
unread_list = []
for notification in request.user.notifications.unread()[0:num_to_fetch]:
struct = model_to_dict(notification)
struct["slug"] = id2slug(notification.id)
if notification.actor:
struct["actor"] = str(notification.actor)
if notification.target:
struct["target"] = str(notification.target)
if notification.action_object:
struct["action_object"] = str(notification.action_object)
if notification.data:
struct["data"] = notification.data
unread_list.append(struct)
if request.GET.get("mark_as_read"):
notification.mark_as_read()
data = {
"unread_count": request.user.notifications.unread().count(),
"unread_list": unread_list,
}
return JsonResponse(data)
@never_cache
def live_all_notification_list(request):
"""Return a json with a unread notification list"""
try:
user_is_authenticated = request.user.is_authenticated()
except TypeError: # Django >= 1.11
user_is_authenticated = request.user.is_authenticated
if not user_is_authenticated:
data = {"all_count": 0, "all_list": []}
return JsonResponse(data)
default_num_to_fetch = get_config()["NUM_TO_FETCH"]
try:
# If they don't specify, make it 5.
num_to_fetch = request.GET.get("max", default_num_to_fetch)
num_to_fetch = int(num_to_fetch)
if not (1 <= num_to_fetch <= 100):
num_to_fetch = default_num_to_fetch
except ValueError: # If casting to an int fails.
num_to_fetch = default_num_to_fetch
all_list = []
for notification in request.user.notifications.all()[0:num_to_fetch]:
struct = model_to_dict(notification)
struct["slug"] = id2slug(notification.id)
if notification.actor:
struct["actor"] = str(notification.actor)
if notification.target:
struct["target"] = str(notification.target)
if notification.action_object:
struct["action_object"] = str(notification.action_object)
if notification.data:
struct["data"] = notification.data
all_list.append(struct)
if request.GET.get("mark_as_read"):
notification.mark_as_read()
data = {"all_count": request.user.notifications.count(), "all_list": all_list}
return JsonResponse(data)
def live_all_notification_count(request):
try:
user_is_authenticated = request.user.is_authenticated()
except TypeError: # Django >= 1.11
user_is_authenticated = request.user.is_authenticated
if not user_is_authenticated:
data = {"all_count": 0}
else:
data = {
"all_count": request.user.notifications.count(),
}
return JsonResponse(data)
@login_required
def notification_sound(request):
employee = request.user.employee_get
sound, created = NotificationSound.objects.get_or_create(employee=employee)
if not created:
sound.sound_enabled = not sound.sound_enabled
sound.save()
return HttpResponse("")