Skip to content

Commit

Permalink
updating search
Browse files Browse the repository at this point in the history
- added django-filter plugin
- add multiselect search
  • Loading branch information
daniel-gray-tangent committed Jul 10, 2024
1 parent 8973156 commit 914981b
Show file tree
Hide file tree
Showing 8 changed files with 375 additions and 143 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
cp .env.testing app/.env
cd app/
mkdir -p static_files
python manage.py validate_templates
python manage.py validate_templates --ignore-app django_filters
- name: Run Tests
run: |
cp .env.testing app/.env
Expand Down
1 change: 1 addition & 0 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"general",
"simple_history",
"accounts",
"django_filters",
]

# Add django-extensions to the installed apps if DEBUG is True
Expand Down
51 changes: 23 additions & 28 deletions app/app/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import os

from django.contrib.postgres.search import SearchHeadline, SearchQuery, SearchRank
from django.core.paginator import Paginator
# from pprint import pprint
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db.models import Count
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.utils.translation import gettext as _

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


Expand Down Expand Up @@ -230,39 +229,35 @@ def institutions(request):


def search(request):
q = request.GET.get("q")
# f = DocumentFileFilter(request.GET, queryset=DocumentFile.objects.all().defer("document_data"))
f = DocumentFileFilter(request.GET, queryset=DocumentFile.objects.all())

if q:
queue = SearchQuery(q)
search_headline = SearchHeadline("document_data", queue)
template = "app/search.html"

documents = (
DocumentFile.objects.annotate(rank=SearchRank("search_vector", queue))
.annotate(search_headline=search_headline)
.filter(search_vector=queue)
.order_by("-rank")
)
# print(f.qs)

else:
documents = None
paginator = Paginator(f.qs, 5) # 5 documents per page

# 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
print(paginator)

# 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"
try:
# Get the page
page_obj = paginator.page(page_number)
except PageNotAnInteger:
# If page is not an integer, deliver first page
page_obj = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g., 9999), deliver last page of results
page_obj = paginator.page(paginator.num_pages)

print(page_obj)
# Update the context with the page object
context = {
"search_results": paginator.page(page_obj.number),
"filter": f,
"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)
51 changes: 51 additions & 0 deletions app/general/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import django_filters
from django import forms
from django.contrib.postgres.search import SearchHeadline, SearchQuery, SearchRank
from django.db.models import F
from django_filters import ModelMultipleChoiceFilter, MultipleChoiceFilter

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


class DocumentFileFilter(django_filters.FilterSet):
search = django_filters.CharFilter(method="filter_search", label="Search")
institution = ModelMultipleChoiceFilter(
queryset=Institution.objects.all(), widget=forms.CheckboxSelectMultiple
)
document_type = MultipleChoiceFilter(
choices=DocumentFile.document_type_choices, widget=forms.CheckboxSelectMultiple
)
subjects = ModelMultipleChoiceFilter(
queryset=Subject.objects.all(), widget=forms.CheckboxSelectMultiple
)
languages = ModelMultipleChoiceFilter(
queryset=Language.objects.all(), widget=forms.CheckboxSelectMultiple
)

class Meta:
model = DocumentFile
fields = [
"document_type",
"institution",
"subjects",
"languages",
]

def filter_search(self, queryset, name, value):
if value:
queue = SearchQuery(value)
search_rank = SearchRank(F("search_vector"), queue)

queryset = (
queryset.annotate(
rank=search_rank,
)
.defer("document_data")
.select_related("institution")
.filter(search_vector=queue)
.order_by("-rank")
)
return queryset

else:
return ""
39 changes: 39 additions & 0 deletions app/general/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# forms.py
import django_filters
from django import forms

from general.models import DocumentFile


class DocumentFileSearchForm(forms.Form):
title = django_filters.CharFilter(lookup_expr="iexact")
# title = django_filters.CharFilter(lookup_expr='iexact')

class Meta:
model = DocumentFile
fields = ["subjects", "languages"]


# class DocumentFileSearchForm(forms.Form):
# query = forms.CharField(label='Search', max_length=255, required=False)
#
# subjects = forms.MultipleChoiceField(
# choices=[],
# required=False,
# widget=forms.SelectMultiple
# )
#
# languages = forms.MultipleChoiceField(
# choices=[],
# required=False,
# # widget=forms.CheckboxSelectMultiple
# widget=forms.SelectMultiple
# )
#
# def __init__(self, *args, **kwargs):
# super().__init__(*args, **kwargs)
#
# self.fields['subjects'].choices = [(cat, cat) for cat in
# Subject.objects.values_list('name', flat=True).distinct()]
# self.fields['languages'].choices = [(brand, brand) for brand in
# Language.objects.values_list('name', flat=True).distinct()]
Loading

0 comments on commit 914981b

Please sign in to comment.