From 88c2016df03157d513937aa1b6921c0d7d33c9b0 Mon Sep 17 00:00:00 2001 From: Hina Khadim Date: Mon, 23 Oct 2023 23:22:35 +0500 Subject: [PATCH] feat: add organization and category filter in course_runs and programs --- course_discovery/apps/api/v1/views/search.py | 1 + .../course_metadata/search_indexes/documents/program.py | 6 ++++++ .../discovery_dataloader_app/api/v1/views/dataloader_api.py | 6 ++++-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/course_discovery/apps/api/v1/views/search.py b/course_discovery/apps/api/v1/views/search.py index 4fd7c6bea7..ec969b6bc8 100644 --- a/course_discovery/apps/api/v1/views/search.py +++ b/course_discovery/apps/api/v1/views/search.py @@ -163,6 +163,7 @@ class ProgramSearchViewSet(BaseElasticsearchDocumentViewSet): 'title': 'title', 'type': {'field': 'type.raw'}, 'uuid': 'uuid', + 'org': {'field': 'org', 'lookups': [LOOKUP_FILTER_TERM, LOOKUP_FILTER_TERMS, LOOKUP_QUERY_EXCLUDE]} } def get_serializer_context(self): diff --git a/course_discovery/apps/course_metadata/search_indexes/documents/program.py b/course_discovery/apps/course_metadata/search_indexes/documents/program.py index aa09e85ef0..6b557cfa87 100644 --- a/course_discovery/apps/course_metadata/search_indexes/documents/program.py +++ b/course_discovery/apps/course_metadata/search_indexes/documents/program.py @@ -77,6 +77,8 @@ class ProgramDocument(BaseDocument, OrganizationsMixin): no_of_courses = fields.IntegerField(fields={'raw': fields.KeywordField()}) one_click_purchase_enabled = fields.BooleanField() + org = fields.KeywordField(multi=True) + def prepare_aggregation_key(self, obj): return 'program:{}'.format(obj.uuid) @@ -124,6 +126,10 @@ def prepare_type(self, obj): def prepare_no_of_courses(self, obj): return len([course_run for course_run in obj.course_runs]) + + def prepare_org(self, obj): + organizations = [org.key for org in obj.authoring_organizations.all()] + [org.key for org in obj.credit_backing_organizations.all()] + return list(set(organizations)) def get_queryset(self): return super().get_queryset().select_related('type').select_related('partner').prefetch_related('courses__course_runs') diff --git a/course_discovery/apps/discovery_dataloader_app/api/v1/views/dataloader_api.py b/course_discovery/apps/discovery_dataloader_app/api/v1/views/dataloader_api.py index 62116d39ef..ea77c233f5 100644 --- a/course_discovery/apps/discovery_dataloader_app/api/v1/views/dataloader_api.py +++ b/course_discovery/apps/discovery_dataloader_app/api/v1/views/dataloader_api.py @@ -5,7 +5,7 @@ from discovery_dataloader_app import serializers from django_elasticsearch_dsl_drf.constants import ( - LOOKUP_FILTER_TERM, LOOKUP_FILTER_TERMS, LOOKUP_QUERY_EXCLUDE, + LOOKUP_FILTER_TERM, LOOKUP_FILTER_TERMS, LOOKUP_QUERY_EXCLUDE, LOOKUP_QUERY_IN ) from django_elasticsearch_dsl_drf.filter_backends import DefaultOrderingFilterBackend, FilteringFilterBackend, MultiMatchSearchFilterBackend, SearchFilterBackend from opaque_keys.edx.keys import CourseKey @@ -97,6 +97,8 @@ class DataLoaderCourseRunSearchViewSet(CourseRunSearchViewSet): 'published': 'published', 'availability': 'availability', 'featured': 'course_overridden', - 'title': 'title', 'number': 'number', + 'org': {'field': 'org.raw', 'lookups': [LOOKUP_FILTER_TERM, LOOKUP_FILTER_TERMS]}, + 'category': {'field': 'subjects.raw', 'lookups': [LOOKUP_FILTER_TERM, LOOKUP_FILTER_TERMS, LOOKUP_QUERY_IN]}, + }