From 3d222aedab0636a84011dced568c5dcd48fc5b15 Mon Sep 17 00:00:00 2001 From: Mehdi Benadda Date: Tue, 17 Jul 2018 18:25:23 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F(search)=20import=20indexers?= =?UTF-8?q?=20in=20viewsets=20through=20settings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Indexers are meant to expose functionality that can be easily overridden through settings. That cannot work if we import them directly though. Use the values from settings instead. --- src/richie/apps/search/viewsets/courses.py | 17 ++++++++++------- .../apps/search/viewsets/organizations.py | 19 +++++++++++-------- src/richie/apps/search/viewsets/subjects.py | 19 +++++++++++-------- 3 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/richie/apps/search/viewsets/courses.py b/src/richie/apps/search/viewsets/courses.py index c986a01faf..af4e86e134 100644 --- a/src/richie/apps/search/viewsets/courses.py +++ b/src/richie/apps/search/viewsets/courses.py @@ -2,6 +2,7 @@ API endpoints to access courses through ElasticSearch """ from django.conf import settings +from django.utils.module_loading import import_string from django.utils.translation import get_language_from_request from elasticsearch.exceptions import NotFoundError @@ -9,7 +10,6 @@ from rest_framework.viewsets import ViewSet from ..forms import CourseListForm -from ..indexers.courses import CoursesIndexer class CoursesViewSet(ViewSet): @@ -18,6 +18,9 @@ class CoursesViewSet(ViewSet): See API Blueprint for details on consumer use """ + # Get the courses indexer from the settings + indexer = import_string(settings.ES_INDICES.courses) + # pylint: disable=no-self-use,unused-argument,too-many-locals def list(self, request, version): """ @@ -126,8 +129,8 @@ def list(self, request, version): } course_query_response = settings.ES_CLIENT.search( - index=CoursesIndexer.index_name, - doc_type=CoursesIndexer.document_type, + index=self.indexer.index_name, + doc_type=self.indexer.document_type, body={"aggs": aggs, "query": query}, # Directly pass meta-params through as arguments to the ES client from_=params_form.cleaned_data.get("offset") or 0, @@ -141,7 +144,7 @@ def list(self, request, version): "total_count": course_query_response["hits"]["total"], }, "objects": [ - CoursesIndexer.format_es_course_for_api( + self.indexer.format_es_course_for_api( es_course, # Get the best language we can return multilingual fields in get_language_from_request(request), @@ -174,8 +177,8 @@ def retrieve(self, request, pk, version): # raise and end up in a 500 error otherwise try: query_response = settings.ES_CLIENT.get( - index=CoursesIndexer.index_name, - doc_type=CoursesIndexer.document_type, + index=self.indexer.index_name, + doc_type=self.indexer.document_type, id=pk, ) except NotFoundError: @@ -183,7 +186,7 @@ def retrieve(self, request, pk, version): # Format a clean course object as a response return Response( - CoursesIndexer.format_es_course_for_api( + self.indexer.format_es_course_for_api( query_response, # Get the best language we can return multilingual fields in get_language_from_request(request), diff --git a/src/richie/apps/search/viewsets/organizations.py b/src/richie/apps/search/viewsets/organizations.py index 81f6847f74..7684b7424b 100644 --- a/src/richie/apps/search/viewsets/organizations.py +++ b/src/richie/apps/search/viewsets/organizations.py @@ -2,6 +2,7 @@ API endpoints to access organizations through ElasticSearch """ from django.conf import settings +from django.utils.module_loading import import_string from django.utils.translation import get_language_from_request from elasticsearch.exceptions import NotFoundError @@ -9,7 +10,6 @@ from rest_framework.viewsets import ViewSet from ..exceptions import QueryFormatException -from ..indexers.organizations import OrganizationsIndexer class OrganizationsViewSet(ViewSet): @@ -18,6 +18,9 @@ class OrganizationsViewSet(ViewSet): See API Blueprint for details on consumer use. """ + # Get the organizations indexer from the settings + indexer = import_string(settings.ES_INDICES.organizations) + # pylint: disable=no-self-use,unused-argument def list(self, request, version): """ @@ -25,14 +28,14 @@ def list(self, request, version): organizations and returns a list of matching items """ try: - limit, offset, query = OrganizationsIndexer.build_es_query(request) + limit, offset, query = self.indexer.build_es_query(request) except QueryFormatException as exc: # Return a 400 with error information if the query params are not as expected return Response(status=400, data={"errors": exc.args[0]}) search_query_response = settings.ES_CLIENT.search( - index=OrganizationsIndexer.index_name, - doc_type=OrganizationsIndexer.document_type, + index=self.indexer.index_name, + doc_type=self.indexer.document_type, body=query, # Directly pass meta-params through as arguments to the ES client from_=offset, @@ -49,7 +52,7 @@ def list(self, request, version): "total_count": search_query_response["hits"]["total"], }, "objects": [ - OrganizationsIndexer.format_es_organization_for_api( + self.indexer.format_es_organization_for_api( organization, # Get the best language to return multilingual fields get_language_from_request(request), @@ -69,8 +72,8 @@ def retrieve(self, request, pk, version): # raise and end up in a 500 error otherwise try: query_response = settings.ES_CLIENT.get( - index=OrganizationsIndexer.index_name, - doc_type=OrganizationsIndexer.document_type, + index=self.indexer.index_name, + doc_type=self.indexer.document_type, id=pk, ) except NotFoundError: @@ -78,7 +81,7 @@ def retrieve(self, request, pk, version): # Format a clean organization object as a response return Response( - OrganizationsIndexer.format_es_organization_for_api( + self.indexer.format_es_organization_for_api( query_response, # Get the best language we can return multilingual fields in get_language_from_request(request), diff --git a/src/richie/apps/search/viewsets/subjects.py b/src/richie/apps/search/viewsets/subjects.py index 60129da098..ffe9b1b5e9 100644 --- a/src/richie/apps/search/viewsets/subjects.py +++ b/src/richie/apps/search/viewsets/subjects.py @@ -2,6 +2,7 @@ API endpoints to access subjects through ElasticSearch """ from django.conf import settings +from django.utils.module_loading import import_string from django.utils.translation import get_language_from_request from elasticsearch.exceptions import NotFoundError @@ -9,7 +10,6 @@ from rest_framework.viewsets import ViewSet from ..exceptions import QueryFormatException -from ..indexers.subjects import SubjectsIndexer class SubjectsViewSet(ViewSet): @@ -18,6 +18,9 @@ class SubjectsViewSet(ViewSet): See API Blueprint for details on consumer use. """ + # Get the subjects indexer from the settings + indexer = import_string(settings.ES_INDICES.subjects) + # pylint: disable=no-self-use,unused-argument def list(self, request, version): """ @@ -25,14 +28,14 @@ def list(self, request, version): and returns a list of matching items """ try: - limit, offset, query = SubjectsIndexer.build_es_query(request) + limit, offset, query = self.indexer.build_es_query(request) except QueryFormatException as exc: # Return a 400 with error information if the query params are not as expected return Response(status=400, data={"errors": exc.args[0]}) query_response = settings.ES_CLIENT.search( - index=SubjectsIndexer.index_name, - doc_type=SubjectsIndexer.document_type, + index=self.indexer.index_name, + doc_type=self.indexer.document_type, body=query, # Directly pass meta-params through as arguments to the ES client from_=offset, @@ -49,7 +52,7 @@ def list(self, request, version): "total_count": query_response["hits"]["total"], }, "objects": [ - SubjectsIndexer.format_es_subject_for_api( + self.indexer.format_es_subject_for_api( subject, # Get the best language we can return multilingual fields in get_language_from_request(request), @@ -69,8 +72,8 @@ def retrieve(self, request, pk, version): # raise and end up in a 500 error otherwise try: query_response = settings.ES_CLIENT.get( - index=SubjectsIndexer.index_name, - doc_type=SubjectsIndexer.document_type, + index=self.indexer.index_name, + doc_type=self.indexer.document_type, id=pk, ) except NotFoundError: @@ -78,7 +81,7 @@ def retrieve(self, request, pk, version): # Format a clean subject object as a response return Response( - SubjectsIndexer.format_es_subject_for_api( + self.indexer.format_es_subject_for_api( query_response, # Get the best language we can return multilingual fields in get_language_from_request(request),