From 7aff413a16789104c2fc58394c51f272c4c8ce8e Mon Sep 17 00:00:00 2001 From: Muhammad Afaq Shuaib Date: Mon, 9 Sep 2024 14:06:46 +0500 Subject: [PATCH] feat: return override fields from properties when they are populated --- .../commands/populate_product_catalog.py | 2 +- .../apps/course_metadata/models.py | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) 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 769b6e9e8fc..6b6f3b75303 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 @@ -198,7 +198,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/models.py b/course_discovery/apps/course_metadata/models.py index d93a888a181..93f221c868e 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 {course_run.language for course_run in self.course_runs if course_run.language is not None} + @property def transcript_languages(self): languages = [course_run.transcript_languages.all() for course_run in self.course_runs] @@ -3540,6 +3550,22 @@ 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. + """ + if self.primary_subject_override: + if self.primary_subject_override not in self.subjects.all(): + product_subjects = [self.primary_subject_override] + list(self.subjects.all()) + else: + product_subjects = list(self.subjects.all()) + + return product_subjects + @property def topics(self): """