From fe5b3c41aa898fa7491a7ec9bce28c5e1c5b542d Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 6 Nov 2020 18:21:48 +0530 Subject: Statistics app for video tracking --- stats/views.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 stats/views.py (limited to 'stats/views.py') diff --git a/stats/views.py b/stats/views.py new file mode 100644 index 0000000..ddbc1b3 --- /dev/null +++ b/stats/views.py @@ -0,0 +1,54 @@ +# Django Imports +from django.shortcuts import render, get_object_or_404 +from django.http import JsonResponse +from django.utils import timezone +from django.contrib.auth.decorators import login_required +from django.core.paginator import Paginator + +# Local Imports +from stats.models import TrackLesson +from yaksh.models import Course +from yaksh.decorators import email_verified + + +@login_required +@email_verified +def add_tracker(request, tracker_id): + user = request.user + track = get_object_or_404( + TrackLesson.objects.select_related("course"), id=tracker_id + ) + if not track.course.is_student(user): + raise Http404("You are not enrolled in this course") + context = {} + video_duration = request.POST.get('video_duration') + current_time = request.POST.get('current_video_time') + if current_time: + track.current_time = current_time + track.video_duration = video_duration + track.last_access_time = timezone.now() + track.save() + success = True + else: + success = False + context = {"success": success} + return JsonResponse(context) + + +@login_required +@email_verified +def view_lesson_watch_stats(request, course_id, lesson_id): + user = request.user + course = get_object_or_404(Course, pk=course_id) + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('This course does not belong to you') + trackings = TrackLesson.objects.get_queryset().filter( + course_id=course_id, lesson_id=lesson_id + ).order_by("id") + total = trackings.count() + paginator = Paginator(trackings, 30) + page = request.GET.get('page') + trackings = paginator.get_page(page) + context = {'objects': trackings, 'total': total, 'course_id': course_id, + 'lesson_id': lesson_id} + return render(request, 'view_lesson_tracking.html', context) -- cgit From 6b5b21fc26879c1724bf02952584196f6c302b91 Mon Sep 17 00:00:00 2001 From: adityacp Date: Sat, 7 Nov 2020 11:58:22 +0530 Subject: Add tests for lesson tracking --- stats/views.py | 1 + 1 file changed, 1 insertion(+) (limited to 'stats/views.py') diff --git a/stats/views.py b/stats/views.py index ddbc1b3..3bfe9c3 100644 --- a/stats/views.py +++ b/stats/views.py @@ -4,6 +4,7 @@ from django.http import JsonResponse from django.utils import timezone from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator +from django.http import Http404 # Local Imports from stats.models import TrackLesson -- cgit From d09ff51b6c957137e705fee73f1808c6333eed7f Mon Sep 17 00:00:00 2001 From: adityacp Date: Sat, 7 Nov 2020 18:54:15 +0530 Subject: Add additional model to store last access for lesson --- stats/views.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'stats/views.py') diff --git a/stats/views.py b/stats/views.py index 3bfe9c3..f7c028f 100644 --- a/stats/views.py +++ b/stats/views.py @@ -7,7 +7,7 @@ from django.core.paginator import Paginator from django.http import Http404 # Local Imports -from stats.models import TrackLesson +from stats.models import TrackLesson, LessonLog from yaksh.models import Course from yaksh.decorators import email_verified @@ -27,7 +27,9 @@ def add_tracker(request, tracker_id): if current_time: track.current_time = current_time track.video_duration = video_duration - track.last_access_time = timezone.now() + LessonLog.objects.create( + track_id=track.id, last_access_time=timezone.now() + ) track.save() success = True else: -- cgit