views.py
27.1 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
from django.shortcuts import render, get_object_or_404, redirect
from django.views.generic import ListView, CreateView, DeleteView, UpdateView, TemplateView, DetailView
from categories.models import Category
from django.core.urlresolvers import reverse_lazy
from rolepermissions.verifications import has_role
from django.db.models import Q
from django.contrib import messages
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.mixins import LoginRequiredMixin
from random import shuffle
from rolepermissions.mixins import HasRoleMixin
from categories.forms import CategoryForm
import operator
from braces import views
from subjects.models import Subject
from django.contrib.auth.decorators import login_required
from collections import namedtuple
from log.mixins import LogMixin
from log.decorators import log_decorator_ajax
from log.models import Log
from itertools import chain
from .models import Tag
import time
import datetime
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .forms import CreateSubjectForm, UpdateSubjectForm
from .utils import has_student_profile, has_professor_profile, count_subjects, get_category_page
from users.models import User
from topics.models import Topic, Resource
from amadeus.permissions import has_category_permissions, has_subject_permissions, has_subject_view_permissions, has_resource_permissions
class HomeView(LoginRequiredMixin, ListView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'subjects/initial.html'
context_object_name = 'subjects'
paginate_by = 10
total = 0
def get_queryset(self):
if self.request.user.is_staff:
subjects = Subject.objects.all().order_by("name")
else:
pk = self.request.user.pk
subjects = Subject.objects.filter(Q(students__pk=pk) | Q(professor__pk=pk) | Q(category__coordinators__pk=pk)).distinct()
self.total = subjects.count()
return subjects
def get_context_data(self, **kwargs):
context = super(HomeView, self).get_context_data(**kwargs)
context['title'] = _('Home')
context['show_buttons'] = True #So it shows subscribe and access buttons
#bringing users
tag_amount = 50
tags = Tag.objects.all()
tags_list = []
for tag in tags:
if len(tags_list) <= tag_amount:
if Resource.objects.filter(tags__pk=tag.pk, students__pk = self.request.user.pk).count() > 0 or Subject.objects.filter(tags__pk = tag.pk).count() > 0:
tags_list.append((tag.name, Subject.objects.filter(tags__pk = tag.pk).count()))
tags_list.sort(key= lambda x: x[1], reverse=True) #sort by value
elif len(tags_list) > tag_amount:
count = Subject.objects.filter(tags__pk = tag.pk).count()
if count > tags_list[tag_amount][1]:
tags_list[tag_amount - 1] = (tag.name, count)
tags_list.sort(key = lambda x: x[1], reverse=True)
i = 0
tags = []
for item in tags_list:
if i < tag_amount/10:
tags.append((item[0], 0))
elif i < tag_amount/2:
tags.append((item[0], 1))
else:
tags.append((item[0], 2))
i += 1
shuffle(tags)
context['tags'] = tags
context['total_subs'] = self.total
return context
class IndexView(LoginRequiredMixin, ListView):
totals = {}
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'subjects/list.html'
context_object_name = 'categories'
paginate_by = 10
def get_queryset(self):
self.totals['all_subjects'] = count_subjects(self.request.user)
self.totals['my_subjects'] = self.totals['all_subjects']
if self.request.user.is_staff:
categories = Category.objects.all().order_by('name')
else:
pk = self.request.user.pk
self.totals['my_subjects'] = count_subjects(self.request.user, False)
if not self.kwargs.get('option'):
my_categories = Category.objects.filter(Q(coordinators__pk=pk) | Q(subject_category__professor__pk=pk) | Q(subject_category__students__pk = pk, visible = True)).distinct().order_by('name')
categories = my_categories
else:
categories = Category.objects.filter(Q(coordinators__pk = pk) | Q(visible=True) ).distinct().order_by('name')
#if not self.request.user.is_staff:
#my_categories = [category for category in categories if self.request.user in category.coordinators.all() \
#or has_professor_profile(self.request.user, category) or has_student_profile(self.request.user, category)]
#So I remove all categories that doesn't have the possibility for the user to be on
return categories
def paginate_queryset(self, queryset, page_size):
paginator = self.get_paginator(
queryset, page_size, orphans=self.get_paginate_orphans(),
allow_empty_first_page=self.get_allow_empty())
page_kwarg = self.page_kwarg
page = self.kwargs.get(page_kwarg) or self.request.GET.get(page_kwarg) or 1
if self.kwargs.get('slug'):
categories = queryset
paginator = Paginator(categories, self.paginate_by)
page = get_category_page(categories, self.kwargs.get('slug'), self.paginate_by)
try:
page_number = int(page)
except ValueError:
if page == 'last':
page_number = paginator.num_pages
else:
raise Http404(_("Page is not 'last', nor can it be converted to an int."))
try:
page = paginator.page(page_number)
return (paginator, page, page.object_list, page.has_other_pages())
except InvalidPage as e:
raise Http404(_('Invalid page (%(page_number)s): %(message)s') % {
'page_number': page_number,
'message': str(e)
})
def render_to_response(self, context, **response_kwargs):
if self.request.user.is_staff:
context['page_template'] = "categories/home_admin_content.html"
else:
context['page_template'] = "categories/home_teacher_student.html"
if self.request.is_ajax():
if self.request.user.is_staff:
self.template_name = "categories/home_admin_content.html"
return self.response_class(request = self.request, template = self.template_name, context = context, using = self.template_engine, **response_kwargs)
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['all'] = False
context['title'] = _('My Subjects')
context['show_buttons'] = True #So it shows subscribe and access buttons
context['totals'] = self.totals
if self.kwargs.get('option'):
context['all'] = True
context['title'] = _('All Subjects')
if self.kwargs.get('slug'):
context['cat_slug'] = self.kwargs.get('slug')
context['subjects_menu_active'] = 'subjects_menu_active'
return context
class GetSubjectList(LoginRequiredMixin, ListView):
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'subjects/_list.html'
model = Subject
context_object_name = 'subjects'
def get_queryset(self):
slug = self.kwargs.get('slug')
category = get_object_or_404(Category, slug = slug)
return category.subject_category.all()
def get_context_data(self, **kwargs):
context = super(GetSubjectList, self).get_context_data(**kwargs)
context['show_buttons'] = True #So it shows subscribe and access buttons
if 'all' in self.request.META.get('HTTP_REFERER'):
context['all'] = True
return context
class SubjectCreateView(LoginRequiredMixin, LogMixin, CreateView):
log_component = 'subject'
log_action = 'create'
log_resource = 'subject'
log_context = {}
model = Subject
template_name = "subjects/create.html"
login_url = reverse_lazy('users:login')
redirect_field_name = 'next'
form_class = CreateSubjectForm
success_url = reverse_lazy('subject:index')
def dispatch(self, request, *args, **kwargs):
if kwargs.get('subject_slug'):
subject = get_object_or_404(Subject, slug = kwargs.get('subject_slug', ''))
if not has_category_permissions(request.user, subject.category):
return redirect(reverse_lazy('subjects:home'))
if kwargs.get('slug'):
category = get_object_or_404(Category, slug = kwargs.get('slug', ''))
if not has_category_permissions(request.user, category):
return redirect(reverse_lazy('subjects:home'))
return super(SubjectCreateView, self).dispatch(request, *args, **kwargs)
def get_initial(self):
initial = super(SubjectCreateView, self).get_initial()
if self.kwargs.get('slug'): #when the user creates a subject
initial['category'] = Category.objects.all().filter(slug=self.kwargs['slug'])
# print (initial)
# initial['professor'] = User.objects.all()
if self.kwargs.get('subject_slug'): #when the user replicate a subject
subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug'])
initial = initial.copy()
initial['category'] = subject.category
initial['description'] = subject.description
initial['name'] = subject.name
initial['visible'] = subject.visible
initial['professor'] = subject.professor.all()
initial['tags'] = ", ".join(subject.tags.all().values_list("name", flat = True))
initial['init_date'] = subject.init_date
initial['end_date'] = subject.end_date
initial['students'] = subject.students.all()
initial['description_brief'] = subject.description_brief
self.log_action = 'replicate'
self.log_context['replicated_subject_id'] = subject.id
self.log_context['replicated_subject_name'] = subject.name
self.log_context['replicated_subject_slug'] = subject.slug
return initial
def get_context_data(self, **kwargs):
context = super(SubjectCreateView, self).get_context_data(**kwargs)
context['title'] = _('Create Subject')
try:
students_selected = context['form'].cleaned_data['students'].values_list('id',flat=True)
professors_selected = context['form'].cleaned_data['professor'].values_list('id',flat=True)
context['form'].fields['professor'].queryset = context['form'].fields['professor'].queryset.exclude(id__in=students_selected)
context['form'].fields['students'].queryset = context['form'].fields['students'].queryset.exclude(id__in=professors_selected)
except AttributeError:
pass
if self.kwargs.get('slug'):
context['slug'] = self.kwargs['slug']
if self.kwargs.get('subject_slug'):
context['title'] = _('Replicate Subject')
subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug'])
context['slug'] = subject.category.slug
context['replicate'] = True
context['subject'] = subject
context['subjects_menu_active'] = 'subjects_menu_active'
return context
def form_valid(self, form):
self.object = form.save()
if self.kwargs.get('slug'):
self.object.category = Category.objects.get(slug=self.kwargs['slug'])
if self.kwargs.get('subject_slug'):
subject = get_object_or_404(Subject, slug = self.kwargs['subject_slug'])
self.object.category = subject.category
self.object.save()
self.log_context['category_id'] = self.object.category.id
self.log_context['category_name'] = self.object.category.name
self.log_context['category_slug'] = self.object.category.slug
self.log_context['subject_id'] = self.object.id
self.log_context['subject_name'] = self.object.name
self.log_context['subject_slug'] = self.object.slug
super(SubjectCreateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context)
return super(SubjectCreateView, self).form_valid(form)
def get_success_url(self):
if not self.object.category.visible:
self.object.visible = False
self.object.save()
messages.success(self.request, _('The Subject "%s" was registered on "%s" Category successfully!')%(self.object.name, self.object.category.name ))
return reverse_lazy('subjects:index')
class SubjectUpdateView(LoginRequiredMixin, LogMixin, UpdateView):
log_component = 'subject'
log_action = 'update'
log_resource = 'subject'
log_context = {}
model = Subject
form_class = UpdateSubjectForm
template_name = 'subjects/update.html'
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
def dispatch(self, request, *args, **kwargs):
subject = get_object_or_404(Subject, slug = kwargs.get('slug', ''))
self.subject = subject
if not has_subject_permissions(request.user, subject):
return redirect(reverse_lazy('subjects:home'))
return super(SubjectUpdateView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(SubjectUpdateView, self).get_context_data(**kwargs)
try:
students_selected = context['form'].cleaned_data['students'].values_list('id',flat=True)
professors_selected = context['form'].cleaned_data['professor'].values_list('id',flat=True)
except AttributeError:
students_selected = self.subject.students.all().values_list('id',flat=True)
professors_selected = self.subject.professor.all().values_list('id',flat=True)
context['form'].fields['professor'].queryset = context['form'].fields['professor'].queryset.exclude(id__in=students_selected)
context['form'].fields['students'].queryset = context['form'].fields['students'].queryset.exclude(id__in=professors_selected)
context['title'] = _('Update Subject')
context['template_extends'] = 'categories/home.html'
context['subjects_menu_active'] = 'subjects_menu_active'
return context
def get_success_url(self):
if not self.object.category.visible:
self.object.visible = False
self.object.save()
if not self.object.visible:
Topic.objects.filter(subject = self.object, repository = False).update(visible = False)
Resource.objects.filter(topic__subject = self.object, topic__repository = False).update(visible = False)
self.log_context['category_id'] = self.object.category.id
self.log_context['category_name'] = self.object.category.name
self.log_context['category_slug'] = self.object.category.slug
self.log_context['subject_id'] = self.object.id
self.log_context['subject_name'] = self.object.name
self.log_context['subject_slug'] = self.object.slug
super(SubjectUpdateView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context)
messages.success(self.request, _('The Subject "%s" was updated on "%s" Category successfully!')%(self.object.name, self.object.category.name ))
return reverse_lazy('subjects:index')
class SubjectDeleteView(LoginRequiredMixin, LogMixin, DeleteView):
log_component = 'subject'
log_action = 'delete'
log_resource = 'subject'
log_context = {}
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
model = Subject
template_name = 'subjects/delete.html'
def dispatch(self, request, *args, **kwargs):
subject = get_object_or_404(Subject, slug = kwargs.get('slug', ''))
if not has_subject_permissions(request.user, subject):
return redirect(reverse_lazy('subjects:home'))
return super(SubjectDeleteView, self).dispatch(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
self.object.delete()
if not (self.request.GET.get('view') == 'index'): #It still falling all the time into this if, I need to fix this
return self.ajax_success()
return HttpResponseRedirect(self.get_success_url())
def ajax_success(self):
self.log_context['category_id'] = self.object.category.id
self.log_context['category_name'] = self.object.category.name
self.log_context['category_slug'] = self.object.category.slug
self.log_context['subject_id'] = self.object.id
self.log_context['subject_name'] = self.object.name
self.log_context['subject_slug'] = self.object.slug
super(SubjectDeleteView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context)
messages.success(self.request, _('Subject "%s" removed successfully!')%(self.object.name))
return JsonResponse({'url':reverse_lazy('subjects:index')})
def get(self, request, *args, **kwargs):
self.object = self.get_object()
if self.object.students.count() > 0:
messages.error(self.request, _("Subject can't be removed. The subject still possess students and learning objects associated"))
return JsonResponse({'error':True,'url':reverse_lazy('subjects:index')})
for topic in self.object.topic_subject.all():
if topic.resource_topic.count() > 0:
messages.error(self.request, _("Subject can't be removed. The subject still possess students and learning objects associated"))
return JsonResponse({'error':True,'url':reverse_lazy('subjects:index')})
context = self.get_context_data(object=self.object)
return self.render_to_response(context)
def post(self, *args, **kwargs):
return self.delete(*args, **kwargs)
def get_context_data(self, **kwargs):
context = super(SubjectDeleteView, self).get_context_data(**kwargs)
subject = get_object_or_404(Subject, slug = self.kwargs.get('slug'))
context['subject'] = subject
if (self.request.GET.get('view') == 'index'):
context['index'] = True
else:
context['index'] = False
return context
def get_success_url(self):
self.log_context['category_id'] = self.object.category.id
self.log_context['category_name'] = self.object.category.name
self.log_context['category_slug'] = self.object.category.slug
self.log_context['subject_id'] = self.object.id
self.log_context['subject_name'] = self.object.name
self.log_context['subject_slug'] = self.object.slug
super(SubjectDeleteView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context)
messages.success(self.request, _('Subject "%s" removed successfully!')%(self.object.name))
return reverse_lazy('subjects:index')
class SubjectDetailView(LoginRequiredMixin, LogMixin, DetailView):
log_component = 'subject'
log_action = 'access'
log_resource = 'subject'
log_context = {}
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
model = Subject
template_name = 'subjects/view.html'
context_object_name = 'subject'
def dispatch(self, request, *args,**kwargs):
subject = get_object_or_404(Subject, slug = kwargs.get('slug', ''))
if not has_subject_view_permissions(request.user, subject):
return redirect(reverse_lazy('subjects:home'))
return super(SubjectDetailView, self).dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super(SubjectDetailView, self).get_context_data(**kwargs)
context['title'] = self.object.name
resources = self.request.session.get('resources', None)
if resources:
context['resource_new_page'] = resources['new_page']
context['resource_new_page_url'] = resources['new_page_url']
self.request.session['resources'] = None
if self.kwargs.get('topic_slug'):
context['topic_slug'] = self.kwargs.get('topic_slug')
self.log_context['category_id'] = self.object.category.id
self.log_context['category_name'] = self.object.category.name
self.log_context['category_slug'] = self.object.category.slug
self.log_context['subject_id'] = self.object.id
self.log_context['subject_name'] = self.object.name
self.log_context['subject_slug'] = self.object.slug
self.log_context['timestamp_start'] = str(int(time.time()))
super(SubjectDetailView, 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 SubjectSubscribeView(LoginRequiredMixin, LogMixin, TemplateView):
log_component = 'subject'
log_action = 'subscribe'
log_resource = 'subject'
log_context = {}
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'subjects/subscribe.html'
def get_context_data(self, **kwargs):
context = super(SubjectSubscribeView, self).get_context_data(**kwargs)
context['subject'] = get_object_or_404(Subject, slug= kwargs.get('slug'))
return context
def post(self, request, *args, **kwargs):
subject = get_object_or_404(Subject, slug= kwargs.get('slug'))
if subject.subscribe_end < datetime.datetime.today().date():
messages.error(self.request, _('Subscription date is due!'))
else:
self.log_context['category_id'] = subject.category.id
self.log_context['category_name'] = subject.category.name
self.log_context['category_slug'] = subject.category.slug
self.log_context['subject_id'] = subject.id
self.log_context['subject_name'] = subject.name
self.log_context['subject_slug'] = subject.slug
super(SubjectSubscribeView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context)
subject.students.add(request.user)
subject.save()
messages.success(self.request, _('Subscription was successfull!'))
return JsonResponse({'url':reverse_lazy('subjects:index')})
class SubjectSearchView(LoginRequiredMixin, LogMixin, ListView):
log_component = 'subject'
log_action = 'search'
log_resource = 'subject/resources'
log_context = {}
login_url = reverse_lazy("users:login")
redirect_field_name = 'next'
template_name = 'subjects/list_search.html'
context_object_name = 'subjects'
paginate_by = 10
def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
tags = request.GET.get('search')
tags = tags.split(" ")
if tags[0] == '':
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
return super(SubjectSearchView, self).dispatch(request, *args, **kwargs)
def get_queryset(self):
tags = self.request.GET.get('search')
self.tags = tags
tags = tags.split(" ")
q = Q()
for tag in tags:
for word in tag.split(' '):
q = q | Q(tags__name__unaccent__iexact=word )
subjects = Subject.objects.filter(q).distinct()
self.resources = Resource.objects.select_related('link', 'filelink', 'webpage', 'ytvideo', 'pdffile').filter(q).distinct()
self.resources = [resource.id for resource in self.resources if has_resource_permissions(self.request.user, resource)]
self.resources = Resource.objects.select_related('link', 'filelink', 'webpage', 'ytvideo', 'pdffile').filter(id__in = self.resources)
self.totals = {'resources': self.resources.count(), 'my_subjects': subjects.count()}
option = self.kwargs.get('option')
if option and option == 'resources':
return self.resources
return subjects
def get_context_data(self, **kwargs):
context = super(SubjectSearchView, self).get_context_data(**kwargs)
if self.totals['resources'] == 0 and self.totals['my_subjects'] == 0:
context['empty'] = True
context['tags'] = self.tags
context['all'] = False
context['title'] = _('Subjects')
context['show_buttons'] = True #So it shows subscribe and access buttons
context['totals'] = self.totals
option = self.kwargs.get('option')
if option and option == 'resources':
context['all'] = True
context['title'] = _('Resources')
context['resources'] = self.resources
context['subjects_menu_active'] = ''
self.log_context['search_for'] = self.tags
super(SubjectSearchView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context)
return context
@log_decorator_ajax('subject', 'view', 'subject')
def subject_view_log(request, subject):
action = request.GET.get('action')
if action == 'open':
subject = get_object_or_404(Subject, id = subject)
log_context = {}
log_context['category_id'] = subject.category.id
log_context['category_name'] = subject.category.name
log_context['category_slug'] = subject.category.slug
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'})
"""
Subject view that returns a list of the most used subjects """
@login_required
def most_acessed_subjects(request):
data = {} #empty response
data = Log.objects.filter(resource = 'subject')
subjects = {}
for datum in data:
if datum.context:
subject_id = datum.context['subject_id']
if subject_id in subjects.keys():
subjects[subject_id]['count'] = subjects[subject_id]['count'] + 1
else:
subjects[subject_id] = {'name': datum.context['subject_name'], 'count':0 }
#order the values of the dictionary by the count in descendent order
subjects = sorted(subjects.values(), key = lambda x: x['count'], reverse=True )
subjects = subjects[:30]
return JsonResponse(subjects, safe=False)