Skip to content

Commit

Permalink
Added get_dated_courseruns and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jenniw committed Aug 21, 2024
1 parent 572bc9f commit 9c955e1
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
12 changes: 5 additions & 7 deletions courses/serializers/v2/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
ProductRelatedField,
)
from courses.serializers.v1.departments import DepartmentSerializer
from courses.utils import get_archived_courseruns
from courses.utils import get_dated_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 Down Expand Up @@ -99,12 +99,10 @@ def get_certificate_type(self, instance):

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"
dated_courseruns = get_dated_courseruns(instance.courseruns)
if dated_courseruns.count() == 0:
return "anytime"
return "dated"

class Meta:
model = models.Course
Expand Down
18 changes: 18 additions & 0 deletions courses/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,21 @@ def get_archived_courseruns(queryset):
& Q(end_date__lt=now)
& (Q(enrollment_end__isnull=True) | Q(enrollment_end__gt=now))
)


def get_dated_courseruns(queryset):
"""
Returns course runs that are dated meaning they are
- Not self-paced
- Have a start date
- End date can be dated and greater than now or null
- Enrollable (enrollment start is in the past and enrollment end is in the future or null)
"""
now = now_in_utc()
return queryset.filter(
Q(is_self_paced=False)
& Q(start_date__isnull=False)
& (Q(end_date__isnull=True) | Q(end_date__gt=now))
& (Q(enrollment_end__isnull=True) | Q(enrollment_end__gt=now))
& Q(enrollment_start__lt=now)
)
41 changes: 40 additions & 1 deletion courses/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
ProgramFactory, # noqa: F401
program_with_requirements, # noqa: F401
)
from courses.models import Course
from courses.models import Course, CourseRun
from courses.utils import (
get_dated_courseruns,
get_enrollable_courseruns_qs,
get_enrollable_courses,
get_program_certificate_by_enrollment,
Expand Down Expand Up @@ -266,3 +267,41 @@ def test_get_unenrollable_courses():
unenrollable_courses = get_unenrollable_courses(Course.objects.all())
assert unenrollable_course in unenrollable_courses
assert enrollable_course not in unenrollable_courses


def test_get_dated_courseruns():
"""
Test get_dated_courseruns
"""
now = now_in_utc()
future_date = now + timedelta(days=1)
past_date = now - timedelta(days=1)
enrollable_course = CourseFactory.create()
unenrollable_course = CourseFactory.create()
self_paced_course = CourseFactory.create()
enrollable_courserun = CourseRunFactory.create(
course=enrollable_course,
live=True,
start_date=now,
enrollment_start=past_date,
enrollment_end=future_date,
)
unenrollable_courserun = CourseRunFactory.create(
course=unenrollable_course,
live=True,
start_date=future_date,
enrollment_start=future_date,
enrollment_end=None,
)
self_paced_courserun = CourseRunFactory.create(
course=self_paced_course,
live=True,
self_paced=True,
start_date=now,
enrollment_start=past_date,
enrollment_end=future_date,
)
dated_courseruns = get_dated_courseruns(CourseRun.objects.all())
assert enrollable_courserun in dated_courseruns
assert unenrollable_courserun not in dated_courseruns
assert self_paced_courserun not in dated_courseruns

0 comments on commit 9c955e1

Please sign in to comment.