Skip to content

Commit

Permalink
♻️(search) import indexers in viewsets through settings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbenadda committed Jul 19, 2018
1 parent 9834aa3 commit 3d222ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
17 changes: 10 additions & 7 deletions src/richie/apps/search/viewsets/courses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
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
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet

from ..forms import CourseListForm
from ..indexers.courses import CoursesIndexer


class CoursesViewSet(ViewSet):
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -174,16 +177,16 @@ 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:
return Response(status=404)

# 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),
Expand Down
19 changes: 11 additions & 8 deletions src/richie/apps/search/viewsets/organizations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
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
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet

from ..exceptions import QueryFormatException
from ..indexers.organizations import OrganizationsIndexer


class OrganizationsViewSet(ViewSet):
Expand All @@ -18,21 +18,24 @@ 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):
"""
Organization search endpoint: pass query params to ElasticSearch so it filters
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,
Expand All @@ -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),
Expand All @@ -69,16 +72,16 @@ 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:
return Response(status=404)

# 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),
Expand Down
19 changes: 11 additions & 8 deletions src/richie/apps/search/viewsets/subjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
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
from rest_framework.response import Response
from rest_framework.viewsets import ViewSet

from ..exceptions import QueryFormatException
from ..indexers.subjects import SubjectsIndexer


class SubjectsViewSet(ViewSet):
Expand All @@ -18,21 +18,24 @@ 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):
"""
Subject search endpoint: pass query params to ElasticSearch so it filters subjects
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,
Expand All @@ -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),
Expand All @@ -69,16 +72,16 @@ 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:
return Response(status=404)

# 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),
Expand Down

0 comments on commit 3d222ae

Please sign in to comment.