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 769b6e9e8f..aa51d80035 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 @@ -105,7 +105,8 @@ def get_products(self, product_type, product_source): subject_translations ) elif product_type == 'degree': - queryset = Program.objects.marketable().exclude(degree__isnull=True).select_related('partner', 'type') + queryset = Program.objects.marketable().exclude(degree__isnull=True) \ + .select_related('partner', 'type', 'primary_subject_override', 'language_override') if product_source: queryset = queryset.filter(product_source__slug=product_source) @@ -164,12 +165,12 @@ def get_transformed_data(self, product, product_type): }) elif product_type == 'degree': data.update({ - "Subjects": ", ".join(subject.name for subject in product.subjects), + "Subjects": ", ".join(subject.name for subject in product.active_subjects), "Subjects Spanish": ", ".join( translation.name for subject in product.subjects for translation in subject.spanish_translations ), - "Languages": ", ".join(language.code for language in product.languages), + "Languages": ", ".join(language.code for language in product.active_languages), "Marketing Image": product.card_image.url if product.card_image else "", }) @@ -198,7 +199,7 @@ def handle(self, *args, **options): raise CommandError('No products found for the given criteria.') products_count = products.count() - logger.info(f'Fetched {products_count} courses from the database') + logger.info(f'Fetched {products_count} {product_type}s from the database') if output_csv: with open(output_csv, 'w', newline='') as output_file: output_writer = self.write_csv_header(output_file) 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 557cfb1dbf..511034edc3 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 @@ -309,8 +309,8 @@ def test_get_transformed_data_for_degree(self): org.logo_image.url for org in product_authoring_orgs if org.logo_image ), "Organizations Abbr": ", ".join(org.key for org in product_authoring_orgs), - "Languages": ", ".join(language.code for language in product.languages), - "Subjects": ", ".join(subject.name for subject in product.subjects), + "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 for translation in subject.spanish_translations diff --git a/course_discovery/apps/course_metadata/models.py b/course_discovery/apps/course_metadata/models.py index d93a888a18..26d81bc6a4 100644 --- a/course_discovery/apps/course_metadata/models.py +++ b/course_discovery/apps/course_metadata/models.py @@ -3518,6 +3518,16 @@ def course_run_statuses(self): def languages(self): return {course_run.language for course_run in self.course_runs if course_run.language is not None} + @property + def active_languages(self): + """ + :return: The list of languages; It gives preference to the language_override over the languages + extracted from the course runs. + """ + if self.language_override: + return {self.language_override} + return self.languages + @property def transcript_languages(self): languages = [course_run.transcript_languages.all() for course_run in self.course_runs] @@ -3540,6 +3550,24 @@ def subjects(self): common_others = [s for s, _ in Counter(course_subjects).most_common() if s not in common_primary] return common_primary + common_others + @property + def active_subjects(self): + """ + :return: The list of subjects; the first subject should be the most common primary subjects of its courses, + other subjects should be collected and ranked by frequency among the courses. + + Note: This method gives preference to the primary_subject_override over the primary subject of the courses. + """ + subjects = self.subjects + + if self.primary_subject_override: + if self.primary_subject_override not in subjects: + subjects = [self.primary_subject_override] + subjects + else: + subjects = [self.primary_subject_override] + [subject for subject in subjects if subject != self.primary_subject_override] + + return subjects + @property def topics(self): """