Browse Source

Vylepšen autocomplete na webu, potřeba otestovat výkon.

middleware_test
parent
commit
4a44ec6b98
  1. 14
      api/views/autocomplete.py

14
api/views/autocomplete.py

@ -5,16 +5,20 @@ from django.db.models import Q
import seminar.models as m import seminar.models as m
from .helpers import LoginRequiredAjaxMixin from .helpers import LoginRequiredAjaxMixin
# TODO filosofie - zkratky, jak v databázi, tak ve vyhledávání (SPŠE, GASOŠ, Kpt., soukr)
class SkolaAutocomplete(autocomplete.Select2QuerySetView): class SkolaAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self): def get_queryset(self):
# Don't forget to filter out results depending on the visitor ! # Don't forget to filter out results depending on the visitor !
qs = m.Skola.objects.all() qs = m.Skola.objects.all()
if self.q: if self.q:
qs = qs.filter( words = self.q.split(' ') #TODO re split podle bileho znaku
Q(nazev__istartswith=self.q)| partq = Q()
Q(kratky_nazev__istartswith=self.q)| for w in words: # Hledej po slovech, zahoď čárky a tečky z konců.
Q(ulice__istartswith=self.q)| if w[-1] in (".",","):
Q(mesto__istartswith=self.q)) w = w[:-1]
partq &= (Q(nazev__icontains=w)|Q(kratky_nazev__icontains=w)|Q(ulice__icontains=w)|Q(mesto__icontains=w))
qs = qs.filter(partq)
return qs return qs

Loading…
Cancel
Save