Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add language support in courses #3335

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cms/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ def filter_and_sort_catalog_pages(
page.product.current_price,
page.title,
)
default_sorting_key = lambda page: (page_run_dates[page], page.title) # noqa: E731
default_sorting_key = lambda page: ( # noqa: E731
page.language.priority,
page_run_dates[page],
page.title,
)

# Best Match and Start Date sorting has same logic
sorting_key_map = defaultdict(
Expand Down
40 changes: 37 additions & 3 deletions cms/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ def test_filter_and_sort_catalog_pages_with_default_sorting(sort_by):
initial_program_pages = [
run.course.program.page for run in [second_program_run, first_program_run]
]

all_pages, program_pages, course_pages = filter_and_sort_catalog_pages(
initial_program_pages,
initial_course_pages,
Expand Down Expand Up @@ -114,13 +113,13 @@ def test_filter_and_sort_catalog_pages_with_default_sorting(sort_by):
assert past_run.course not in (
None if page.is_external_course_page else page.course for page in course_pages
)

# Pages should be sorted by next run date
# Pages should be sorted by language and then next run date (When language priority is the same)
assert [page.program for page in program_pages] == [
first_program_run.course.program,
second_program_run.course.program,
later_external_program_page.program,
]

expected_course_run_sort = [
non_program_run,
first_program_run,
Expand All @@ -135,6 +134,41 @@ def test_filter_and_sort_catalog_pages_with_default_sorting(sort_by):
run.course for run in expected_course_run_sort
]

# Pages should be sorted by language then next run date (When language priority is the different)
first_program_run.course.program.page.language.priority = 2
first_program_run.course.program.page.save()
second_program_run.course.program.page.language.priority = 1
second_program_run.course.program.page.save()
later_external_program_page.language.priority = 3
later_external_program_page.save()

all_pages, program_pages, course_pages = filter_and_sort_catalog_pages(
initial_program_pages,
initial_course_pages,
external_course_pages,
external_program_pages,
sort_by,
)

assert [page.program for page in program_pages] == [
second_program_run.course.program,
first_program_run.course.program,
later_external_program_page.program,
]

expected_course_run_sort = [
non_program_run,
first_program_run,
second_program_run,
later_external_course_page,
future_enrollment_end_run,
earlier_external_course_page,
]
# The sort should also include external course pages as expected
assert [page.course for page in course_pages] == [
run.course for run in expected_course_run_sort
]


@pytest.mark.parametrize(
(
Expand Down
11 changes: 10 additions & 1 deletion cms/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@
WebinarPage,
WhoShouldEnrollPage,
)
from courses.factories import CourseFactory, PlatformFactory, ProgramFactory
from courses.factories import (
CourseFactory,
CourseLanguageFactory,
PlatformFactory,
ProgramFactory,
)

factory.Faker.add_provider(internet)

Expand All @@ -78,6 +83,7 @@ class ProgramPageFactory(wagtail_factories.PageFactory):
subhead = factory.fuzzy.FuzzyText(prefix="Subhead ")
thumbnail_image = factory.SubFactory(wagtail_factories.ImageFactory)
background_image = factory.SubFactory(wagtail_factories.ImageFactory)
language = factory.SubFactory(CourseLanguageFactory)
parent = factory.SubFactory(wagtail_factories.PageFactory)
certificate_page = factory.RelatedFactory(
"cms.factories.CertificatePageFactory", "parent"
Expand Down Expand Up @@ -108,6 +114,7 @@ class CoursePageFactory(wagtail_factories.PageFactory):
subhead = factory.fuzzy.FuzzyText(prefix="Subhead ")
thumbnail_image = factory.SubFactory(wagtail_factories.ImageFactory)
background_image = factory.SubFactory(wagtail_factories.ImageFactory)
language = factory.SubFactory(CourseLanguageFactory)
parent = factory.SubFactory(wagtail_factories.PageFactory)
certificate_page = factory.RelatedFactory(
"cms.factories.CertificatePageFactory", "parent"
Expand Down Expand Up @@ -141,6 +148,7 @@ class ExternalCoursePageFactory(wagtail_factories.PageFactory):
subhead = factory.fuzzy.FuzzyText(prefix="Subhead ")
thumbnail_image = factory.SubFactory(wagtail_factories.ImageFactory)
background_image = factory.SubFactory(wagtail_factories.ImageFactory)
language = factory.SubFactory(CourseLanguageFactory)
parent = factory.SubFactory(wagtail_factories.PageFactory)

class Meta:
Expand Down Expand Up @@ -170,6 +178,7 @@ class ExternalProgramPageFactory(wagtail_factories.PageFactory):
subhead = factory.fuzzy.FuzzyText(prefix="Subhead ")
thumbnail_image = factory.SubFactory(wagtail_factories.ImageFactory)
background_image = factory.SubFactory(wagtail_factories.ImageFactory)
language = factory.SubFactory(CourseLanguageFactory)
parent = factory.SubFactory(wagtail_factories.PageFactory)

class Meta:
Expand Down
81 changes: 81 additions & 0 deletions cms/migrations/0078_add_courseware_page_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Generated by Django 4.2.17 on 2025-01-03 08:36

import django.db.models.deletion
from django.db import migrations, models


def populate_course_language(apps, schema_editor):
"""Prepopulate the course language for the course pages"""

CoursePage = apps.get_model("cms.CoursePage")
ExternalCoursePage = apps.get_model("cms.ExternalCoursePage")
ProgramPage = apps.get_model("cms.ProgramPage")
ExternalProgramPage = apps.get_model("cms.ExternalProgramPage")

CourseLanguage = apps.get_model("courses.CourseLanguage")
# English is the default language for all the courses
course_language_english, _ = CourseLanguage.objects.get_or_create(
name="English", priority=1
)

CoursePage.objects.update(language=course_language_english)
ExternalCoursePage.objects.update(language=course_language_english)
ProgramPage.objects.update(language=course_language_english)
ExternalProgramPage.objects.update(language=course_language_english)


class Migration(migrations.Migration):
dependencies = [
("courses", "0042_add_course_language"),
("cms", "0077_alter_certificatepage_ceus"),
]

operations = [
migrations.AddField(
model_name="coursepage",
name="language",
field=models.ForeignKey(
blank=True,
help_text="The course/program language for this page",
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="courses.courselanguage",
),
),
migrations.AddField(
model_name="externalcoursepage",
name="language",
field=models.ForeignKey(
blank=True,
help_text="The course/program language for this page",
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="courses.courselanguage",
),
),
migrations.AddField(
model_name="externalprogrampage",
name="language",
field=models.ForeignKey(
blank=True,
help_text="The course/program language for this page",
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="courses.courselanguage",
),
),
migrations.AddField(
model_name="programpage",
name="language",
field=models.ForeignKey(
blank=True,
help_text="The course/program language for this page",
null=True,
on_delete=django.db.models.deletion.PROTECT,
to="courses.courselanguage",
),
),
migrations.RunPython(
populate_course_language, reverse_code=migrations.RunPython.noop
),
]
53 changes: 53 additions & 0 deletions cms/migrations/0079_make_language_non_nullable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 4.2.17 on 2025-01-03 08:39

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("cms", "0078_add_courseware_page_language"),
]

operations = [
migrations.AlterField(
model_name="coursepage",
name="language",
field=models.ForeignKey(
help_text="The course/program language for this page",
on_delete=django.db.models.deletion.PROTECT,
to="courses.courselanguage",
),
),
migrations.AlterField(
model_name="externalcoursepage",
name="language",
field=models.ForeignKey(
help_text="The course/program language for this page",
on_delete=django.db.models.deletion.PROTECT,
to="courses.courselanguage",
),
),
migrations.AlterField(
model_name="externalprogrampage",
name="language",
field=models.ForeignKey(
default="",
help_text="The course/program language for this page",
on_delete=django.db.models.deletion.PROTECT,
to="courses.courselanguage",
),
preserve_default=False,
),
migrations.AlterField(
model_name="programpage",
name="language",
field=models.ForeignKey(
default="",
help_text="The course/program language for this page",
on_delete=django.db.models.deletion.PROTECT,
to="courses.courselanguage",
),
preserve_default=False,
),
]
23 changes: 19 additions & 4 deletions cms/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def get_context(self, request, *args, **kwargs): # noqa: ARG002
ProgramPage.objects.live()
.filter(program__live=True)
.order_by("id")
.select_related("program")
.select_related("program", "language")
.prefetch_related(
Prefetch(
"program__courses",
Expand All @@ -538,16 +538,22 @@ def get_context(self, request, *args, **kwargs): # noqa: ARG002
),
)
)
external_program_qset = ExternalProgramPage.objects.live().order_by("title")
external_program_qset = (
ExternalProgramPage.objects.live()
.select_related("program", "language")
.order_by("title")
)

course_page_qset = (
CoursePage.objects.live()
.filter(course__live=True)
.order_by("id")
.select_related("course")
.select_related("course", "language")
)
external_course_qset = (
ExternalCoursePage.objects.live().select_related("course").order_by("title")
ExternalCoursePage.objects.live()
.select_related("course", "language")
.order_by("title")
)

if topic_filter != ALL_TOPICS:
Expand Down Expand Up @@ -922,6 +928,14 @@ class ProductPage(MetadataPageMixin, WagtailCachedPageMixin, Page):
class Meta:
abstract = True

language = models.ForeignKey(
"courses.CourseLanguage",
null=False,
blank=False,
Comment on lines +933 to +934
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: null and blank default to False, can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These flags were added and then changed in the same PR and this thing has 2 migrations so I intentionally kept it like this to keep readability

on_delete=models.PROTECT,
help_text="The course/program language for this page",
)

description = RichTextField(
blank=True, help_text="The description shown on the product page"
)
Expand Down Expand Up @@ -1030,6 +1044,7 @@ class Meta:
use_json_field=True,
)
content_panels = Page.content_panels + [ # noqa: RUF005
FieldPanel("language"),
FieldPanel("external_marketing_url"),
FieldPanel("marketing_hubspot_form_id"),
FieldPanel("subhead"),
Expand Down
4 changes: 3 additions & 1 deletion cms/models_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
from cms.wagtail_hooks import create_product_and_versions_for_courseware_pages
from courses.factories import (
CourseFactory,
CourseLanguageFactory,
CourseRunCertificateFactory,
CourseRunFactory,
ProgramCertificateFactory,
Expand Down Expand Up @@ -2173,7 +2174,7 @@ def _create_external_course_page(superuser_client, course_id, slug):
Asserts:
Response status code is 302 (successful redirection).
"""

language = CourseLanguageFactory.create()
post_data = {
"course": course_id,
"title": "Icon Grid #6064",
Expand All @@ -2182,6 +2183,7 @@ def _create_external_course_page(superuser_client, course_id, slug):
"content-count": 0,
"slug": slug,
"action-publish": "action-publish",
"language": language.id,
}
response = superuser_client.post(
reverse(
Expand Down
10 changes: 10 additions & 0 deletions courses/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from .models import (
Course,
CourseLanguage,
CourseRun,
CourseRunCertificate,
CourseRunEnrollment,
Expand Down Expand Up @@ -422,3 +423,12 @@ class PlatformAdmin(TimestampedModelAdmin):
model = Platform
list_display = ["id", "name", "created_on", "updated_on"]
search_fields = ["name"]


@admin.register(CourseLanguage)
class CourseLanguageAdmin(admin.ModelAdmin):
"""Admin for CourseLanguage"""

model = CourseLanguage
list_display = ["id", "name", "priority"]
search_fields = ["name"]
10 changes: 10 additions & 0 deletions courses/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from .models import (
Course,
CourseLanguage,
CourseRun,
CourseRunCertificate,
CourseRunEnrollment,
Expand All @@ -38,6 +39,15 @@ class Meta:
model = Company


class CourseLanguageFactory(DjangoModelFactory):
"""Factory for Course Language"""

name = factory.Sequence(lambda n: f"Language_{n}")

class Meta:
model = CourseLanguage


class PlatformFactory(DjangoModelFactory):
"""Factory for Platform"""

Expand Down
Loading
Loading