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/models.py | 13 ++++++++++++- stats/templates/view_lesson_tracking.html | 8 ++++++-- stats/views.py | 6 ++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/stats/models.py b/stats/models.py index f2f1bce..95def40 100644 --- a/stats/models.py +++ b/stats/models.py @@ -13,12 +13,23 @@ class TrackLesson(models.Model): lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE) current_time = models.CharField(max_length=100, default="00:00:00") video_duration = models.CharField(max_length=100, default="00:00:00") - last_access_time = models.DateTimeField(default=timezone.now) creation_time = models.DateTimeField(auto_now_add=True) class Meta: unique_together = ('user', 'course', 'lesson') + def get_last_access_time_and_vists(self): + lesson_logs = self.lessonlog_set + last_access_time = None + if lesson_logs.exists(): + last_access_time = lesson_logs.last().last_access_time + return last_access_time, lesson_logs.count() + def __str__(self): return (f"Track {self.lesson} in {self.course} " f"for {self.user.get_full_name()}") + + +class LessonLog(models.Model): + track = models.ForeignKey(TrackLesson, on_delete=models.CASCADE) + last_access_time = models.DateTimeField(default=timezone.now) diff --git a/stats/templates/view_lesson_tracking.html b/stats/templates/view_lesson_tracking.html index fa891e3..7962410 100644 --- a/stats/templates/view_lesson_tracking.html +++ b/stats/templates/view_lesson_tracking.html @@ -47,20 +47,24 @@ Student Name  Last access on  Started on  - Current Time  + Current Duration  Video Duration  Percentage watched  + Total visits  {% for track in trackings %} {{ forloop.counter0|add:objects.start_index }} {{track.user.get_full_name}} - {{track.last_access_time}} + {% with track.get_last_access_time_and_vists as time_and_visits %} + {{time_and_visits.0}} {{track.creation_time}} {{track.current_time}} {{track.video_duration}} + {{time_and_visits.1}} + {% endwith %} {% endfor %} 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