Skip to content

Commit

Permalink
Merge pull request #60 from SADiLaR/feature/document-search
Browse files Browse the repository at this point in the history
Upated search page and views for testing
  • Loading branch information
daniel-gray-tangent authored May 28, 2024
2 parents 8c991dc + c47e02e commit 4baf265
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 12 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=''
1 change: 1 addition & 0 deletions .env.testing
Original file line number Diff line number Diff line change
Expand Up @@ -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=''
45 changes: 43 additions & 2 deletions app/app/views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
12 changes: 6 additions & 6 deletions app/templates/app/institutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ <h5 class="card-title">{{ institution.name }}</h5>
{% elif institution.rating <= 80 %}
<i class="bi-star-fill"></i>
<i class="bi-star-fill"></i>
<i class="bi-star-fill""></i>
<i class="bi-star-fill""></i>
<i class="bi-star-fill"></i>
<i class="bi-star-fill"></i>
<i class="bi-star"></i>
{% else %}
<i class="bi-star-fill"></i>
<i class="bi-star-fill""></i>
<i class="bi-star-fill""></i>
<i class="bi-star-fill""></i>
<i class="bi-star-fill""></i>
<i class="bi-star-fill"></i>
<i class="bi-star-fill"></i>
<i class="bi-star-fill"></i>
<i class="bi-star-fill"></i>
{% endif %}
</div>
<div class="card-logo">
Expand Down
76 changes: 72 additions & 4 deletions app/templates/app/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,79 @@
<div class="card content-card">
<div class="card-body">
<h5 class="card-title">Search a term</h5>
<p class="card-text text-center">
Functionality of this page coming soon
</p>
</div>
{% if feature_flag == "search_feature" %}
{# test section start#}
<form>
<div class="input-group mb-3">
<input type="search" class="form-control" name="q" placeholder="Search">
<div class="input-group-append">
<button class="btn btn-outline-secondary">Search</button>
</div>
</div>
</form>
<div class="mb-3">
<span class="mr-5"><b>Results:</b></span>
<span>{{ documents|length }}</span>
</div>
<div>
{% for document in documents %}
<div>
<ul>
<li>
<span class="mr-5 text-primary"><b>Title:</b></span>
<span>{{ document.title }}</span>
</li>
<li class="mb-2">
<span class="mr-5"><b>Institution:</b></span>
<span>{{ document.institution }}</span>
</li>
<li class="mb-2">
<span class="mr-5"><b>Headline:</b></span>
<span>{{ document.search_headline|safe }}</span>
</li>
<li>
<span class="mr-5"><b>File:</b></span>
<span>{{ document.uploaded_file }}</span>
</li>
<li>
<span class="mr-5"><b>License:</b></span>
<span>{{ document.license }}</span>
</li>
<li>
<span class="mr-5"><b>Mime Type:</b></span>
<span>{{ document.mime_type }}</span>
</li>
<li class="mt-2">
<span class="mr-5"><b>Rank:</b></span>
<span><i>{{ document.rank }}</i></span>
</li>
</ul>
</div>
{% endfor %}
</div>
<div class="pagination">
<span class="step-links">
{% if documents.has_previous %}
<a href="?page=1">&laquo; first</a>
<a href="?page={{ documents.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ documents.number }} of {{ documents.paginator.num_pages }}.
</span>
{% if documents.has_next %}
<a href="?page={{ documents.next_page_number }}">next</a>
<a href="?page={{ documents.paginator.num_pages }}">last &raquo;</a>
{% endif %}
</span>
<hr>
</div>
</div>
{# test section end#}
{% else %}
<p>Functionality of this page coming soon</p>
{% endif %}
</div>
</div>
</div>

{% endblock content %}
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 4baf265

Please sign in to comment.