diff --git a/orp/orp_search/utils/search.py b/orp/orp_search/utils/search.py index 45c3e61..8e9b63c 100644 --- a/orp/orp_search/utils/search.py +++ b/orp/orp_search/utils/search.py @@ -56,9 +56,20 @@ def _create_search_query(search_string): return preprocess_query -def _search_database( +def search_database( config: SearchDocumentConfig, ) -> QuerySet[DataResponseModel]: + """ + Search the database for documents based on the search query + + :param config: The search configuration object + :return: A QuerySet of DataResponseModel objects + """ + + # If an id is provided, return the document with that id + if config.id: + return DataResponseModel.objects.filter(id=config.id) + # Sanatize the query string query_str = sanitize_input(config.search_query) logger.info(f"sanitized search query: {query_str}") @@ -125,7 +136,7 @@ def search(context: dict, request: HttpRequest) -> dict: config.print_to_log() # Search across specific fields - results = _search_database(config) + results = search_database(config) # convert search_results into json pag_start_time = time.time() diff --git a/orp/orp_search/views.py b/orp/orp_search/views.py index 47c8363..ac77d6e 100644 --- a/orp/orp_search/views.py +++ b/orp/orp_search/views.py @@ -5,10 +5,10 @@ from orp_search.config import SearchDocumentConfig from orp_search.models import DataResponseModel -from orp_search.public_gateway import PublicGateway -from orp_search.utils.search import search +from orp_search.utils.search import search, search_database from django.conf import settings +from django.core.serializers import serialize from django.http import HttpRequest, HttpResponse from django.shortcuts import render from django.views.decorators.http import require_http_methods @@ -38,22 +38,10 @@ def document(request: HttpRequest, id) -> HttpResponse: config = SearchDocumentConfig(search_query="", id=document_id) # Use the PublicGateway class to fetch the details - public_gateway = PublicGateway() - try: - search_result = public_gateway.search(config) - # logger.info("search result: %s", search_result) - - if "regulatory_topics" in search_result: - search_result["regulatory_topics"] = str( - search_result["regulatory_topics"] - ).split("\n") - if "related_legislation" in search_result: - search_result["related_legislation"] = str( - search_result["related_legislation"] - ).split("\n") - - context["result"] = search_result + try: + queryset = search_database(config) + context["result"] = serialize("json", queryset) return render(request, template_name="document.html", context=context) except Exception as e: logger.error("error fetching details: %s", e)