From ca12bdccedf5ff6429c72154c6f16b2622d0e017 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Wed, 7 Jun 2017 13:53:19 -0400 Subject: [PATCH] Add API endpoint to show all available search models. Signed-off-by: Roberto Rosario --- HISTORY.rst | 1 + docs/releases/2.3.rst | 1 + mayan/apps/dynamic_search/api_views.py | 24 ++++++++++++--------- mayan/apps/dynamic_search/classes.py | 4 ++++ mayan/apps/dynamic_search/serializers.py | 11 ++++++++++ mayan/apps/dynamic_search/tests/test_api.py | 12 +++++++++++ mayan/apps/dynamic_search/urls.py | 8 ++++++- 7 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 mayan/apps/dynamic_search/serializers.py diff --git a/HISTORY.rst b/HISTORY.rst index 6626624bd3..daef2bc4c7 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -12,6 +12,7 @@ - Catch documents with not document version when displaying their thumbnails. - Document page navigation fix when using Mayan as a sub URL app. - Add support for indexing on workflow state changes. +- Add search model list API endpoint. 2.2 (2017-04-26) ================ diff --git a/docs/releases/2.3.rst b/docs/releases/2.3.rst index 1a0f38f059..88fe3db328 100644 --- a/docs/releases/2.3.rst +++ b/docs/releases/2.3.rst @@ -38,6 +38,7 @@ Changes name `publish_workflow`, it will be accessible in the indexing template as {{ document.workflow.publish_workflow }}. The latest state of the workflow can be accessed using {{ document.workflow.publish_workflow.get_current_state }}. +- Added a new API endpoint to display a list of all the available search models. Removals -------- diff --git a/mayan/apps/dynamic_search/api_views.py b/mayan/apps/dynamic_search/api_views.py index a592a411c4..0932b046e9 100644 --- a/mayan/apps/dynamic_search/api_views.py +++ b/mayan/apps/dynamic_search/api_views.py @@ -5,7 +5,9 @@ from rest_framework.exceptions import ParseError from rest_api.filters import MayanObjectPermissionsFilter +from .classes import SearchModel from .mixins import SearchModelMixin +from .serializers import SearchModelSerializer class APISearchView(SearchModelMixin, generics.ListAPIView): @@ -15,11 +17,6 @@ class APISearchView(SearchModelMixin, generics.ListAPIView): GET: omit_serializer: true parameters: - - name: search_model - paramType: path - type: string - required: true - description: Possible values are "documents.Document" or "document.DocumentPageResult" - name: q paramType: query type: string @@ -55,11 +52,6 @@ class APIAdvancedSearchView(SearchModelMixin, generics.ListAPIView): GET: omit_serializer: true parameters: - - name: search_model - paramType: path - type: string - required: true - description: Possible values are "documents.Document" or "document.DocumentPageResult" - name: _match_all paramType: query type: string @@ -94,3 +86,15 @@ class APIAdvancedSearchView(SearchModelMixin, generics.ListAPIView): raise ParseError(unicode(exception)) return queryset + + +class APISearchModelList(generics.ListAPIView): + serializer_class = SearchModelSerializer + queryset = SearchModel.all() + + def get(self, *args, **kwargs): + """ + Returns a list of all the available search models. + """ + + return super(APISearchModelList, self).get(*args, **kwargs) diff --git a/mayan/apps/dynamic_search/classes.py b/mayan/apps/dynamic_search/classes.py index ea4635a103..bc706fd8bf 100644 --- a/mayan/apps/dynamic_search/classes.py +++ b/mayan/apps/dynamic_search/classes.py @@ -47,6 +47,10 @@ class SearchModel(object): self.permission = permission self.__class__.registry[self.get_full_name()] = self + @property + def pk(self): + return self.get_full_name() + @property def model(self): if not self._model: diff --git a/mayan/apps/dynamic_search/serializers.py b/mayan/apps/dynamic_search/serializers.py new file mode 100644 index 0000000000..91e60ad6a1 --- /dev/null +++ b/mayan/apps/dynamic_search/serializers.py @@ -0,0 +1,11 @@ +from __future__ import unicode_literals + +from rest_framework import serializers + +from .classes import SearchModel + + +class SearchModelSerializer(serializers.Serializer): + app_label = serializers.CharField(read_only=True) + model_name = serializers.CharField(read_only=True) + pk = serializers.CharField(read_only=True) diff --git a/mayan/apps/dynamic_search/tests/test_api.py b/mayan/apps/dynamic_search/tests/test_api.py index 252442d847..9c5fdf24a3 100644 --- a/mayan/apps/dynamic_search/tests/test_api.py +++ b/mayan/apps/dynamic_search/tests/test_api.py @@ -14,6 +14,8 @@ from user_management.tests import ( TEST_ADMIN_EMAIL, TEST_ADMIN_PASSWORD, TEST_ADMIN_USERNAME ) +from ..classes import SearchModel + @override_settings(OCR_AUTO_OCR=False) class SearchAPITestCase(BaseAPITestCase): @@ -52,3 +54,13 @@ class SearchAPITestCase(BaseAPITestCase): content = loads(response.content) self.assertEqual(content['results'][0]['label'], document.label) self.assertEqual(content['count'], 1) + + def test_search_models_view(self): + response = self.client.get( + reverse('rest_api:searchmodel-list') + ) + + self.assertEqual( + [search_model['pk'] for search_model in response.data['results']], + [search_model.pk for search_model in SearchModel.all()] + ) diff --git a/mayan/apps/dynamic_search/urls.py b/mayan/apps/dynamic_search/urls.py index 1300f2fa61..3c78896e71 100644 --- a/mayan/apps/dynamic_search/urls.py +++ b/mayan/apps/dynamic_search/urls.py @@ -2,7 +2,9 @@ from __future__ import unicode_literals from django.conf.urls import url -from .api_views import APIAdvancedSearchView, APISearchView +from .api_views import ( + APIAdvancedSearchView, APISearchModelList, APISearchView +) from .views import ( AdvancedSearchView, ResultsView, SearchAgainView, SearchView ) @@ -24,6 +26,10 @@ urlpatterns = [ ] api_urls = [ + url( + r'^search_models/$', APISearchModelList.as_view(), + name='searchmodel-list' + ), url( r'^search/(?P[\.\w]+)/$', APISearchView.as_view(), name='search-view'