From 5b8587572bc9c1c03f9d36f6a5ee54a25bf60868 Mon Sep 17 00:00:00 2001 From: Muhammad Afaq Shuaib Date: Wed, 11 Sep 2024 19:54:08 +0500 Subject: [PATCH] refactor: some goofy logic --- course_discovery/apps/course_metadata/query.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/course_discovery/apps/course_metadata/query.py b/course_discovery/apps/course_metadata/query.py index 24729549b13..fa8a7e0f089 100644 --- a/course_discovery/apps/course_metadata/query.py +++ b/course_discovery/apps/course_metadata/query.py @@ -44,10 +44,6 @@ def available(self, exclude_hidden_runs=False): Q(course_runs__status=CourseRunStatus.Published) ) - if exclude_hidden_runs: - # A CourseRun is "hidden" if it has a "hidden" attribute set to True. - marketable &= ~Q(course_runs__hidden=True) - # exclude() is intentionally avoided here. We want Courses to be included # in the resulting queryset if at least one of their runs matches our availability # criteria. For example, consider a Course with two CourseRuns; one of the @@ -58,7 +54,12 @@ def available(self, exclude_hidden_runs=False): # By itself, the query performs a join across several tables and would return # the id of the same course multiple times (a separate copy for each available # seat in each available run). - ids = self.filter(enrollable & not_ended & marketable).values('id').distinct() + if exclude_hidden_runs: + # A CourseRun is "hidden" if it has a "hidden" attribute set to True. + hidden = ~Q(course_runs__hidden=True) + ids = self.filter(enrollable & not_ended & marketable & hidden).values('id').distinct() + else: + ids = self.filter(enrollable & not_ended & marketable).values('id').distinct() # Now return the full object for each of the selected ids return self.filter(id__in=ids)