From 4a44ec6b9845bd60021d0eaa0bea9edff1b12127 Mon Sep 17 00:00:00 2001 From: "Tomas \"Jethro\" Pokorny" Date: Mon, 11 Oct 2021 19:18:35 +0200 Subject: [PATCH] =?UTF-8?q?Vylep=C5=A1en=20autocomplete=20na=20webu,=20pot?= =?UTF-8?q?=C5=99eba=20otestovat=20v=C3=BDkon.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/views/autocomplete.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/api/views/autocomplete.py b/api/views/autocomplete.py index 121ddefc..a97fbbab 100644 --- a/api/views/autocomplete.py +++ b/api/views/autocomplete.py @@ -5,16 +5,20 @@ from django.db.models import Q import seminar.models as m 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): def get_queryset(self): # Don't forget to filter out results depending on the visitor ! qs = m.Skola.objects.all() if self.q: - qs = qs.filter( - Q(nazev__istartswith=self.q)| - Q(kratky_nazev__istartswith=self.q)| - Q(ulice__istartswith=self.q)| - Q(mesto__istartswith=self.q)) + words = self.q.split(' ') #TODO re split podle bileho znaku + partq = Q() + for w in words: # Hledej po slovech, zahoď čárky a tečky z konců. + if w[-1] in (".",","): + 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