Skip to content

Commit

Permalink
feat: return override fields from properties when they are populated
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Sep 9, 2024
1 parent 025e8a3 commit b237980
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def get_transformed_data(self, product, product_type):
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.languages) or "en-us",
"Marketing Image": product.card_image.url if product.card_image else "",
})

Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions course_discovery/apps/course_metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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):
"""
Expand Down

0 comments on commit b237980

Please sign in to comment.