Skip to content

Commit

Permalink
Add availability to Courses API endpoint (#2308)
Browse files Browse the repository at this point in the history
Co-authored-by: Anna <annagav@mit.edu>
  • Loading branch information
JenniWhitman and annagav authored Aug 16, 2024
1 parent 8a6f7ac commit 5256acb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
12 changes: 12 additions & 0 deletions courses/serializers/v2/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
ProductRelatedField,
)
from courses.serializers.v1.departments import DepartmentSerializer
from courses.utils import get_archived_courseruns
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
Expand All @@ -32,6 +33,7 @@ class CourseSerializer(BaseCourseSerializer):
programs = serializers.SerializerMethodField()
topics = serializers.SerializerMethodField()
certificate_type = serializers.SerializerMethodField()
availability = serializers.SerializerMethodField()
required_prerequisites = serializers.SerializerMethodField()
duration = serializers.SerializerMethodField()
time_commitment = serializers.SerializerMethodField()
Expand Down Expand Up @@ -95,6 +97,15 @@ def get_certificate_type(self, instance):
return "MicroMasters Credential"
return "Certificate of Completion"

def get_availability(self, instance):
"""Get course availability"""
archived_course_runs = get_archived_courseruns(
instance.courseruns.filter(is_self_paced=False)
)
if archived_course_runs.count() == 0:
return "dated"
return "anytime"

class Meta:
model = models.Course
fields = [
Expand All @@ -110,6 +121,7 @@ class Meta:
"required_prerequisites",
"duration",
"time_commitment",
"availability",
]


Expand Down
2 changes: 2 additions & 0 deletions courses/serializers/v2/courses_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_serialize_course(
"departments": [{"name": department}],
"page": CoursePageSerializer(course.page).data,
"certificate_type": certificate_type,
"availability": "dated",
"topics": [{"name": topic.name} for topic in topics],
"required_prerequisites": True,
"duration": course.page.length,
Expand Down Expand Up @@ -105,6 +106,7 @@ def test_serialize_course_required_prerequisites(
"page": CoursePageSerializer(course.page).data,
"certificate_type": "Certificate of Completion",
"topics": [],
"availability": "dated",
"required_prerequisites": expected_required_prerequisites,
"duration": course.page.length,
"time_commitment": course.page.effort,
Expand Down
18 changes: 18 additions & 0 deletions courses/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,21 @@ def get_unenrollable_courses(queryset):
.filter(courseruns__id__in=courseruns_qs.values_list("id", flat=True))
.distinct()
)


def get_archived_courseruns(queryset):
"""
Returns course runs that are archived. This is defined as:
- The course run end date has passed
- The course run enrollment end date is in the future or None.
This logic is set to match the logic found in frontend/public/src/lib/courseApi.js isRunArchived
Args:
queryset: Queryset of CourseRun objects
"""
now = now_in_utc()
return queryset.filter(
get_enrollable_course_run_filter(now)
& Q(end_date__lt=now)
& (Q(enrollment_end__isnull=True) | Q(enrollment_end__gt=now))
)

0 comments on commit 5256acb

Please sign in to comment.