import os from django.db import models from django.utils.translation import gettext_lazy as _ from filer.fields.image import FilerImageField from filer.fields.file import FilerFileField from apps.account.models import StudentUser def lesson_file_upload_to(instance, filename): return os.path.join(f"courses/{instance.course.slug}/lessons/{filename}") class Lesson(models.Model): class ContentTypeChoices(models.TextChoices): LINK = 'link', 'Link' FILE = 'file', 'File' course = models.ForeignKey("course.Course", on_delete=models.CASCADE, related_name='lessons', verbose_name='Course') title = models.CharField(max_length=255, verbose_name='Lesson Title') priority = models.IntegerField(null=True, blank=True, verbose_name='Priority') is_active = models.BooleanField(default=True, verbose_name=_('Is Active')) duration = models.PositiveIntegerField(verbose_name='Duration (in minutes)') content_type = models.CharField(max_length=10, choices=ContentTypeChoices.choices, verbose_name='Content Type') content_file = models.FileField( null=True, blank=True, upload_to=lesson_file_upload_to, ) video_link = models.CharField(max_length=500, null=True, blank=True, verbose_name='Video Link') created_at = models.DateTimeField(auto_now_add=True, verbose_name=_("Created at")) updated_at = models.DateTimeField(auto_now=True, verbose_name=_("Updated At")) def __str__(self): return f"{self.course.title} - {self.title}" def is_completed_by(self, student): return self.completions.filter(student=student).exists() class LessonCompletion(models.Model): student = models.ForeignKey( StudentUser, on_delete=models.CASCADE, related_name='lesson_completions' ) lesson = models.ForeignKey( Lesson, on_delete=models.CASCADE, related_name='completions' ) completed_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True, verbose_name=_("Created at")) class Meta: unique_together = ('student', 'lesson') def __str__(self): return f"{self.student.fullname} - {self.lesson.title} - Completed"