Skip to content

Commit

Permalink
projects to search filters
Browse files Browse the repository at this point in the history
updated tests
  • Loading branch information
daniel-gray-tangent committed Jul 23, 2024
1 parent b248001 commit 2e23070
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
36 changes: 31 additions & 5 deletions app/general/filters.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import django_filters
from django import forms
from django.contrib.postgres.search import SearchHeadline, SearchQuery, SearchRank
from django.contrib.postgres.search import (
SearchHeadline,
SearchQuery,
SearchRank,
SearchVector,
)
from django.db.models import F
from django_filters import ModelMultipleChoiceFilter, MultipleChoiceFilter

from general.models import DocumentFile, Institution, Language, Subject
from general.models import DocumentFile, Institution, Language, Project, Subject


class DocumentFileFilter(django_filters.FilterSet):
Expand Down Expand Up @@ -35,8 +40,27 @@ class Meta:
def filter_queryset(self, queryset):
queryset = super().filter_queryset(queryset)

search = self.form.cleaned_data.get("search", "")
queue = SearchQuery(search.strip())
search = self.form.cleaned_data.get("search", "").strip()
queue = SearchQuery(search)

search_document_files = (
DocumentFile.objects.annotate(
search=SearchVector("title") + SearchVector("description"),
search_headline=SearchHeadline("description", queue),
)
.filter(search=SearchQuery(search))
.only("title", "description")
)

project_query = (
Project.objects.annotate(
search=SearchVector("name") + SearchVector("description"),
search_headline=SearchHeadline("description", queue),
)
.filter(search=SearchQuery(search))
.defer("institution_id", "subjects", "languages")
)

search_rank = SearchRank(F("search_vector"), queue)
search_headline = SearchHeadline("document_data", queue)
queryset = (
Expand All @@ -48,7 +72,9 @@ def filter_queryset(self, queryset):
.select_related("institution")
).order_by("-rank")

return queryset
combined_results = list(project_query) + list(search_document_files) + list(queryset)

return combined_results

def filter_search(self, queryset, name, value):
if value:
Expand Down
4 changes: 2 additions & 2 deletions app/general/tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ def test_combined_filters(self):
def test_search_filter(self):
data = {"search": "Document"}
filter = DocumentFileFilter(data=data)
self.assertEqual(len(filter.qs), 2)
self.assertEqual(len(filter.qs), 4)
self.assertIn(self.doc1, filter.qs)
self.assertIn(self.doc2, filter.qs)

data = {"search": "Document 1"}
filter = DocumentFileFilter(data=data)
self.assertEqual(len(filter.qs), 1)
self.assertEqual(len(filter.qs), 2)
self.assertIn(self.doc1, filter.qs)


Expand Down
21 changes: 19 additions & 2 deletions app/templates/app/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ <h5 class="card-title">{% trans "Search a term" %}</h5>
{% for document in search_results %}
<div>
<ul>
<li>
<span class="mr-5 text-primary"><b>{% trans "Project Name:" %}</b></span>
<span>{{ document.name }}</span>
</li>
<li>
<span class="mr-5 text-primary"><b>{% trans "Project description:" %}</b></span>
<span>{{ document.description }}</span>
</li>
<li>
<span> {% if document.logo %}
<img src="{{ document.logo.url }}" alt="
{% blocktrans with project_name=document.name %}{{ project_name }} logo{% endblocktrans %}"
class="project-logo">
{% endif %}
</span>
</li>
<li>
<span class="mr-5 text-primary"><b>{% trans "Title:" %}</b></span>
<span>{{ document.title }}</span>
Expand Down Expand Up @@ -66,8 +83,8 @@ <h5 class="card-title">{% trans "Search a term" %}</h5>
<a href="{{ search_params }}&page={{ documents.previous_page_number }}">&lt; previous</a>
{% endif %}
<span class="current">
Page {{ documents.number }} of {{ documents.paginator.num_pages }}.
</span>
Page {{ documents.number }} of {{ documents.paginator.num_pages }}.
</span>
{% if documents.has_next %}
<a href="{{ search_params }}&page={{ documents.next_page_number }}">next &gt;</a>
<a href="{{ search_params }}&page={{ documents.paginator.num_pages }}">last
Expand Down

0 comments on commit 2e23070

Please sign in to comment.