From 2c569c287b74d9baca519bd9abb1fb0f2cd6a5dc Mon Sep 17 00:00:00 2001 From: Hamza Shafique Date: Mon, 16 Sep 2024 19:03:11 +0500 Subject: [PATCH] chore: get spanish translations from active subjects --- .../management/commands/populate_product_catalog.py | 4 ++-- .../commands/tests/test_populate_product_catalog.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/course_discovery/apps/course_metadata/management/commands/populate_product_catalog.py b/course_discovery/apps/course_metadata/management/commands/populate_product_catalog.py index 3f6971d0f7..340b9d5527 100644 --- a/course_discovery/apps/course_metadata/management/commands/populate_product_catalog.py +++ b/course_discovery/apps/course_metadata/management/commands/populate_product_catalog.py @@ -116,7 +116,7 @@ def get_products(self, product_type, product_source): ).filter(Q(num_orgs__gt=0) & Q(card_image__isnull=False) & ~Q(card_image='')) subject_translations = Prefetch( - 'courses__subjects__translations', + 'active_subjects__translations', queryset=SubjectTranslation.objects.filter(language_code='es'), to_attr='spanish_translations' ) @@ -167,7 +167,7 @@ def get_transformed_data(self, product, product_type): data.update({ "Subjects": ", ".join(subject.name for subject in product.active_subjects), "Subjects Spanish": ", ".join( - translation.name for subject in product.subjects + translation.name for subject in product.active_subjects for translation in subject.spanish_translations ), "Languages": ", ".join(language.code for language in product.active_languages), diff --git a/course_discovery/apps/course_metadata/management/commands/tests/test_populate_product_catalog.py b/course_discovery/apps/course_metadata/management/commands/tests/test_populate_product_catalog.py index ea6a798c42..88e359f885 100644 --- a/course_discovery/apps/course_metadata/management/commands/tests/test_populate_product_catalog.py +++ b/course_discovery/apps/course_metadata/management/commands/tests/test_populate_product_catalog.py @@ -7,11 +7,12 @@ import factory import mock from django.core.management import CommandError, call_command +from django.db.models import Prefetch, prefetch_related_objects from django.test import TestCase from course_discovery.apps.course_metadata.choices import CourseRunStatus, ProgramStatus from course_discovery.apps.course_metadata.management.commands.populate_product_catalog import Command -from course_discovery.apps.course_metadata.models import Course, CourseType, ProgramType +from course_discovery.apps.course_metadata.models import Course, CourseType, ProgramType, SubjectTranslation from course_discovery.apps.course_metadata.tests.factories import ( CourseFactory, CourseRunFactory, CourseTypeFactory, DegreeFactory, OrganizationFactory, PartnerFactory, ProgramTypeFactory, SeatFactory, SourceFactory, SubjectFactory @@ -406,6 +407,13 @@ def test_get_transformed_data_for_degree(self): product = self.degrees[0] command = Command() product_authoring_orgs = product.authoring_organizations.all() + subject_translations = Prefetch( + "active_subjects__translations", + queryset=SubjectTranslation.objects.filter(language_code="es"), + to_attr="spanish_translations", + ) + prefetch_related_objects([product], subject_translations) + transformed_prod_data = command.get_transformed_data(product, "degree") assert transformed_prod_data == { "UUID": str(product.uuid.hex), @@ -418,7 +426,7 @@ def test_get_transformed_data_for_degree(self): "Languages": ", ".join(language.code for language in product.active_languages), "Subjects": ", ".join(subject.name for subject in product.active_subjects), "Subjects Spanish": ", ".join( - translation.name for subject in product.subjects + translation.name for subject in product.active_subjects for translation in subject.spanish_translations ), "Marketing URL": product.marketing_url,