Skip to content

Commit

Permalink
Implement full text search into the Dashboard
Browse files Browse the repository at this point in the history
Fix #226
  • Loading branch information
cuducos committed Sep 28, 2017
1 parent ea5c9a9 commit 7020281
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions jarbas/dashboard/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from brazilnum.cnpj import format_cnpj
from brazilnum.cpf import format_cpf
from django.contrib.admin import SimpleListFilter
from django.contrib.postgres.search import SearchQuery, SearchRank
from django.db.models import F
from django.forms.widgets import Widget
from simple_history.admin import SimpleHistoryAdmin

Expand Down Expand Up @@ -247,16 +249,7 @@ class ReimbursementModelAdmin(SimpleHistoryAdmin):
# 'still_available',
)

search_fields = (
'applicant_id',
'cnpj_cpf',
'congressperson_name',
'document_id',
'party',
'state',
'supplier',
'subquota_description',
)
search_fields = ('search_vector',)

list_filter = (
SuspiciousListFilter,
Expand Down Expand Up @@ -376,5 +369,18 @@ def formfield_for_dbfield(self, db_field, **kwargs):
kwargs['widget'] = widgets.get(db_field.name)
return super().formfield_for_dbfield(db_field, **kwargs)

def get_search_results(self, request, queryset, search_term):
if not search_term:
return super(ReimbursementModelAdmin, self) \
.get_search_results(request, queryset, search_term)

query = SearchQuery(search_term, config='portuguese')
rank = SearchRank(F('search_vector'), query)
queryset = Reimbursement.objects.annotate(rank=rank) \
.filter(search_vector=query) \
.order_by('-rank')

return queryset, False


dashboard.register(Reimbursement, ReimbursementModelAdmin)

0 comments on commit 7020281

Please sign in to comment.