Commit eb31966f authored by Matheus Miranda's avatar Matheus Miranda

Add enroll field to course serializer

parent 8b883238
......@@ -8,6 +8,7 @@ from .models import (
ProfessorMessage,
ProfessorMessageRead,
CourseTrack,
CourseStudent
)
......@@ -33,15 +34,18 @@ class CourseSerializer(serializers.ModelSerializer):
is_user_coordinator = serializers.SerializerMethodField()
is_assistant_or_coordinator = serializers.SerializerMethodField()
track = serializers.SerializerMethodField()
is_user_enrolled = serializers.SerializerMethodField()
enroll_course_url = serializers.SerializerMethodField()
class Meta:
model = Course
fields = ("id", "slug", "name", "intro_video", "application", "requirement",
"abstract", "structure", "workload", "status", "is_public",
"thumbnail_url", "home_thumbnail_url", "home_position",
"start_date", "home_published", "authors_names", "has_started",
"min_percent_to_complete", "is_user_assistant", "is_user_coordinator",
"is_assistant_or_coordinator", 'professors', 'track', 'forum_id')
fields = ('id', 'slug', 'name', 'intro_video', 'application', 'requirement',
'abstract', 'structure', 'workload', 'status', 'is_public',
'thumbnail_url', 'home_thumbnail_url', 'home_position',
'start_date', 'home_published', 'authors_names', 'has_started',
'min_percent_to_complete', 'is_user_assistant', 'is_user_coordinator',
'is_assistant_or_coordinator', 'professors', 'track', 'forum_id',
'is_user_enrolled', 'enroll_course_url')
@staticmethod
def get_home_thumbnail_url(obj):
......@@ -58,6 +62,31 @@ class CourseSerializer(serializers.ModelSerializer):
def get_is_assistant_or_coordinator(self, obj):
return obj.is_assistant_or_coordinator(self.context['request'].user)
def get_is_user_enrolled(self, obj):
user = None
request = self.context.get("request")
if request and hasattr(request, "user"):
user = request.user
if user:
return CourseStudent.objects.filter(course=obj, user=user).exists()
return False
def get_enroll_course_url(self, obj):
course = obj
if course.is_enrolled(self.request.user):
return 'resume_course'
if course.status == 'draft':
return 'courses'
if self.request.user.accepted_terms or not settings.TERMS_ACCEPTANCE_REQUIRED:
course.enroll_student(self.request.user)
if course.has_started and course.first_lesson():
return 'lesson'
else:
return 'course_home'
else:
'accept_terms'
def get_track(self, obj):
course_track = CourseTrack.objects.filter(courses=obj)
if course_track:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment