Commit 6e6f8741 by Renzo Lucioni

Support program language faceting

Indexes program languages and includes the new field in search responses.

LEARNER-340
parent 01b6ba90
......@@ -64,7 +64,7 @@ PROGRAM_FACET_FIELD_OPTIONS = {
BASE_PROGRAM_FIELDS = (
'text', 'uuid', 'title', 'subtitle', 'type', 'marketing_url', 'content_type', 'status', 'card_image_url',
'published', 'partner',
'published', 'partner', 'language',
)
PROGRAM_SEARCH_FIELDS = BASE_PROGRAM_FIELDS + (
......
# pylint: disable=no-member, test-inherits-tests
import datetime
import itertools
from urllib.parse import urlencode
import ddt
......@@ -1221,13 +1222,18 @@ class ProgramSearchSerializerTests(TestCase):
'published': program.status == ProgramStatus.Active,
'partner': program.partner.short_code,
'authoring_organization_uuids': get_uuids(program.authoring_organizations.all()),
'subject_uuids': get_uuids([course.subjects for course in program.courses.all()]),
'staff_uuids': get_uuids([course.staff for course in list(program.course_runs)]),
'subject_uuids': get_uuids(
itertools.chain.from_iterable(course.subjects.all() for course in program.courses.all())
),
'staff_uuids': get_uuids(
itertools.chain.from_iterable(course.staff.all() for course in list(program.course_runs))
),
'aggregation_key': 'program:{}'.format(program.uuid),
'weeks_to_complete_min': program.weeks_to_complete_min,
'weeks_to_complete_max': program.weeks_to_complete_max,
'min_hours_effort_per_week': program.min_hours_effort_per_week,
'max_hours_effort_per_week': program.max_hours_effort_per_week,
'language': [serialize_language(language) for language in program.languages],
}
def test_data(self):
......@@ -1254,6 +1260,29 @@ class ProgramSearchSerializerTests(TestCase):
expected = self._create_expected_data(program)
assert serializer.data == expected
def test_data_with_languages(self):
"""
Verify that program languages are serialized.
"""
course_run = CourseRunFactory(
language=LanguageTag.objects.get(code='en-us'),
authoring_organizations=[OrganizationFactory()]
)
CourseRunFactory(
course=course_run.course,
language=LanguageTag.objects.get(code='zh-cmn')
)
program = ProgramFactory(courses=[course_run.course])
result = SearchQuerySet().models(Program).filter(uuid=program.uuid)[0]
serializer = ProgramSearchSerializer(result)
expected = self._create_expected_data(program)
assert serializer.data == expected
assert {'English', 'Chinese - Mandarin'} == {*expected['language']}
class TypeaheadCourseRunSearchSerializerTests(TestCase):
def test_data(self):
......
......@@ -68,6 +68,16 @@ class BaseIndex(indexes.SearchIndex):
def prepare_authoring_organization_uuids(self, obj):
return [str(organization.uuid) for organization in obj.authoring_organizations.all()]
def _prepare_language(self, language):
if language:
# ECOM-5466: Render the macro language for all languages except Chinese
if language.code.startswith('zh'):
return language.name
else:
return language.macrolanguage
return None
class BaseCourseIndex(OrganizationsMixin, BaseIndex):
key = indexes.CharField(model_attr='key', stored=True)
......@@ -176,16 +186,6 @@ class CourseRunIndex(BaseCourseIndex, indexes.Indexable):
def prepare_published(self, obj):
return obj.status == CourseRunStatus.Published
def _prepare_language(self, language):
if language:
# ECOM-5466: Render the macro language for all languages except Chinese
if language.code.startswith('zh'):
return language.name
else:
return language.macrolanguage
return None
def prepare_language(self, obj):
return self._prepare_language(obj.language)
......@@ -240,6 +240,7 @@ class ProgramIndex(BaseIndex, indexes.Indexable, OrganizationsMixin):
max_hours_effort_per_week = indexes.IntegerField(model_attr='max_hours_effort_per_week', null=True)
weeks_to_complete_min = indexes.IntegerField(model_attr='weeks_to_complete_min', null=True)
weeks_to_complete_max = indexes.IntegerField(model_attr='weeks_to_complete_max', null=True)
language = indexes.MultiValueField(faceted=True)
def prepare_aggregation_key(self, obj):
return 'program:{}'.format(obj.uuid)
......@@ -261,3 +262,6 @@ class ProgramIndex(BaseIndex, indexes.Indexable, OrganizationsMixin):
def prepare_marketing_url(self, obj):
return obj.marketing_url
def prepare_language(self, obj):
return [self._prepare_language(language) for language in obj.languages]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment