views.py
13 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from django.shortcuts import get_object_or_404, redirect, render
from django.views import generic
from django.contrib import messages
from django.core.urlresolvers import reverse, reverse_lazy
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from django.db.models import Q, Count
from dateutil import parser
from datetime import datetime
from django.utils import formats, timezone
from amadeus.permissions import has_subject_view_permissions, has_category_permission
from subjects.models import Subject
from categories.models import Category
from log.models import Log
from log.mixins import LogMixin
from log.decorators import log_decorator, log_decorator_ajax
import time
from .models import Notification
from .utils import get_order_by, is_date
class SubjectNotifications(LoginRequiredMixin, LogMixin, generic.ListView):
log_component = 'pendencies'
log_action = 'view'
log_resource = 'pendencies'
log_context = {}
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
context_object_name = 'notifications'
template_name = 'notifications/subject.html'
paginate_by = 10
total = 0
def dispatch(self, request, *args, **kwargs):
slug = self.kwargs.get('slug', '')
subject = get_object_or_404(Subject, slug = slug)
if not has_subject_view_permissions(request.user, subject):
return redirect(reverse_lazy('subjects:home'))
return super(SubjectNotifications, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
slug = self.kwargs.get('slug', '')
subject = get_object_or_404(Subject, slug = slug)
notifications = Notification.objects.filter(user = self.request.user, task__resource__topic__subject = subject, creation_date = datetime.now()).order_by("task__limit_date", "task__end_date")
notifications.update(viewed = True)
self.total = notifications.count()
return notifications
def get_context_data(self, **kwargs):
context = super(SubjectNotifications, self).get_context_data(**kwargs)
slug = self.kwargs.get('slug', '')
subject = get_object_or_404(Subject, slug = slug)
context['title'] = _('%s - Pendencies')%(subject.name)
context['subject'] = subject
context['total'] = self.total
self.log_context['subject_id'] = subject.id
self.log_context['subject_name'] = subject.name
self.log_context['subject_slug'] = subject.slug
self.log_context['view_page'] = self.request.GET.get("page", 1)
self.log_context['timestamp_start'] = str(int(time.time()))
super(SubjectNotifications, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context)
self.request.session['log_id'] = Log.objects.latest('id').id
return context
class SubjectHistory(LoginRequiredMixin, LogMixin, generic.ListView):
log_component = 'pendencies'
log_action = 'view_history'
log_resource = 'pendencies'
log_context = {}
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
context_object_name = 'notifications'
template_name = 'notifications/subject.html'
paginate_by = 10
total = 0
num_rows = 0
def dispatch(self, request, *args, **kwargs):
slug = self.kwargs.get('slug', '')
subject = get_object_or_404(Subject, slug = slug)
if not has_subject_view_permissions(request.user, subject):
return redirect(reverse_lazy('subjects:home'))
return super(SubjectHistory, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
slug = self.kwargs.get('slug', '')
subject = get_object_or_404(Subject, slug = slug)
order = get_order_by(self.request.GET.get("order_by", None))
search = self.request.GET.get("search", None)
notifications = Notification.objects.filter(user = self.request.user, task__resource__topic__subject = subject).order_by(*order)
self.total = notifications.filter(creation_date = datetime.now()).count()
if search:
queries = Q(task__resource__name__icontains = search)
queries |= Q(task__action__icontains = search)
if search.isdigit():
queries |= Q(level = search)
if is_date(search):
search_date = parser.parse(search)
search_date = timezone.make_aware(search_date, timezone.get_current_timezone())
queries |= Q(creation_date = search_date)
queries |= Q(task__limit_date = search_date)
queries |= Q(task__end_date = search_date)
queries |= Q(meta__date = search_date)
notifications = notifications.filter(queries).order_by(*order)
self.num_rows = notifications.count()
return notifications
def get_context_data(self, **kwargs):
context = super(SubjectHistory, self).get_context_data(**kwargs)
slug = self.kwargs.get('slug', '')
subject = get_object_or_404(Subject, slug = slug)
context['title'] = _('%s - Pendencies')%(subject.name)
context['subject'] = subject
context['history'] = True
context['total'] = self.total
context['rows'] = self.num_rows
context['searched'] = self.request.GET.get("search", "")
self.log_context['subject_id'] = subject.id
self.log_context['subject_name'] = subject.name
self.log_context['subject_slug'] = subject.slug
self.log_context['history_page'] = self.request.GET.get("page", 1)
self.log_context['searched'] = self.request.GET.get("search", "")
self.log_context['timestamp_start'] = str(int(time.time()))
super(SubjectHistory, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context)
self.request.session['log_id'] = Log.objects.latest('id').id
return context
class IndexView(LoginRequiredMixin, generic.ListView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
context_object_name = 'notifications'
template_name = 'notifications/index.html'
paginate_by = 10
def dispatch(self, request, *args, **kwargs):
cat = self.kwargs.get('slug', None)
if cat:
if not has_category_permission(request.user, cat):
return redirect(reverse_lazy('subjects:home'))
return super(IndexView, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
cat = self.kwargs.get('slug', None)
if cat:
notifications = Notification.objects.filter(user = self.request.user, creation_date = datetime.now(), task__resource__topic__subject__category__slug = cat).values('task__resource__topic__subject', 'task__resource__topic__subject__name').annotate(total = Count('task__resource__topic__subject'))
else:
notifications = Notification.objects.filter(user = self.request.user, creation_date = datetime.now()).values('task__resource__topic__subject', 'task__resource__topic__subject__name').annotate(total = Count('task__resource__topic__subject'))
return notifications
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['title'] = _('Pendencies')
cat = self.kwargs.get('slug', None)
if cat:
context['category'] = get_object_or_404(Category, slug = cat)
else:
context['pendencies_menu_active'] = "subjects_menu_active"
return context
class AjaxNotifications(LoginRequiredMixin, generic.ListView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
context_object_name = 'notifications'
template_name = 'notifications/_view.html'
def get_queryset(self):
subject_id = self.kwargs.get('id', '')
notifications = Notification.objects.filter(user = self.request.user, task__resource__topic__subject__id = subject_id, creation_date = datetime.now()).order_by("task__limit_date", "task__end_date")
notifications.update(viewed = True)
return notifications
class AjaxHistory(LoginRequiredMixin, generic.ListView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
context_object_name = 'notifications'
template_name = 'notifications/_ajax_history.html'
def get_queryset(self):
subject_id = self.kwargs.get('id', '')
order = get_order_by(self.request.GET.get("order_by", None))
search = self.request.GET.get("search", None)
notifications = Notification.objects.filter(user = self.request.user, task__resource__topic__subject__id = subject_id).order_by(*order)
if search:
queries = Q(task__resource__name__icontains = search)
queries |= Q(task__action__icontains = search)
if search.isdigit():
queries |= Q(level = search)
if is_date(search):
search_date = parser.parse(search)
search_date = timezone.make_aware(search_date, timezone.get_current_timezone())
queries |= Q(creation_date = search_date)
queries |= Q(task__limit_date = search_date)
queries |= Q(task__end_date = search_date)
queries |= Q(meta__date = search_date)
notifications = notifications.filter(queries).order_by(*order)
self.num_rows = notifications.count()
return notifications
def get_context_data(self, **kwargs):
context = super(AjaxHistory, self).get_context_data(**kwargs)
subject_id = self.kwargs.get('id', '')
context['subject_id'] = subject_id
context['rows'] = self.num_rows
context['searched'] = self.request.GET.get("search", "")
context['order_by'] = self.request.GET.get("order_by", "")
return context
@login_required
@log_decorator('pendencies', 'set_goal', 'pendencies')
def set_goal(request):
if request.method == "POST" and request.is_ajax():
meta = request.POST.get('meta', None)
if not meta:
return JsonResponse({'error': True, 'message': _('No goal date received')})
meta = parser.parse(meta)
notify_id = request.POST.get('id', None)
if not notify_id:
return JsonResponse({'error': True, 'message': _('Could not identify the notification')})
notification = get_object_or_404(Notification, id = notify_id)
meta = timezone.make_aware(meta, timezone.get_current_timezone())
if meta < timezone.now():
return JsonResponse({'error': True, 'message': _("The goal date should be equal or after today's date")})
if meta.date() > notification.task.resource.topic.subject.end_date:
return JsonResponse({'error': True, 'message': _("The goal date should be equal or before subject's date")})
notification.meta = meta
notification.save()
log_context = {}
log_context['notification_id'] = notification.id
log_context['notification'] = str(notification)
request.log_context = log_context
if notification.level == 2:
message = _('Your new goal to realize the task %s is %s')%(str(notification), formats.date_format(meta, "SHORT_DATETIME_FORMAT"))
else:
message = _('Your goal to realize the task %s is %s')%(str(notification), formats.date_format(meta, "SHORT_DATETIME_FORMAT"))
return JsonResponse({'error': False, 'message': message})
@log_decorator_ajax('pendencies', 'view', 'pendencies')
def pendencies_view_log(request, subject):
action = request.GET.get('action')
if action == 'open':
subject = get_object_or_404(Subject, id = subject)
log_context = {}
log_context['subject_id'] = subject.id
log_context['subject_name'] = subject.name
log_context['subject_slug'] = subject.slug
log_context['timestamp_start'] = str(int(time.time()))
log_context['timestamp_end'] = '-1'
request.log_context = log_context
log_id = Log.objects.latest('id').id
return JsonResponse({'message': 'ok', 'log_id': log_id})
return JsonResponse({'message': 'ok'})
@log_decorator_ajax('pendencies', 'view_history', 'pendencies')
def pendencies_hist_log(request, subject):
action = request.GET.get('action')
if action == 'open':
subject = get_object_or_404(Subject, id = subject)
log_context = {}
log_context['subject_id'] = subject.id
log_context['subject_name'] = subject.name
log_context['subject_slug'] = subject.slug
log_context['timestamp_start'] = str(int(time.time()))
log_context['timestamp_end'] = '-1'
request.log_context = log_context
log_id = Log.objects.latest('id').id
return JsonResponse({'message': 'ok', 'log_id': log_id})
return JsonResponse({'message': 'ok'})