-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds v2 of the courses API for the program page (#1983)
- Loading branch information
Showing
6 changed files
with
396 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
"""Courses v2 serializers""" | ||
|
||
from rest_framework import serializers | ||
from rest_framework.exceptions import ValidationError | ||
|
||
import logging | ||
|
||
from cms.serializers import BaseCoursePageSerializer | ||
from courses import models | ||
from courses.api import create_run_enrollments | ||
from courses.serializers.v1.base import ( | ||
BaseCourseSerializer, | ||
BaseCourseRunEnrollmentSerializer, | ||
BaseCourseRunSerializer, | ||
ProductRelatedField, | ||
) | ||
from courses.serializers.v1.departments import DepartmentSerializer | ||
from flexiblepricing.api import is_courseware_flexible_price_approved | ||
from main import features | ||
from openedx.constants import EDX_ENROLLMENT_AUDIT_MODE, EDX_ENROLLMENT_VERIFIED_MODE | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
class CourseSerializer(BaseCourseSerializer): | ||
"""Course model serializer""" | ||
|
||
departments = DepartmentSerializer(many=True, read_only=True) | ||
next_run_id = serializers.SerializerMethodField() | ||
page = BaseCoursePageSerializer(read_only=True) | ||
programs = serializers.SerializerMethodField() | ||
|
||
def get_next_run_id(self, instance): | ||
"""Get next run id""" | ||
run = instance.first_unexpired_run | ||
return run.id if run is not None else None | ||
|
||
def get_programs(self, instance): | ||
if self.context.get("all_runs", False): | ||
from courses.serializers.v1.base import BaseProgramSerializer | ||
|
||
return BaseProgramSerializer(instance.programs, many=True).data | ||
|
||
return None | ||
|
||
class Meta: | ||
model = models.Course | ||
fields = [ | ||
"id", | ||
"title", | ||
"readable_id", | ||
"next_run_id", | ||
"departments", | ||
"page", | ||
"programs", | ||
] | ||
|
||
|
||
class CourseRunSerializer(BaseCourseRunSerializer): | ||
"""CourseRun model serializer""" | ||
|
||
products = ProductRelatedField(many=True, read_only=True) | ||
approved_flexible_price_exists = serializers.SerializerMethodField() | ||
|
||
class Meta: | ||
model = models.CourseRun | ||
fields = BaseCourseRunSerializer.Meta.fields + [ | ||
"products", | ||
"approved_flexible_price_exists", | ||
] | ||
|
||
def to_representation(self, instance): | ||
data = super().to_representation(instance) | ||
if self.context and self.context.get("include_enrolled_flag"): | ||
return { | ||
**data, | ||
**{ | ||
"is_enrolled": getattr(instance, "user_enrollments", 0) > 0, | ||
"is_verified": getattr(instance, "verified_enrollments", 0) > 0, | ||
}, | ||
} | ||
return data | ||
|
||
def get_approved_flexible_price_exists(self, instance): | ||
if not self.context or not self.context.get("include_approved_financial_aid"): | ||
return False | ||
|
||
# Get the User object if it exists. | ||
user = self.context["request"].user if "request" in self.context else None | ||
|
||
# Check for an approved flexible price record if the | ||
# user exists and has an ID (not an Anonymous user). | ||
# Otherwise return False. | ||
flexible_price_exists = ( | ||
is_courseware_flexible_price_approved( | ||
instance.course, self.context["request"].user | ||
) | ||
if user and user.id | ||
else False | ||
) | ||
return flexible_price_exists | ||
|
||
|
||
class CourseWithCourseRunsSerializer(CourseSerializer): | ||
"""Course model serializer - also serializes child course runs""" | ||
|
||
courseruns = serializers.SerializerMethodField(read_only=True) | ||
|
||
def get_courseruns(self, instance): | ||
context = { | ||
"include_approved_financial_aid": self.context.get( | ||
"include_approved_financial_aid", False | ||
) | ||
} | ||
|
||
return CourseRunSerializer( | ||
instance.courseruns.all(), many=True, read_only=True, context=context | ||
).data | ||
|
||
class Meta: | ||
model = models.Course | ||
fields = CourseSerializer.Meta.fields + [ | ||
"courseruns", | ||
] | ||
|
||
|
||
class CourseRunWithCourseSerializer(CourseRunSerializer): | ||
""" | ||
CourseRun model serializer - also serializes the parent Course. | ||
""" | ||
|
||
course = CourseSerializer(read_only=True, context={"include_page_fields": True}) | ||
|
||
class Meta: | ||
model = models.CourseRun | ||
fields = CourseRunSerializer.Meta.fields + [ | ||
"course", | ||
] | ||
|
||
|
||
class CourseRunEnrollmentSerializer(BaseCourseRunEnrollmentSerializer): | ||
"""CourseRunEnrollment model serializer""" | ||
|
||
run = CourseRunWithCourseSerializer(read_only=True) | ||
run_id = serializers.IntegerField(write_only=True) | ||
certificate = serializers.SerializerMethodField(read_only=True) | ||
enrollment_mode = serializers.ChoiceField( | ||
(EDX_ENROLLMENT_AUDIT_MODE, EDX_ENROLLMENT_VERIFIED_MODE), read_only=True | ||
) | ||
approved_flexible_price_exists = serializers.SerializerMethodField() | ||
grades = serializers.SerializerMethodField(read_only=True) | ||
|
||
def create(self, validated_data): | ||
user = self.context["user"] | ||
run_id = validated_data["run_id"] | ||
try: | ||
run = models.CourseRun.objects.get(id=run_id) | ||
except models.CourseRun.DoesNotExist: | ||
raise ValidationError({"run_id": f"Invalid course run id: {run_id}"}) | ||
successful_enrollments, edx_request_success = create_run_enrollments( | ||
user, | ||
[run], | ||
keep_failed_enrollments=features.is_enabled(features.IGNORE_EDX_FAILURES), | ||
) | ||
return successful_enrollments | ||
|
||
class Meta(BaseCourseRunEnrollmentSerializer.Meta): | ||
fields = BaseCourseRunEnrollmentSerializer.Meta.fields + [ | ||
"run_id", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.