Skip to content

Commit

Permalink
Filtering only enrollable runs on the product page (#2332)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
annagav and pre-commit-ci[bot] authored Aug 9, 2024
1 parent 2975891 commit 04b3906
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 195 deletions.
14 changes: 11 additions & 3 deletions courses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,19 +569,27 @@ 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
8 changes: 4 additions & 4 deletions frontend/public/src/components/CourseInfoBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Modal, ModalBody, ModalHeader } from "reactstrap"
type CourseInfoBoxProps = {
courses: Array<BaseCourseRun>,
currentUser: CurrentUser,
enrollableCourseRuns: Array<EnrollmentFlaggedCourseRun>,
setCurrentCourseRun: (run: EnrollmentFlaggedCourseRun) => Promise<any>
}

Expand Down Expand Up @@ -154,7 +155,7 @@ export default class CourseInfoBox extends React.PureComponent<CourseInfoBoxProp
}

render() {
const { courses } = this.props
const { courses, enrollableCourseRuns } = this.props

if (!courses || courses.length < 1) {
return null
Expand All @@ -166,10 +167,9 @@ export default class CourseInfoBox extends React.PureComponent<CourseInfoBoxProp
const isArchived = isRunArchived(run)

const startDates = []
const moreEnrollableCourseRuns =
course.courseruns && course.courseruns.length > 1
const moreEnrollableCourseRuns = enrollableCourseRuns.length > 1
if (moreEnrollableCourseRuns) {
course.courseruns.forEach((courseRun, index) => {
enrollableCourseRuns.forEach((courseRun, index) => {
if (courseRun.id !== run.id) {
startDates.push(
<li key={index}>{getCourseDates(courseRun, isArchived, true)}</li>
Expand Down
Loading

0 comments on commit 04b3906

Please sign in to comment.