Commit 103fc8cce0108b85bb69aba3cba4e416e7bdb675
1 parent
71437835
Exists in
master
and in
5 other branches
Changing timestamp format and make middleware ignore admin module requests [Issue: #231]
Showing
6 changed files
with
33 additions
and
39 deletions
Show diff stats
core/middleware.py
1 | from datetime import datetime | 1 | from datetime import datetime |
2 | +import time | ||
3 | +from django.core.urlresolvers import resolve | ||
2 | from django.shortcuts import get_object_or_404 | 4 | from django.shortcuts import get_object_or_404 |
3 | import json | 5 | import json |
4 | 6 | ||
@@ -9,37 +11,24 @@ class TimeSpentMiddleware(object): | @@ -9,37 +11,24 @@ class TimeSpentMiddleware(object): | ||
9 | self.get_response = get_response | 11 | self.get_response = get_response |
10 | 12 | ||
11 | def process_request(self, request): | 13 | def process_request(self, request): |
12 | - if not request.is_ajax(): | ||
13 | - log_id = request.session.get('log_id', None) | 14 | + app_names = resolve(request.path).app_names |
14 | 15 | ||
15 | - if not log_id is None: | ||
16 | - log = get_object_or_404(Log, id = log_id) | 16 | + if not 'admin' in app_names: |
17 | + if not request.is_ajax(): | ||
18 | + log_id = request.session.get('log_id', None) | ||
17 | 19 | ||
18 | - #date_time_click = datetime.strptime(request.session.get('time_spent'), "%Y-%m-%d %H:%M:%S.%f") | ||
19 | - #_now = datetime.now() | 20 | + if not log_id is None: |
21 | + log = get_object_or_404(Log, id = log_id) | ||
20 | 22 | ||
21 | - #time_spent = _now - date_time_click | 23 | + if type(log.context) == dict: |
24 | + log_context = log.context | ||
25 | + else: | ||
26 | + log_context = json.loads(log.context) | ||
22 | 27 | ||
23 | - #secs = time_spent.total_seconds() | ||
24 | - #hours = int(secs / 3600) | ||
25 | - #minutes = int(secs / 60) % 60 | ||
26 | - #secs = secs % 60 | 28 | + log_context['timestamp_end'] = str(int(time.time())) |
27 | 29 | ||
30 | + log.context = log_context | ||
28 | 31 | ||
29 | - if type(log.context) == dict: | ||
30 | - log_context = log.context | ||
31 | - else: | ||
32 | - log_context = json.loads(log.context) | 32 | + log.save() |
33 | 33 | ||
34 | - #time = {} | ||
35 | - #time['hours'] = hours | ||
36 | - #time['minutes'] = minutes | ||
37 | - #time['seconds'] = secs | ||
38 | - | ||
39 | - log_context['timestamp_end'] = str(datetime.now()) | ||
40 | - | ||
41 | - log.context = log_context | ||
42 | - | ||
43 | - log.save() | ||
44 | - | ||
45 | - request.session['log_id'] = None | 34 | + request.session['log_id'] = None |
courses/views.py
@@ -25,6 +25,7 @@ from courses.models import Material | @@ -25,6 +25,7 @@ from courses.models import Material | ||
25 | from django.urls import reverse | 25 | from django.urls import reverse |
26 | 26 | ||
27 | from datetime import date, datetime | 27 | from datetime import date, datetime |
28 | +import time | ||
28 | 29 | ||
29 | #API IMPORTS | 30 | #API IMPORTS |
30 | from rest_framework import viewsets, permissions | 31 | from rest_framework import viewsets, permissions |
@@ -345,7 +346,7 @@ class CourseView(LogMixin, NotificationMixin, generic.DetailView): | @@ -345,7 +346,7 @@ class CourseView(LogMixin, NotificationMixin, generic.DetailView): | ||
345 | self.log_context['course_slug'] = course.slug | 346 | self.log_context['course_slug'] = course.slug |
346 | self.log_context['course_category_id'] = course.category.id | 347 | self.log_context['course_category_id'] = course.category.id |
347 | self.log_context['course_category_name'] = course.category.name | 348 | self.log_context['course_category_name'] = course.category.name |
348 | - self.log_context['timestamp_start'] = str(datetime.now()) | 349 | + self.log_context['timestamp_start'] = str(int(time.time())) |
349 | 350 | ||
350 | super(CourseView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | 351 | super(CourseView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) |
351 | 352 | ||
@@ -552,7 +553,7 @@ class SubjectsView(LoginRequiredMixin, LogMixin, generic.ListView): | @@ -552,7 +553,7 @@ class SubjectsView(LoginRequiredMixin, LogMixin, generic.ListView): | ||
552 | self.log_context['course_slug'] = subject.course.slug | 553 | self.log_context['course_slug'] = subject.course.slug |
553 | self.log_context['course_category_id'] = subject.course.category.id | 554 | self.log_context['course_category_id'] = subject.course.category.id |
554 | self.log_context['course_category_name'] = subject.course.category.name | 555 | self.log_context['course_category_name'] = subject.course.category.name |
555 | - self.log_context['timestamp_start'] = str(datetime.now()) | 556 | + self.log_context['timestamp_start'] = str(int(time.time())) |
556 | 557 | ||
557 | super(SubjectsView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | 558 | super(SubjectsView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) |
558 | 559 | ||
@@ -657,7 +658,7 @@ class TopicsView(LoginRequiredMixin, LogMixin, generic.ListView): | @@ -657,7 +658,7 @@ class TopicsView(LoginRequiredMixin, LogMixin, generic.ListView): | ||
657 | self.log_context['course_slug'] = topic.subject.course.slug | 658 | self.log_context['course_slug'] = topic.subject.course.slug |
658 | self.log_context['course_category_id'] = topic.subject.course.category.id | 659 | self.log_context['course_category_id'] = topic.subject.course.category.id |
659 | self.log_context['course_category_name'] = topic.subject.course.category.name | 660 | self.log_context['course_category_name'] = topic.subject.course.category.name |
660 | - self.log_context['timestamp_start'] = str(datetime.now()) | 661 | + self.log_context['timestamp_start'] = str(int(time.time())) |
661 | 662 | ||
662 | super(TopicsView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | 663 | super(TopicsView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) |
663 | 664 | ||
@@ -1003,7 +1004,7 @@ class FileMaterialView(LoginRequiredMixin, LogMixin, generic.DetailView): | @@ -1003,7 +1004,7 @@ class FileMaterialView(LoginRequiredMixin, LogMixin, generic.DetailView): | ||
1003 | self.log_context['course_slug'] = file.topic.subject.course.slug | 1004 | self.log_context['course_slug'] = file.topic.subject.course.slug |
1004 | self.log_context['course_category_id'] = file.topic.subject.course.category.id | 1005 | self.log_context['course_category_id'] = file.topic.subject.course.category.id |
1005 | self.log_context['course_category_name'] = file.topic.subject.course.category.name | 1006 | self.log_context['course_category_name'] = file.topic.subject.course.category.name |
1006 | - self.log_context['timestamp_start'] = str(datetime.now()) | 1007 | + self.log_context['timestamp_start'] = str(int(time.time())) |
1007 | 1008 | ||
1008 | super(FileMaterialView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | 1009 | super(FileMaterialView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) |
1009 | 1010 |
exam/views.py
@@ -10,6 +10,7 @@ from rolepermissions.verifications import has_role | @@ -10,6 +10,7 @@ from rolepermissions.verifications import has_role | ||
10 | from rolepermissions.verifications import has_object_permission | 10 | from rolepermissions.verifications import has_object_permission |
11 | from django.db.models import Q | 11 | from django.db.models import Q |
12 | from datetime import datetime | 12 | from datetime import datetime |
13 | +import time | ||
13 | # from django.views.generic.edit import FormMixin | 14 | # from django.views.generic.edit import FormMixin |
14 | 15 | ||
15 | from .forms import ExamForm | 16 | from .forms import ExamForm |
@@ -59,7 +60,7 @@ class ViewExam(LoginRequiredMixin, LogMixin, generic.DetailView): | @@ -59,7 +60,7 @@ class ViewExam(LoginRequiredMixin, LogMixin, generic.DetailView): | ||
59 | self.log_context['course_slug'] = exam.topic.subject.course.slug | 60 | self.log_context['course_slug'] = exam.topic.subject.course.slug |
60 | self.log_context['course_category_id'] = exam.topic.subject.course.category.id | 61 | self.log_context['course_category_id'] = exam.topic.subject.course.category.id |
61 | self.log_context['course_category_name'] = exam.topic.subject.course.category.name | 62 | self.log_context['course_category_name'] = exam.topic.subject.course.category.name |
62 | - self.request.session['time_spent'] = str(datetime.now()) | 63 | + self.request.session['time_spent'] = str(int(time.time())) |
63 | 64 | ||
64 | super(ViewExam, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | 65 | super(ViewExam, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) |
65 | 66 | ||
@@ -318,7 +319,7 @@ class AnswerStudentExam(LoginRequiredMixin, LogMixin, generic.CreateView): | @@ -318,7 +319,7 @@ class AnswerStudentExam(LoginRequiredMixin, LogMixin, generic.CreateView): | ||
318 | minutes = int(secs / 60) % 60 | 319 | minutes = int(secs / 60) % 60 |
319 | secs = secs % 60 | 320 | secs = secs % 60 |
320 | 321 | ||
321 | - self.log_context['timestamp_end'] = str(datetime.now()) | 322 | + self.log_context['timestamp_end'] = str(int(time.time())) |
322 | self.log_context['time_spent'] = {} | 323 | self.log_context['time_spent'] = {} |
323 | self.log_context['time_spent']['hours'] = hours | 324 | self.log_context['time_spent']['hours'] = hours |
324 | self.log_context['time_spent']['minutes'] = minutes | 325 | self.log_context['time_spent']['minutes'] = minutes |
@@ -347,7 +348,7 @@ class AnswerStudentExam(LoginRequiredMixin, LogMixin, generic.CreateView): | @@ -347,7 +348,7 @@ class AnswerStudentExam(LoginRequiredMixin, LogMixin, generic.CreateView): | ||
347 | context['answers'] = answers | 348 | context['answers'] = answers |
348 | context['keys'] = keys | 349 | context['keys'] = keys |
349 | 350 | ||
350 | - self.log_context['timestamp_start'] = str(datetime.now()) | 351 | + self.log_context['timestamp_start'] = str(int(time.time())) |
351 | self.request.session['time_spent'] = str(datetime.now()) | 352 | self.request.session['time_spent'] = str(datetime.now()) |
352 | 353 | ||
353 | return context | 354 | return context |
forum/views.py
@@ -8,6 +8,7 @@ from django.core.paginator import Paginator, EmptyPage | @@ -8,6 +8,7 @@ from django.core.paginator import Paginator, EmptyPage | ||
8 | from django.http import Http404, JsonResponse | 8 | from django.http import Http404, JsonResponse |
9 | from django.urls import reverse | 9 | from django.urls import reverse |
10 | from django.template.loader import render_to_string | 10 | from django.template.loader import render_to_string |
11 | +import time | ||
11 | 12 | ||
12 | from rolepermissions.mixins import HasRoleMixin | 13 | from rolepermissions.mixins import HasRoleMixin |
13 | from rolepermissions.verifications import has_object_permission | 14 | from rolepermissions.verifications import has_object_permission |
@@ -229,7 +230,7 @@ class ForumDetailView(LoginRequiredMixin, LogMixin, generic.DetailView): | @@ -229,7 +230,7 @@ class ForumDetailView(LoginRequiredMixin, LogMixin, generic.DetailView): | ||
229 | self.log_context['course_slug'] = forum.topic.subject.course.slug | 230 | self.log_context['course_slug'] = forum.topic.subject.course.slug |
230 | self.log_context['course_category_id'] = forum.topic.subject.course.category.id | 231 | self.log_context['course_category_id'] = forum.topic.subject.course.category.id |
231 | self.log_context['course_category_name'] = forum.topic.subject.course.category.name | 232 | self.log_context['course_category_name'] = forum.topic.subject.course.category.name |
232 | - self.log_context['timestamp_start'] = str(datetime.now()) | 233 | + self.log_context['timestamp_start'] = str(int(time.time())) |
233 | 234 | ||
234 | super(ForumDetailView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | 235 | super(ForumDetailView, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) |
235 | 236 |
links/views.py
@@ -10,6 +10,7 @@ from core.mixins import NotificationMixin | @@ -10,6 +10,7 @@ from core.mixins import NotificationMixin | ||
10 | from django.urls import reverse | 10 | from django.urls import reverse |
11 | from django.core.files.base import ContentFile | 11 | from django.core.files.base import ContentFile |
12 | from rolepermissions.verifications import has_role | 12 | from rolepermissions.verifications import has_role |
13 | +import time | ||
13 | 14 | ||
14 | from core.models import Log | 15 | from core.models import Log |
15 | from core.mixins import LogMixin | 16 | from core.mixins import LogMixin |
@@ -244,7 +245,7 @@ class ViewLink(LoginRequiredMixin, HasRoleMixin, LogMixin, generic.DetailView): | @@ -244,7 +245,7 @@ class ViewLink(LoginRequiredMixin, HasRoleMixin, LogMixin, generic.DetailView): | ||
244 | self.log_context['course_slug'] = link.topic.subject.course.slug | 245 | self.log_context['course_slug'] = link.topic.subject.course.slug |
245 | self.log_context['course_category_id'] = link.topic.subject.course.category.id | 246 | self.log_context['course_category_id'] = link.topic.subject.course.category.id |
246 | self.log_context['course_category_name'] = link.topic.subject.course.category.name | 247 | self.log_context['course_category_name'] = link.topic.subject.course.category.name |
247 | - self.log_context['timestamp_start'] = str(datetime.now()) | 248 | + self.log_context['timestamp_start'] = str(int(time.time())) |
248 | 249 | ||
249 | super(ViewLink, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | 250 | super(ViewLink, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) |
250 | 251 |
poll/views.py
@@ -10,6 +10,7 @@ from rolepermissions.verifications import has_role | @@ -10,6 +10,7 @@ from rolepermissions.verifications import has_role | ||
10 | from rolepermissions.verifications import has_object_permission | 10 | from rolepermissions.verifications import has_object_permission |
11 | from django.db.models import Q | 11 | from django.db.models import Q |
12 | from django.urls import reverse | 12 | from django.urls import reverse |
13 | +import time | ||
13 | 14 | ||
14 | from .forms import PollForm | 15 | from .forms import PollForm |
15 | from .models import Poll, Answer, AnswersStudent | 16 | from .models import Poll, Answer, AnswersStudent |
@@ -48,7 +49,7 @@ class ViewPoll(LoginRequiredMixin, LogMixin, generic.DetailView): | @@ -48,7 +49,7 @@ class ViewPoll(LoginRequiredMixin, LogMixin, generic.DetailView): | ||
48 | self.log_context['course_slug'] = poll.topic.subject.course.slug | 49 | self.log_context['course_slug'] = poll.topic.subject.course.slug |
49 | self.log_context['course_category_id'] = poll.topic.subject.course.category.id | 50 | self.log_context['course_category_id'] = poll.topic.subject.course.category.id |
50 | self.log_context['course_category_name'] = poll.topic.subject.course.category.name | 51 | self.log_context['course_category_name'] = poll.topic.subject.course.category.name |
51 | - self.log_context['timestamp_start'] = str(datetime.now()) | 52 | + self.log_context['timestamp_start'] = str(int(time.time())) |
52 | 53 | ||
53 | super(ViewPoll, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) | 54 | super(ViewPoll, self).createLog(self.request.user, self.log_component, self.log_action, self.log_resource, self.log_context) |
54 | 55 | ||
@@ -315,7 +316,7 @@ class AnswerStudentPoll(LoginRequiredMixin, LogMixin, generic.CreateView): | @@ -315,7 +316,7 @@ class AnswerStudentPoll(LoginRequiredMixin, LogMixin, generic.CreateView): | ||
315 | def dispatch(self, *args, **kwargs): | 316 | def dispatch(self, *args, **kwargs): |
316 | if self.request.method == 'GET': | 317 | if self.request.method == 'GET': |
317 | self.request.session['time_spent'] = str(datetime.datetime.now()) | 318 | self.request.session['time_spent'] = str(datetime.datetime.now()) |
318 | - self.log_context['timestamp_start'] = str(datetime.now()) | 319 | + self.log_context['timestamp_start'] = str(int(time.time())) |
319 | 320 | ||
320 | return super(AnswerStudentPoll, self).dispatch(*args, **kwargs) | 321 | return super(AnswerStudentPoll, self).dispatch(*args, **kwargs) |
321 | 322 | ||
@@ -345,7 +346,7 @@ class AnswerStudentPoll(LoginRequiredMixin, LogMixin, generic.CreateView): | @@ -345,7 +346,7 @@ class AnswerStudentPoll(LoginRequiredMixin, LogMixin, generic.CreateView): | ||
345 | self.log_context['course_slug'] = poll.topic.subject.course.slug | 346 | self.log_context['course_slug'] = poll.topic.subject.course.slug |
346 | self.log_context['course_category_id'] = poll.topic.subject.course.category.id | 347 | self.log_context['course_category_id'] = poll.topic.subject.course.category.id |
347 | self.log_context['course_category_name'] = poll.topic.subject.course.category.name | 348 | self.log_context['course_category_name'] = poll.topic.subject.course.category.name |
348 | - self.log_context['timestamp_end'] = str(datetime.now()) | 349 | + self.log_context['timestamp_end'] = str(int(time.time())) |
349 | 350 | ||
350 | date_time_click = datetime.datetime.strptime(self.request.session.get('time_spent'), "%Y-%m-%d %H:%M:%S.%f") | 351 | date_time_click = datetime.datetime.strptime(self.request.session.get('time_spent'), "%Y-%m-%d %H:%M:%S.%f") |
351 | _now = datetime.datetime.now() | 352 | _now = datetime.datetime.now() |