Skip to content

Commit

Permalink
feat: Add course language filter
Browse files Browse the repository at this point in the history
  • Loading branch information
arslanashraf7 committed Jan 27, 2025
1 parent b91d0a5 commit 6460e27
Show file tree
Hide file tree
Showing 13 changed files with 518 additions and 182 deletions.
6 changes: 5 additions & 1 deletion cms/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from wagtail.models import Page, Site

from cms import models as cms_models
from cms.constants import CERTIFICATE_INDEX_SLUG, ENTERPRISE_PAGE_SLUG, CatalogSorting
from cms.constants import (
CERTIFICATE_INDEX_SLUG,
ENTERPRISE_PAGE_SLUG,
CatalogSorting,
)

log = logging.getLogger(__name__)
DEFAULT_HOMEPAGE_PROPS = dict(title="Home Page", subhead="This is the home page") # noqa: C408
Expand Down
1 change: 1 addition & 0 deletions cms/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
COMMON_COURSEWARE_COMPONENT_INDEX_SLUG = "common-courseware-component-pages"

ALL_TOPICS = "All Topics"
ALL_LANGUAGES = "All Languages"
ALL_TAB = "all-tab"

# ************** CONSTANTS FOR WEBINARS **************
Expand Down
27 changes: 26 additions & 1 deletion cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
from cms.constants import (
ALL_TAB,
ALL_TOPICS,
ALL_LANGUAGES,
BLOG_INDEX_SLUG,
CERTIFICATE_INDEX_SLUG,
COMMON_COURSEWARE_COMPONENT_INDEX_SLUG,
Expand Down Expand Up @@ -90,8 +91,11 @@
Program,
ProgramCertificate,
ProgramRun,
CourseLanguage,
)
from ecommerce.models import Product
from mitol.olposthog.features import is_enabled
from mitxpro.features import CATALOG_LANGUAGE_FILTER
from mitxpro.utils import now_in_utc
from mitxpro.views import get_base_context

Expand Down Expand Up @@ -516,6 +520,9 @@ def get_context(self, request, *args, **kwargs): # noqa: ARG002
Populate the context with live programs, courses and programs + courses
"""
topic_filter = request.GET.get("topic", ALL_TOPICS)
language_filter = request.GET.get("language", ALL_LANGUAGES)

is_language_filter_enabled = is_enabled(CATALOG_LANGUAGE_FILTER, default=False)

# Best Match is the default sorting.
sort_by = request.GET.get("sort-by", CatalogSorting.BEST_MATCH.sorting_value)
Expand Down Expand Up @@ -556,10 +563,19 @@ def get_context(self, request, *args, **kwargs): # noqa: ARG002
.order_by("title")
)

if language_filter != ALL_LANGUAGES:
program_page_qset = program_page_qset.filter(language__name=language_filter)
external_program_qset = external_program_qset.filter(
language__name=language_filter
)
course_page_qset = course_page_qset.filter(language__name=language_filter)
external_course_qset = external_course_qset.filter(
language__name=language_filter
)

if topic_filter != ALL_TOPICS:
program_page_qset = program_page_qset.related_pages(topic_filter)
external_program_qset = external_program_qset.related_pages(topic_filter)

course_page_qset = course_page_qset.related_pages(topic_filter)
external_course_qset = external_course_qset.related_pages(topic_filter)

Expand Down Expand Up @@ -647,6 +663,15 @@ def get_context(self, request, *args, **kwargs): # noqa: ARG002
}
for sorting_option in CatalogSorting
],
show_language_filter=is_language_filter_enabled,
selected_language=language_filter,
language_options=[ALL_LANGUAGES]
+ [
course_language.name
for course_language in CourseLanguage.objects.filter(is_active=True)
]
if is_language_filter_enabled
else [],
)


Expand Down
75 changes: 75 additions & 0 deletions cms/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
UPCOMING_WEBINAR,
UPCOMING_WEBINAR_BUTTON_TITLE,
WEBINAR_HEADER_BANNER,
ALL_LANGUAGES,
)
from cms.factories import (
CatalogPageFactory,
CertificatePageFactory,
CommonComponentIndexPageFactory,
CompaniesLogoCarouselPageFactory,
Expand Down Expand Up @@ -88,6 +90,7 @@
CourseRunFactory,
ProgramCertificateFactory,
)
from courses.models import CourseLanguage
from ecommerce.factories import ProductFactory, ProductVersionFactory

pytestmark = [pytest.mark.django_db]
Expand Down Expand Up @@ -240,6 +243,78 @@ def test_webinar_detail_page_button_title():
assert upcoming_webinar.detail_page_button_title == UPCOMING_WEBINAR_BUTTON_TITLE


@pytest.mark.parametrize(
"languages, selected_language",
[
(None, ALL_LANGUAGES),
(
[
"Languag1",
"Language2",
],
"Language2",
),
(["Languag1", "Language2", "Language3"], "Languag1"),
],
)
def test_catalog_page_language_context(
mocker, staff_user, languages, selected_language
):
"""
Verify the language context is properly passed to the catalog_page.html
"""
mocker.patch("cms.models.is_enabled", return_value=True)
CourseLanguage.objects.all().delete()
catalog_page = CatalogPageFactory.create()
if languages:
CourseLanguageFactory.create_batch(
len(languages), name=factory.Iterator(languages)
)

rf = RequestFactory()
request = rf.get(f"/?language={selected_language}")
request.user = staff_user
context = catalog_page.get_context(request=request)

assert context.get("self") == catalog_page
assert context.get("page") == catalog_page
assert context.get("request") == request
assert context.get("selected_language") == selected_language
assert (
context.get("language_options") == [ALL_LANGUAGES] + languages
if languages
else ALL_LANGUAGES
)


@pytest.mark.parametrize(
"is_enabled",
[True, False],
)
def test_catalog_page_language_feature_flag(mocker, staff_user, is_enabled):
"""
Verify the language context is properly passed to the catalog_page.html
"""
mocker.patch("cms.models.is_enabled", return_value=is_enabled)
CourseLanguage.objects.all().delete()
catalog_page = CatalogPageFactory.create()
languages = CourseLanguageFactory.create_batch(2)

rf = RequestFactory()
request = rf.get("/")
request.user = staff_user
context = catalog_page.get_context(request=request)

assert context.get("selected_language") == ALL_LANGUAGES
assert context.get("show_language_filter") == is_enabled
expected_languages = (
[ALL_LANGUAGES] + [language.name for language in languages]
if is_enabled
else []
)
assert context.get("language_options") == expected_languages


def test_course_page_program_page():
"""
Verify `program_page` property from the course page returns expected value
Expand Down
Loading

0 comments on commit 6460e27

Please sign in to comment.