From c47e02e366a452f247502811f9cd2d71c0728cc7 Mon Sep 17 00:00:00 2001 From: Daniel Gray Date: Mon, 20 May 2024 16:01:10 +0200 Subject: [PATCH] Upated search page and views for testing - updated search views - added new search query - added feature fag --- .env.example | 1 + .env.testing | 1 + app/app/views.py | 45 ++++++++++++++++- app/templates/app/institutions.html | 12 ++--- app/templates/app/search.html | 76 +++++++++++++++++++++++++++-- docker-compose.yml | 1 + 6 files changed, 124 insertions(+), 12 deletions(-) diff --git a/.env.example b/.env.example index 17d3f136..ec78dd8b 100644 --- a/.env.example +++ b/.env.example @@ -10,3 +10,4 @@ LOGGING_HANDLERS_LEVEL=INFO LOGGING_LOGGERS_LEVEL=INFO LOGGING_LOGGERS_DJANGO_LEVEL=INFO TESTING_DIR=/app/general/tests/files/ +FEATURE_FLAG='' diff --git a/.env.testing b/.env.testing index ebf914ca..d8d2568a 100644 --- a/.env.testing +++ b/.env.testing @@ -10,3 +10,4 @@ LOGGING_HANDLERS_LEVEL=INFO LOGGING_LOGGERS_LEVEL=INFO LOGGING_LOGGERS_DJANGO_LEVEL=INFO TESTING_DIR=/home/runner/work/term_platform/term_platform/app/general/tests/files/ +FEATURE_FLAG='' diff --git a/app/app/views.py b/app/app/views.py index 07d44a42..0f9d47c8 100644 --- a/app/app/views.py +++ b/app/app/views.py @@ -1,8 +1,17 @@ +import os + +from django.contrib.postgres.search import ( + SearchHeadline, + SearchQuery, + SearchRank, + SearchVector, +) +from django.core.paginator import Paginator from django.db.models import Count from django.http import HttpResponse from django.shortcuts import render -from general.models import Institution +from general.models import DocumentFile, Institution def health(request): @@ -58,7 +67,39 @@ def institutions(request): def search(request): + q = request.GET.get("q") + + if q: + vector = SearchVector("title", "document_data") + queue = SearchQuery(q) + search_headline = SearchHeadline("document_data", queue) + + documents = ( + DocumentFile.objects.annotate(rank=SearchRank(vector, queue)) + .annotate(search_headline=search_headline) + .order_by("-rank") + ) + + else: + documents = None + + # Create a Paginator instance with the documents and the number of items per page + paginator = Paginator(documents, 10) if documents else None # Show 10 documents per page + + # Get the page number from the request's GET parameters + page_number = request.GET.get("page") + + # Use the get_page method to get the Page object for that page number + page_obj = paginator.get_page(page_number) if paginator else None + + feature_flag = os.getenv("FEATURE_FLAG", False) + template = "app/search.html" - context = {"current_page": "search"} + context = { + "documents": page_obj, + "current_page": "search", + "document_count": len(documents) if documents else 0, + "feature_flag": feature_flag, + } return render(request, template_name=template, context=context) diff --git a/app/templates/app/institutions.html b/app/templates/app/institutions.html index f8d5bcce..03aaee39 100644 --- a/app/templates/app/institutions.html +++ b/app/templates/app/institutions.html @@ -45,15 +45,15 @@
{{ institution.name }}
{% elif institution.rating <= 80 %} - - + + {% else %} - - - - + + + + {% endif %} + {% endblock content %} diff --git a/docker-compose.yml b/docker-compose.yml index ece0836c..768de828 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -38,3 +38,4 @@ services: - DB_USER=sadilar # see POSTGRES_USER above - DB_PASSWORD=sadilar # see POSTGRES_PASSWORD above - TESTING_DIR=/app/general/tests/files/ + - FEATURE_FLAG=search_feature