Skip to content

Commit

Permalink
non archived courses should be preferred
Browse files Browse the repository at this point in the history
  • Loading branch information
annagav committed Aug 8, 2024
1 parent e5b102f commit 0d14eb5
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions courses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,19 +569,25 @@ def active_products(self):
@cached_property
def first_unexpired_run(self):
"""
Gets the first unexpired CourseRun associated with this Course
Gets the first unexpired/enrollable CourseRun associated with this Course. Giving preference to
non-archived courses
Returns:
CourseRun or None: An unexpired course run
CourseRun or None: An unexpired/enrollable course run
# NOTE: This is implemented with sorted() and courseruns.all() to allow for prefetch_related
# optimization. You can get the desired course_run with a filter, but
# that would run a new query even if prefetch_related was used.
"""
course_runs = self.courseruns.all()
eligible_course_runs = [
course_run for course_run in course_runs if course_run.is_enrollable
course_run for course_run in course_runs if (course_run.is_enrollable and not course_run.is_past)
]
if len(eligible_course_runs) < 1:
# check for archived courses
eligible_course_runs = [
course_run for course_run in course_runs if course_run.is_enrollable
]
return first_matching_item(
sorted(eligible_course_runs, key=lambda course_run: course_run.start_date),
lambda course_run: True, # noqa: ARG005
Expand Down

0 comments on commit 0d14eb5

Please sign in to comment.