Skip to content

Commit

Permalink
fix: catalog external courses page visibility conditions (#3082)
Browse files Browse the repository at this point in the history
* fix: catalog external courses page visibility conditions
  • Loading branch information
arslanashraf7 authored Aug 5, 2024
1 parent 236d20e commit a5ee481
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
22 changes: 8 additions & 14 deletions cms/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,27 @@ def filter_and_sort_catalog_pages(
tuple of (list of Pages): A tuple containing a list of combined ProgramPages, CoursePages, ExternalCoursePages and ExternalProgramPages, a list of
ProgramPages and ExternalProgramPages, and a list of CoursePages and ExternalCoursePages, all sorted by the next course/program run date and title
"""
all_program_pages = program_pages + external_program_pages
all_course_pages = course_pages + external_course_pages

valid_program_pages = [
page for page in program_pages if page.product.is_catalog_visible
page for page in all_program_pages if page.product.is_catalog_visible
]
valid_course_pages = [
page for page in course_pages if page.product.is_catalog_visible
page for page in all_course_pages if page.product.is_catalog_visible
]

valid_external_course_pages = list(external_course_pages)

valid_external_program_pages = list(external_program_pages)

page_run_dates = {
page: page.product.next_run_date
or datetime(year=MAXYEAR, month=1, day=1, tzinfo=timezone.utc)
for page in itertools.chain(
valid_program_pages,
valid_course_pages,
valid_external_course_pages,
valid_external_program_pages,
)
}
return (
sorted(
valid_program_pages
+ valid_external_program_pages
+ valid_course_pages
+ valid_external_course_pages,
valid_program_pages + valid_course_pages,
# ProgramPages with the same next run date as a CoursePage should be sorted first
key=lambda page: (
page_run_dates[page],
Expand All @@ -67,11 +61,11 @@ def filter_and_sort_catalog_pages(
),
),
sorted(
valid_program_pages + valid_external_program_pages,
valid_program_pages,
key=lambda page: (page_run_dates[page], page.title),
),
sorted(
valid_course_pages + valid_external_course_pages,
valid_course_pages,
key=lambda page: (page_run_dates[page], page.title),
),
)
Expand Down
13 changes: 4 additions & 9 deletions cms/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from cms.api import filter_and_sort_catalog_pages
from cms.factories import ExternalCoursePageFactory, ExternalProgramPageFactory
from cms.models import ExternalCoursePage
from courses.factories import CourseRunFactory, ProgramRunFactory
from mitxpro.utils import now_in_utc

Expand Down Expand Up @@ -84,14 +83,14 @@ def test_filter_and_sort_catalog_pages():
+ len(initial_course_pages)
+ len(external_course_pages)
+ len(external_program_pages)
- 1
- 2
)
assert len(course_pages) == (
len(initial_course_pages) + len(external_course_pages) - 1
)

assert len(program_pages) == (
len(initial_program_pages) + len(external_program_pages)
len(initial_program_pages) + len(external_program_pages) - 1
)

# Filtered out external course page because it does not have a `course` attribute
Expand All @@ -104,7 +103,6 @@ def test_filter_and_sort_catalog_pages():
first_program_run.course.program,
second_program_run.course.program,
later_external_program_page.program,
earlier_external_program_page.program,
]
expected_course_run_sort = [
non_program_run,
Expand All @@ -116,9 +114,6 @@ def test_filter_and_sort_catalog_pages():
]

# The sort should also include external course pages as expected
assert [
page if page.is_external_course_page else page.course for page in course_pages
] == [
run if isinstance(run, ExternalCoursePage) else run.course
for run in expected_course_run_sort
assert [page.course for page in course_pages] == [
run.course for run in expected_course_run_sort
]

0 comments on commit a5ee481

Please sign in to comment.