from dal import autocomplete from django.shortcuts import get_object_or_404 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: 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 class ResitelAutocomplete(LoginRequiredAjaxMixin,autocomplete.Select2QuerySetView): def get_queryset(self): qs = m.Resitel.objects.all() if self.q: parts = self.q.split() query = Q() for part in parts: query &= ( Q(osoba__jmeno__istartswith=self.q)| Q(osoba__prijmeni__istartswith=self.q)| Q(osoba__prezdivka__istartswith=self.q) ) qs = qs.filter(query) return qs class OdevzdatelnyProblemAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): nastaveni = get_object_or_404(m.Nastaveni) rocnik = nastaveni.aktualni_rocnik # Od tohoto místa dál jsem zkoušel spoustu variací podle https://django-polymorphic.readthedocs.io/en/stable/advanced.html temaQ = Q(Tema___rocnik = rocnik, stav=m.Problem.STAV_ZADANY) ulohaQ = Q(Uloha___cislo_zadani__rocnik = rocnik, stav=m.Problem.STAV_ZADANY) clanekQ = Q(Clanek___cislo__rocnik = rocnik, stav=m.Problem.STAV_ZADANY) qs = m.Problem.objects.filter(temaQ | ulohaQ | clanekQ) #print(temata, ulohy, clanky) #ulohy.union(temata, all=True) #print(ulohy) #ulohy.union(clanky, all=True) #print(ulohy) #qs = ulohy print(qs) if self.q: qs = qs.filter( Q(nazev__icontains=self.q)) return qs # Ceka na autocomplete v3 # class OrganizatorAutocomplete(autocomplete.Select2QuerySetView): # def get_queryset(self): # if not self.request.user.is_authenticated(): # return Organizator.objects.none() # # qs = aktivniOrganizatori() # # if self.q: # if self.q[0] == "!": # qs = Organizator.objects.all() # query = self.q[1:] # else: # query = self.q # qs = qs.filter( # Q(prezdivka__isstartswith=query)| # Q(user__first_name__isstartswith=query)| # Q(user__last_name__isstartswith=query)) # # return qs