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 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)) return qs class ResitelAutocomplete(LoginRequiredAjaxMixin,autocomplete.Select2QuerySetView): def get_queryset(self): qs = m.Resitel.objects.all() if self.q: qs = qs.filter( Q(osoba__jmeno__startswith=self.q)| Q(osoba__prijmeni__startswith=self.q)| Q(osoba__prezdivka__startswith=self.q) ) 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