Tomas "Jethro" Pokorny
5 years ago
7 changed files with 175 additions and 74 deletions
@ -0,0 +1,2 @@ |
|||
from .views_all import * |
|||
from .autocomplete import * |
@ -0,0 +1,63 @@ |
|||
from dal import autocomplete |
|||
from django.shortcuts import get_object_or_404 |
|||
|
|||
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 |
|||
temata = m.Tema.objects.filter(rocnik=rocnik, stav=m.Problem.STAV_ZADANY) |
|||
ulohy = m.Uloha.objects.filter(cislo_deadline__rocnik = rocnik) |
|||
ulohy.union(temata) |
|||
qs = ulohy |
|||
if self.q: |
|||
qs = qs.filter( |
|||
Q(nazev__startswith=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 |
@ -0,0 +1,8 @@ |
|||
from dal import autocomplete |
|||
|
|||
class LoginRequiredAjaxMixin(object): |
|||
def dispatch(self, request, *args, **kwargs): |
|||
#if request.is_ajax() and not request.user.is_authenticated: # Pokud to otevřu jako stránku, tak se omezení neuplatní, takže to asi nechceme |
|||
if not request.user.is_authenticated: |
|||
return JsonResponse(data={'results': [], 'pagination': {}}, status=401) |
|||
return super(LoginRequiredAjaxMixin, self).dispatch(request, *args, **kwargs) |
@ -0,0 +1,91 @@ |
|||
# -*- coding: utf-8 -*- |
|||
|
|||
import datetime |
|||
from django.contrib.auth.decorators import user_passes_test |
|||
from html.parser import HTMLParser |
|||
|
|||
import seminar.models as m |
|||
|
|||
staff_member_required = user_passes_test(lambda u: u.is_staff) |
|||
|
|||
class FirstTagParser(HTMLParser): |
|||
def __init__(self, *args, **kwargs): |
|||
self.firstTag = None |
|||
super().__init__(*args, **kwargs) |
|||
def handle_data(self, data): |
|||
if self.firstTag == None: |
|||
self.firstTag = data |
|||
|
|||
def histogram(seznam): |
|||
d = {} |
|||
for i in seznam: |
|||
if i not in d: |
|||
d[i] = 0 |
|||
d[i] += 1 |
|||
return d |
|||
|
|||
|
|||
roman_numerals = zip((1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1), |
|||
('M', 'CM', 'D', 'CD','C', 'XC','L','XL','X','IX','V','IV','I')) |
|||
|
|||
def roman(num): |
|||
res = "" |
|||
for i, n in roman_numerals: |
|||
res += n * (num // i) |
|||
num %= i |
|||
return res |
|||
|
|||
def from_roman(rom): |
|||
if not rom: |
|||
return 0 |
|||
for i, n in roman_numerals: |
|||
if rom.upper().startswith(n): |
|||
return i + from_roman(rom[len(n):]) |
|||
raise Exception('Invalid roman numeral: "%s"', rom) |
|||
|
|||
|
|||
def seznam_problemu(): |
|||
problemy = [] |
|||
|
|||
# Pomocna fce k formatovani problemovych hlasek |
|||
def prb(cls, msg, objs=None): |
|||
s = u'<b>%s:</b> %s' % (cls.__name__, msg) |
|||
if objs: |
|||
s += u' [' |
|||
for o in objs: |
|||
try: |
|||
url = o.admin_url() |
|||
except: |
|||
url = None |
|||
if url: |
|||
s += u'<a href="%s">%s</a>, ' % (url, o.pk, ) |
|||
else: |
|||
s += u'%s, ' % (o.pk, ) |
|||
s = s[:-2] + u']' |
|||
problemy.append(s) |
|||
|
|||
# Duplicita jmen |
|||
jmena = {} |
|||
for r in m.Resitel.objects.all(): |
|||
j = r.plne_jmeno() |
|||
if j not in jmena: |
|||
jmena[j] = [] |
|||
jmena[j].append(r) |
|||
for j in jmena: |
|||
if len(jmena[j]) > 1: |
|||
prb(m.Resitel, u'Duplicitní jméno "%s"' % (j, ), jmena[j]) |
|||
|
|||
# Data maturity a narození |
|||
for r in m.Resitel.objects.all(): |
|||
if not r.rok_maturity: |
|||
prb(m.Resitel, u'Neznámý rok maturity', [r]) |
|||
if r.rok_maturity and (r.rok_maturity < 1990 or r.rok_maturity > datetime.date.today().year + 10): |
|||
prb(m.Resitel, u'Podezřelé datum maturity', [r]) |
|||
if r.datum_narozeni and (r.datum_narozeni.year < 1970 or r.datum_narozeni.year > datetime.date.today().year - 12): |
|||
prb(m.Resitel, u'Podezřelé datum narození', [r]) |
|||
# if not r.email: |
|||
# prb(Resitel, u'Neznámý email', [r]) |
|||
|
|||
return problemy |
|||
|
|||
|
Loading…
Reference in new issue