Skip to content

Commit

Permalink
feat:add new Publishers API endpoint and updated response values
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
hareshkainthdbt committed Nov 15, 2024
1 parent 65a57c8 commit ea6595d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
25 changes: 22 additions & 3 deletions orp/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"},

Check warning

Code scanning / CodeQL

Information exposure through an exception Medium

Stack trace information
flows to this location and may be exposed to an external user.
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)),
Expand Down
1 change: 1 addition & 0 deletions orp/orp_search/utils/paginate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)

Expand Down
10 changes: 10 additions & 0 deletions orp/orp_search/utils/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ea6595d

Please sign in to comment.