|
|
|
@ -211,10 +211,8 @@ class CourseDetailSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
|
|
|
|
def get_access(self, obj): |
|
|
|
if student := self._get_authenticated_user(): |
|
|
|
if not self._is_participant(student, obj): |
|
|
|
return False |
|
|
|
return True |
|
|
|
if user := self._get_authenticated_user(): |
|
|
|
return self._has_access(user, obj) |
|
|
|
return False |
|
|
|
|
|
|
|
def get_professor(self, obj): |
|
|
|
@ -248,9 +246,29 @@ class CourseDetailSerializer(serializers.ModelSerializer): |
|
|
|
return min(completed_count, total_lessons) |
|
|
|
return None |
|
|
|
|
|
|
|
def _has_access(self, user, course): |
|
|
|
""" |
|
|
|
Check if the user has access to the course content. |
|
|
|
Access is granted if: |
|
|
|
1. User is the professor of the course. |
|
|
|
2. User is staff (admin). |
|
|
|
3. User is an active participant in the course. |
|
|
|
""" |
|
|
|
if user.is_staff or user.is_superuser: |
|
|
|
return True |
|
|
|
|
|
|
|
if course.professor_id == user.id: |
|
|
|
return True |
|
|
|
|
|
|
|
return Participant.objects.filter( |
|
|
|
student_id=user.id, |
|
|
|
course=course, |
|
|
|
is_active=True |
|
|
|
).exists() |
|
|
|
|
|
|
|
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() |
|
|
|
"""Deprecated: use _has_access instead. Kept for backward compatibility if needed.""" |
|
|
|
return self._has_access(student, course) |
|
|
|
|
|
|
|
def _get_authenticated_user(self): |
|
|
|
"""Helper method to retrieve the authenticated user from the context.""" |
|
|
|
@ -349,11 +367,19 @@ class MyCourseListSerializer(serializers.ModelSerializer): |
|
|
|
|
|
|
|
def _is_participant(self, student, course): |
|
|
|
"""Helper method to check if a student is a participant in the given course.""" |
|
|
|
if student.is_staff or student.is_superuser: |
|
|
|
return True |
|
|
|
|
|
|
|
# اگر کاربر استاد دوره است، دسترسی کامل دارد |
|
|
|
if course.professor == student: |
|
|
|
if course.professor_id == student.id: |
|
|
|
return True |
|
|
|
|
|
|
|
# در غیر این صورت چک میکنیم که آیا participant است یا خیر |
|
|
|
return Participant.objects.filter(student=student, course=course).exists() |
|
|
|
return Participant.objects.filter( |
|
|
|
student_id=student.id, |
|
|
|
course=course, |
|
|
|
is_active=True |
|
|
|
).exists() |
|
|
|
|
|
|
|
def _get_authenticated_user(self): |
|
|
|
"""Helper method to retrieve the authenticated user from the context.""" |
|
|
|
|