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/models.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 stats/models.py (limited to 'stats/models.py') diff --git a/stats/models.py b/stats/models.py new file mode 100644 index 0000000..f2f1bce --- /dev/null +++ b/stats/models.py @@ -0,0 +1,24 @@ +# Django Imports +from django.db import models +from django.utils import timezone +from django.contrib.auth.models import User + +# Local Imports +from yaksh.models import Course, Lesson + + +class TrackLesson(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE) + course = models.ForeignKey(Course, on_delete=models.CASCADE) + 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 __str__(self): + return (f"Track {self.lesson} in {self.course} " + f"for {self.user.get_full_name()}") -- 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/models.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'stats/models.py') 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) -- cgit From b6f78c5db15cb7e34649ca67f3295dcb1a102bbe Mon Sep 17 00:00:00 2001 From: prathamesh Date: Mon, 9 Nov 2020 15:52:56 +0530 Subject: Modify Lesson Statistics - gives total spent time - sets initial percentage to less than 25% - gives completion status(True or False) --- stats/models.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 3 deletions(-) (limited to 'stats/models.py') diff --git a/stats/models.py b/stats/models.py index 95def40..0200a80 100644 --- a/stats/models.py +++ b/stats/models.py @@ -7,6 +7,19 @@ from django.contrib.auth.models import User from yaksh.models import Course, Lesson +def str_to_datetime(s): + return timezone.datetime.strptime(s, '%H:%M:%S') + + +def str_to_time(s): + return timezone.datetime.strptime(s, '%H:%M:%S').time() + + +def time_to_seconds(time): + return timezone.timedelta(hours=time.hour, minutes=time.minute, + seconds=time.second).total_seconds() + + class TrackLesson(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) course = models.ForeignKey(Course, on_delete=models.CASCADE) @@ -14,16 +27,71 @@ class TrackLesson(models.Model): current_time = models.CharField(max_length=100, default="00:00:00") video_duration = models.CharField(max_length=100, default="00:00:00") creation_time = models.DateTimeField(auto_now_add=True) + watched = models.BooleanField(default=False) class Meta: unique_together = ('user', 'course', 'lesson') - def get_last_access_time_and_vists(self): + def get_log_counter(self): + return self.lessonlog_set.count() + + def get_current_time(self): + if self.current_time == '00:00:00': + return 'just started' + return self.current_time + + def get_video_duration(self): + if self.video_duration == '00:00:00': + return 'will be available after 25% completion' + return self.video_duration + + def set_current_time(self, ct): + t = timezone.datetime.strptime(ct, '%H:%M:%S').time() + current = timezone.datetime.strptime(self.current_time, + '%H:%M:%S').time() + if t > current: + self.current_time = ct + + def get_percentage_complete(self): + if self.current_time == '00:00:00' and self.video_duration == '00:00:00': + return 'less than 25%' + duration = str_to_time(self.video_duration) + watch_time = str_to_time(self.current_time) + duration_seconds = time_to_seconds(duration) + watched_seconds = time_to_seconds(watch_time) + percentage = round((watched_seconds / duration_seconds) * 100) + return 'approx {0} %'.format(percentage) + + + def get_last_access_time(self): lesson_logs = self.lessonlog_set - last_access_time = None + last_access_time = self.creation_time if lesson_logs.exists(): last_access_time = lesson_logs.last().last_access_time - return last_access_time, lesson_logs.count() + return last_access_time + + def set_watched(self): + if self.current_time != '00:00:00' and self.video_duration != '00:00:00': + duration = str_to_time(self.video_duration) + watch_time = (str_to_datetime(self.current_time) + timezone.timedelta( + seconds=10)).time() + self.watched = watch_time >= duration + + def get_watched(self): + if not self.watched: + self.set_watched() + self.save() + return self.watched + + + def time_spent(self): + if self.video_duration != '00:00:00': + hits = self.get_log_counter() + duration = str_to_time(self.video_duration) + hit_duration = int((time_to_seconds(duration))/4) + total_duration = hits * hit_duration + return str(timezone.timedelta(seconds=total_duration)) + return self.get_current_time() def __str__(self): return (f"Track {self.lesson} in {self.course} " @@ -32,4 +100,5 @@ class TrackLesson(models.Model): class LessonLog(models.Model): track = models.ForeignKey(TrackLesson, on_delete=models.CASCADE) + current_time = models.CharField(max_length=20, default="00:00:00") last_access_time = models.DateTimeField(default=timezone.now) -- cgit