-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6762c4
commit 597ce55
Showing
6 changed files
with
352 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import django_filters | ||
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector | ||
|
||
from general.models import DocumentFile | ||
|
||
|
||
class DocumentFileFilter(django_filters.FilterSet): | ||
search = django_filters.CharFilter(method="filter_search", label="Search") | ||
|
||
class Meta: | ||
model = DocumentFile | ||
fields = [ | ||
"document_type", | ||
"institution", | ||
"subjects", | ||
"languages", | ||
] | ||
|
||
def filter_search(self, queryset, name, value): | ||
if value: | ||
queue = SearchQuery(value) | ||
search_vector = SearchVector("title", "description", "document_data") | ||
# search_vector = SearchVector('document_data') | ||
search_rank = SearchRank(search_vector, queue) | ||
queryset = ( | ||
queryset.annotate( | ||
rank=search_rank, | ||
search_vector_annotation=search_vector, # Renamed annotation to avoid conflict | ||
) | ||
.select_related("institution") | ||
.filter(search_vector_annotation=queue) | ||
.order_by("-rank") | ||
) | ||
return queryset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()] |
Oops, something went wrong.