You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
204 lines
6.5 KiB
204 lines
6.5 KiB
from rest_framework import serializers
|
|
|
|
from dj_filer.admin import get_thumbs
|
|
|
|
from apps.course.models import Course, CourseCategory, Attachment, Glossary, LessonCompletion, Participant
|
|
from apps.account.serializers import UserProfileSerializer
|
|
|
|
|
|
|
|
class CourseCategorySerializer(serializers.ModelSerializer):
|
|
course_count = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = CourseCategory
|
|
fields = ['name', 'slug', 'course_count']
|
|
|
|
def get_course_count(self, obj):
|
|
return obj.course_count
|
|
|
|
|
|
class CourseListSerializer(serializers.ModelSerializer):
|
|
category = CourseCategorySerializer()
|
|
thumbnail = serializers.SerializerMethodField()
|
|
participant_count = serializers.SerializerMethodField()
|
|
lessons_count = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Course
|
|
fields = [
|
|
'id',
|
|
'title',
|
|
'slug',
|
|
'participant_count',
|
|
'category',
|
|
'thumbnail',
|
|
'is_online',
|
|
'level',
|
|
'duration',
|
|
'lessons_count',
|
|
'short_description',
|
|
'status',
|
|
'is_free',
|
|
'price',
|
|
'discount_percentage',
|
|
'final_price',
|
|
]
|
|
|
|
def get_thumbnail(self, obj):
|
|
return get_thumbs(obj.thumbnail, self.context.get('request'))
|
|
|
|
def get_participant_count(self, obj):
|
|
return obj.participants.count()
|
|
|
|
def get_lessons_count(self, obj):
|
|
lessons_count = obj.lessons.filter(is_active=True).count()
|
|
return max(lessons_count, obj.lessons_count)
|
|
|
|
|
|
|
|
|
|
|
|
class CourseDetailSerializer(serializers.ModelSerializer):
|
|
category = CourseCategorySerializer()
|
|
professor = UserProfileSerializer()
|
|
thumbnail = serializers.SerializerMethodField()
|
|
participant_count = serializers.SerializerMethodField()
|
|
access = serializers.SerializerMethodField()
|
|
lessons_complated_count = serializers.SerializerMethodField()
|
|
lessons_count = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Course
|
|
fields = [
|
|
'id',
|
|
'title',
|
|
'slug',
|
|
'category',
|
|
'access',
|
|
'participant_count',
|
|
'professor',
|
|
'thumbnail',
|
|
'video_type',
|
|
'video_file',
|
|
'video_link',
|
|
'is_online',
|
|
'level',
|
|
'duration',
|
|
'lessons_count',
|
|
'lessons_complated_count',
|
|
'short_description',
|
|
'status',
|
|
'is_free',
|
|
'price',
|
|
'discount_percentage',
|
|
'final_price',
|
|
'timing',
|
|
'features',
|
|
]
|
|
|
|
def get_access(self, obj):
|
|
if student := self._get_authenticated_user():
|
|
if not self._is_participant(student, obj):
|
|
return False
|
|
return True
|
|
return False
|
|
|
|
def get_is_professor(self, obj):
|
|
if professor := self._get_authenticated_user():
|
|
return obj.professor == professor
|
|
return False
|
|
|
|
def get_lessons_count(self, obj):
|
|
lessons_count = obj.lessons.filter(is_active=True).count()
|
|
return max(lessons_count, obj.lessons_count)
|
|
|
|
|
|
def get_lessons_complated_count(self, obj):
|
|
if student := self._get_authenticated_user():
|
|
if not self._is_participant(student, obj):
|
|
return None
|
|
return self._get_completed_lessons_count(student, obj)
|
|
return None
|
|
|
|
def _is_participant(self, student, course):
|
|
"""Helper method to check if a student is a participant in the given course."""
|
|
return Participant.objects.filter(student=student, course=course).exists()
|
|
|
|
def _get_authenticated_user(self):
|
|
"""Helper method to retrieve the authenticated user from the context."""
|
|
request = self.context.get('request')
|
|
return request.user if request and request.user.is_authenticated else None
|
|
|
|
def _get_completed_lessons_count(self, student, course):
|
|
"""Helper method to count completed lessons for the student in the given course."""
|
|
return LessonCompletion.objects.filter(
|
|
student=student,
|
|
lesson__course=course
|
|
).count()
|
|
|
|
|
|
def get_thumbnail(self, obj):
|
|
return get_thumbs(obj.thumbnail, self.context.get('request'))
|
|
|
|
def get_participant_count(self, obj):
|
|
return obj.participants.count()
|
|
|
|
|
|
|
|
class MyCourseListSerializer(serializers.ModelSerializer):
|
|
category = CourseCategorySerializer()
|
|
thumbnail = serializers.SerializerMethodField()
|
|
lessons_complated_count = serializers.SerializerMethodField()
|
|
|
|
class Meta:
|
|
model = Course
|
|
fields = [
|
|
'id',
|
|
'title',
|
|
'slug',
|
|
'category',
|
|
'thumbnail',
|
|
'lessons_count',
|
|
'lessons_complated_count',
|
|
'short_description',
|
|
'status',
|
|
]
|
|
|
|
def get_thumbnail(self, obj):
|
|
return get_thumbs(obj.thumbnail, self.context.get('request'))
|
|
|
|
def get_lessons_complated_count(self, obj):
|
|
if student := self._get_authenticated_user():
|
|
if not self._is_participant(student, obj):
|
|
return None
|
|
return self._get_completed_lessons_count(student, obj)
|
|
return None
|
|
|
|
def _is_participant(self, student, course):
|
|
"""Helper method to check if a student is a participant in the given course."""
|
|
return Participant.objects.filter(student=student, course=course).exists()
|
|
|
|
def _get_authenticated_user(self):
|
|
"""Helper method to retrieve the authenticated user from the context."""
|
|
request = self.context.get('request')
|
|
return request.user if request and request.user.is_authenticated else None
|
|
|
|
def _get_completed_lessons_count(self, student, course):
|
|
"""Helper method to count completed lessons for the student in the given course."""
|
|
return LessonCompletion.objects.filter(
|
|
student=student,
|
|
lesson__course=course
|
|
).count()
|
|
|
|
|
|
class AttachmentSerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Attachment
|
|
fields = ['id', 'title', 'file', 'file_size']
|
|
|
|
|
|
class GlossarySerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = Glossary
|
|
fields = ['id', 'title', 'description']
|