-
Notifications
You must be signed in to change notification settings - Fork 172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: return override fields from properties when they are populated #4434
Changes from 3 commits
de88767
8b5f4b7
c06627c
0bd4300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,25 @@ 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] | ||
Comment on lines
+3563
to
+3568
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wont this code be simple if we make subjects list set, adding primary override (if present) and convert it back to list? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While converting the subjects list to a set would simplify the code by automatically handling duplicates, we need to ensure the order of subjects is preserved, especially since the primary subject override should always appear first.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not remove the inner if else and swap it with the assignment in the else? That would be equivalent to the current implementation? |
||
|
||
return subjects | ||
|
||
@property | ||
def topics(self): | ||
""" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3519,6 +3519,65 @@ def test_program_duration_override(self): | |
self.program.program_duration_override = '' | ||
assert self.program.program_duration_override is not None | ||
|
||
def test_active_subjects_with_no_override(self): | ||
""" | ||
Test that active_subjects returns the subjects from the associated courses | ||
when no primary_subject_override is set. | ||
""" | ||
|
||
subject1 = SubjectFactory.create(name='Subject 1') | ||
subject2 = SubjectFactory.create(name='Subject 2') | ||
course1 = CourseFactory.create(subjects=[subject1]) | ||
course2 = CourseFactory.create(subjects=[subject2]) | ||
program = ProgramFactory.create(primary_subject_override=None, courses=[course1, course2]) | ||
|
||
expected_subjects = [subject1, subject2] | ||
self.assertEqual(program.active_subjects, expected_subjects) | ||
|
||
def test_active_subjects_with_primary_subject_override(self): | ||
""" | ||
Test that active_subjects includes the primary_subject_override at the beginning | ||
when it is set. | ||
""" | ||
primary_subject_override = SubjectFactory.create(name='Primary Subject') | ||
other_subject = SubjectFactory.create(name='Other Subject') | ||
course = CourseFactory.create(subjects=[other_subject]) | ||
|
||
program = ProgramFactory.create(primary_subject_override=primary_subject_override, courses=[course]) | ||
|
||
expected_subjects = [primary_subject_override, other_subject] | ||
self.assertEqual(program.active_subjects, expected_subjects) | ||
|
||
def test_active_languages_with_no_override(self): | ||
""" | ||
Test that active_languages returns the languages from the associated courses | ||
when no language_override is set. | ||
""" | ||
|
||
language_en = LanguageTag.objects.create(code='en', name='English') | ||
language_fr = LanguageTag.objects.get(code='fr') | ||
|
||
course_run1 = CourseRunFactory.create(language=language_en) | ||
course_run2 = CourseRunFactory.create(language=language_fr) | ||
|
||
program = ProgramFactory.create(language_override=None, courses=[course_run1.course, course_run2.course]) | ||
|
||
expected_languages = {language_en, language_fr} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace set with list |
||
self.assertEqual(program.active_languages, expected_languages) | ||
|
||
def test_active_languages_with_language_override(self): | ||
""" | ||
Test that active_languages returns the language_override when it is set. | ||
""" | ||
|
||
language_es = LanguageTag.objects.get(code='es') | ||
language_de = LanguageTag.objects.get(code='de') | ||
course_run = CourseRunFactory.create(language=language_de) | ||
program = ProgramFactory.create(language_override=language_es, courses=[course_run.course]) | ||
|
||
expected_languages = {language_es} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. replace set with list |
||
self.assertEqual(program.active_languages, expected_languages) | ||
|
||
|
||
class ProgramSubscriptionTests(TestCase): | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's with dict operator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be a list?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1, it should be a list instead of a set
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type of languages is a set, so we're returning a set in active_languages as well to maintain consistency in the code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, makes sense.