|
|
|
from django import forms
|
|
|
|
from dal import autocomplete
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
|
|
from .models import Skola, Resitel, Osoba
|
|
|
|
|
|
|
|
from datetime import date
|
|
|
|
import logging
|
|
|
|
|
|
|
|
class LoginForm(forms.Form):
|
|
|
|
username = forms.CharField(label='Přihlašovací jméno',
|
|
|
|
max_length=256,
|
|
|
|
required=True)
|
|
|
|
password = forms.CharField(
|
|
|
|
label='Heslo',
|
|
|
|
max_length=256,
|
|
|
|
required=True,
|
|
|
|
widget=forms.PasswordInput())
|
|
|
|
|
|
|
|
|
|
|
|
class PrihlaskaForm(forms.Form):
|
|
|
|
username = forms.CharField(label='Přihlašovací jméno',
|
|
|
|
max_length=256,
|
|
|
|
required=True,
|
|
|
|
help_text='Tímto jménem se následně budeš přihlašovat pro odevzdání řešení a další činnosti v semináři')
|
|
|
|
password = forms.CharField(
|
|
|
|
label='Heslo',
|
|
|
|
max_length=256,
|
|
|
|
required=True,
|
|
|
|
widget=forms.PasswordInput())
|
|
|
|
password_check = forms.CharField(
|
|
|
|
label='Ověření hesla',
|
|
|
|
max_length=256,
|
|
|
|
required=True,
|
|
|
|
widget=forms.PasswordInput())
|
|
|
|
|
|
|
|
jmeno = forms.CharField(label='Jméno', max_length=256, required=True)
|
|
|
|
prijmeni = forms.CharField(label='Příjmení', max_length=256, required=True)
|
|
|
|
pohlavi_muz = forms.ChoiceField(label='Pohlaví',
|
|
|
|
choices = ((True,'muž'),(False,'žena')), required=True)
|
|
|
|
email = forms.EmailField(label='E-mail',max_length=256, required=True)
|
|
|
|
telefon = forms.CharField(label='Telefon',max_length=256, required=False)
|
|
|
|
datum_narozeni = forms.DateField(label='Datum narození', required=False)
|
|
|
|
ulice = forms.CharField(label='Ulice', max_length=256, required=False)
|
|
|
|
mesto = forms.CharField(label='Město', max_length=256, required=False)
|
|
|
|
psc = forms.CharField(label='PSČ', max_length=32, required=False)
|
|
|
|
stat = forms.ChoiceField(label='Stát',
|
|
|
|
choices = (('CZ', 'Česká Republika'),
|
|
|
|
('SK', 'Slovenská Republika'),
|
|
|
|
('other', 'Jiné')),
|
|
|
|
required=False)
|
|
|
|
stat_text = forms.CharField(label='Stát', max_length=256, required=False)
|
|
|
|
|
|
|
|
skola = forms.ModelChoiceField(label="Škola",
|
|
|
|
queryset=Skola.objects.all(),
|
|
|
|
widget=autocomplete.ModelSelect2(
|
|
|
|
url='autocomplete_skola',
|
|
|
|
attrs = {'data-placeholder--id': '-1',
|
|
|
|
'data-placeholder--text' : '---',
|
|
|
|
'data-allow-clear': 'true'})
|
|
|
|
,required=False)
|
|
|
|
|
|
|
|
skola_nazev = forms.CharField(label='Název školy', max_length=256, required=False)
|
|
|
|
skola_adresa = forms.CharField(label='Adresa školy', max_length=256, required=False)
|
|
|
|
|
|
|
|
# trida = forms.CharField(label='Třída',max_length=10, required=True)
|
|
|
|
|
|
|
|
rok_maturity = forms.IntegerField(
|
|
|
|
label='Rok maturity',
|
|
|
|
min_value=date.today().year,
|
|
|
|
max_value=date.today().year+8,
|
|
|
|
required=True)
|
|
|
|
zasilat = forms.ChoiceField(label='Kam zasílat čísla a řešení',choices = Resitel.ZASILAT_CHOICES, required=True)
|
|
|
|
gdpr = forms.BooleanField(label='Souhlasím se zpracováním osobních údajů', required=True)
|
|
|
|
spam = forms.BooleanField(label='Souhlasím se zasíláním materiálů od MFF UK', required=False)
|
|
|
|
|
|
|
|
def clean_username(self):
|
|
|
|
err_logger = logging.getLogger('seminar.prihlaska.problem')
|
|
|
|
username = self.cleaned_data.get('username')
|
|
|
|
try:
|
|
|
|
User.objects.get(username=username)
|
|
|
|
msg = "Username {} exists".format(username)
|
|
|
|
err_logger.info(msg)
|
|
|
|
raise forms.ValidationError('Přihlašovací jméno je již použito')
|
|
|
|
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def clean_email(self):
|
|
|
|
err_logger = logging.getLogger('seminar.prihlaska.problem')
|
|
|
|
email = self.cleaned_data.get('email')
|
|
|
|
try:
|
|
|
|
Osoba.objects.get(email=email)
|
|
|
|
msg = "Email {} exists".format(email)
|
|
|
|
err_logger.info(msg)
|
|
|
|
raise forms.ValidationError('Email je již použit')
|
|
|
|
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
super().clean()
|
|
|
|
|
|
|
|
err_logger = logging.getLogger('seminar.prihlaska.problem')
|
|
|
|
|
|
|
|
data = self.cleaned_data
|
|
|
|
if data.get('password') != data.get('password_check'):
|
|
|
|
self.add_error('password_check',forms.ValidationError('Hesla se neshodují'))
|
|
|
|
if data.get('stat') != '' and data.get('stat_text') != '':
|
|
|
|
self.add_error('stat',forms.ValidationError('Nelze mít vybraný stát z menu a zároven zapsaný textem'))
|
|
|
|
if data.get('skola') and (data.get('skola_nazev') or data.get('skola_adresa')):
|
|
|
|
self.add_error('skola',forms.ValidationError('Pokud je škola v seznamu, nevypisujte ji ručně, pokud není, zrušte výběr ze seznamu (křížek vpravo)'))
|
|
|
|
if not data.get('skola'):
|
|
|
|
if data.get('skola_nazev')=='' and data.get('skola_adresa')=='':
|
|
|
|
self.add_error('skola',forms.ValidationError('Je nutné vyplnit školu'))
|
|
|
|
elif data.get('skola_nazev')=='':
|
|
|
|
self.add_error('skola_nazev',forms.ValidationError('Je nutné vyplnit název školy'))
|
|
|
|
elif data.get('skola_adresa')=='':
|
|
|
|
self.add_error('skola_adresa',forms.ValidationError('Je nutné vyplnit adresu školy'))
|