From ea6595d75cb6827e7d02324dd927fb9fc6bbf419 Mon Sep 17 00:00:00 2001 From: Haresh Kainth Date: Fri, 15 Nov 2024 10:27:48 +0000 Subject: [PATCH] feat:add new Publishers API endpoint and updated response values Introduced a new `PublishersViewSet` with a `publishers` endpoint to retrieve distinct publisher names from the database. Updated URL routing to accommodate the new endpoint and added necessary imports and utility functions. --- orp/config/urls.py | 25 ++++++++++++++++++++++--- orp/orp_search/utils/paginate.py | 1 + orp/orp_search/utils/search.py | 10 ++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/orp/config/urls.py b/orp/config/urls.py index 304a4fc..5d5fa80 100644 --- a/orp/config/urls.py +++ b/orp/config/urls.py @@ -6,9 +6,9 @@ import orp_search.views as orp_search_views from orp_search.config import SearchDocumentConfig -from orp_search.models import DataResponseModel +from orp_search.models import DataResponseModel, logger from orp_search.utils.documents import clear_all_documents -from orp_search.utils.search import search +from orp_search.utils.search import get_publisher_names, search from rest_framework import routers, serializers, status, viewsets from rest_framework.decorators import action from rest_framework.response import Response @@ -123,10 +123,29 @@ def rebuild_cache(self, request, *args, **kwargs): ) +class PublishersViewSet(viewsets.ViewSet): + @action(detail=False, methods=["get"], url_path="publishers") + def publishers(self, request, *args, **kwargs): + try: + publishers = get_publisher_names() + logger.info(f"publishers: {publishers}") + + return Response( + data={"results": publishers}, + status=status.HTTP_200_OK, + ) + except Exception as e: + return Response( + data={"message": f"error fetching publishers: {e}"}, + status=status.HTTP_500_INTERNAL_SERVER_ERROR, + ) + + # Routers provide an easy way of automatically determining the URL conf. router = routers.DefaultRouter() router.register(r"v1", DataResponseViewSet, basename="search") -router.register(r"v1", RebuildCacheViewSet, basename="rebuild") +router.register(r"v1/cache", RebuildCacheViewSet, basename="rebuild") +router.register(r"v1/retrieve", PublishersViewSet, basename="publishers") urlpatterns = [ path("api/", include(router.urls)), diff --git a/orp/orp_search/utils/paginate.py b/orp/orp_search/utils/paginate.py index 59aabe3..5b1949b 100644 --- a/orp/orp_search/utils/paginate.py +++ b/orp/orp_search/utils/paginate.py @@ -71,6 +71,7 @@ def paginate( "description": paginated_document.description, "type": paginated_document.type, "date_modified": paginated_document.date_modified, + "regulatory_topics": paginated_document.regulatory_topics, } ) diff --git a/orp/orp_search/utils/search.py b/orp/orp_search/utils/search.py index ce4d6c3..d5f79c9 100644 --- a/orp/orp_search/utils/search.py +++ b/orp/orp_search/utils/search.py @@ -137,3 +137,13 @@ def search(context: dict, request: HttpRequest) -> dict: ) return context + + +def get_publisher_names(): + publishers = DataResponseModel.objects.values("publisher").distinct() + + publishers_list = [] + for publisher in publishers: + publishers_list.append(publisher.publisher) + + return publishers_list