Browse Source

Logovani registracniho formulare.

export_seznamu_prednasek
parent
commit
d407f27ed9
  1. 37
      seminar/forms.py
  2. 87
      seminar/views.py

37
seminar/forms.py

@ -1,9 +1,12 @@
from django import forms from django import forms
from dal import autocomplete from dal import autocomplete
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User
from .models import Skola, Resitel from .models import Skola, Resitel, Osoba
from datetime import date from datetime import date
import logging
class LoginForm(forms.Form): class LoginForm(forms.Form):
username = forms.CharField(label='Přihlašovací jméno', username = forms.CharField(label='Přihlašovací jméno',
@ -71,8 +74,37 @@ class PrihlaskaForm(forms.Form):
zasilat = forms.ChoiceField(label='Kam zasílat čísla a řešení',choices = Resitel.ZASILAT_CHOICES, 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) 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) 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): def clean(self):
super().clean() super().clean()
err_logger = logging.getLogger('seminar.prihlaska.problem')
data = self.cleaned_data data = self.cleaned_data
if data.get('password') != data.get('password_check'): if data.get('password') != data.get('password_check'):
self.add_error('password_check',forms.ValidationError('Hesla se neshodují')) self.add_error('password_check',forms.ValidationError('Hesla se neshodují'))
@ -87,6 +119,3 @@ class PrihlaskaForm(forms.Form):
self.add_error('skola_nazev',forms.ValidationError('Je nutné vyplnit název školy')) self.add_error('skola_nazev',forms.ValidationError('Je nutné vyplnit název školy'))
elif data.get('skola_adresa')=='': elif data.get('skola_adresa')=='':
self.add_error('skola_adresa',forms.ValidationError('Je nutné vyplnit adresu školy')) self.add_error('skola_adresa',forms.ValidationError('Je nutné vyplnit adresu školy'))
from seminar.models import Resitel

87
seminar/views.py

@ -10,6 +10,7 @@ from django.http import Http404,HttpResponseBadRequest,HttpResponseRedirect
from django.db.models import Q from django.db.models import Q
from django.views.decorators.csrf import ensure_csrf_cookie from django.views.decorators.csrf import ensure_csrf_cookie
from django.contrib.auth import authenticate, login, get_user_model, logout from django.contrib.auth import authenticate, login, get_user_model, logout
from django.contrib.auth.models import User
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.db import transaction from django.db import transaction
from dal import autocomplete from dal import autocomplete
@ -1025,83 +1026,75 @@ def logoutView(request):
return render(request, 'seminar/login.html', {'form': form}) return render(request, 'seminar/login.html', {'form': form})
def prihlaska_log_gdpr_safe(logger, gdpr_logger, msg, form_data):
msg = "{}, form_hash:{}".format(msg,hash(form_data))
logger.warn(msg)
gdpr_logger.warn(msg+", form:{}".format(form_data))
def prihlaskaView(request): def prihlaskaView(request):
logger = logging.getLogger('seminar.prihlaska') generic_logger = logging.getLogger('seminar.prihlaska')
err_logger = logging.getLogger('seminar.prihlaska.problem')
form_logger = logging.getLogger('seminar.prihlaska.form')
if request.method == 'POST': if request.method == 'POST':
form = PrihlaskaForm(request.POST) form = PrihlaskaForm(request.POST)
# TODO vyresit, co se bude v jakych situacich zobrazovat # TODO vyresit, co se bude v jakych situacich zobrazovat
if form.is_valid(): if form.is_valid():
print("Form valid") generic_logger.info("Form valid")
try: fcd = form.cleaned_data
# mame jiz email v databazi? form_hash = hash(fcd)
o = Osoba.objects.get(email=form.cleaned_data['email']) form_logger.info(fcd,form_hash=form_hash)
print("Email existuje: {}".format(form.cleaned_data))
# TODO seřvat a nepustit dál
return HttpResponseRedirect('/thanks/')
except ObjectDoesNotExist:
pass
User = get_user_model()
try:
u = User.objects.get(username=form.cleaned_data['username'])
print("Username existuje: {}".format(form.cleaned_data))
# TODO seřvat a nepustit dál
return HttpResponseRedirect('/thanks/')
except ObjectDoesNotExist:
pass
with transaction.atomic(): with transaction.atomic():
u = User.objects.create_user( u = User.objects.create_user(
username=form.cleaned_data['username'], username=fcd['username'],
password=form.cleaned_data['password'], password=fcd['password'],
email = form.cleaned_data['email']) email = fcd['email'])
u.save() u.save()
o = Osoba( o = Osoba(
jmeno = form.cleaned_data['jmeno'], jmeno = fcd['jmeno'],
prijmeni = form.cleaned_data['prijmeni'], prijmeni = fcd['prijmeni'],
pohlavi_muz = form.cleaned_data['pohlavi_muz'], pohlavi_muz = fcd['pohlavi_muz'],
email = form.cleaned_data['email'], email = fcd['email'],
telefon = form.cleaned_data.get('telefon',''), telefon = fcd.get('telefon',''),
datum_narozeni = form.cleaned_data.get('datum_narozeni',None), datum_narozeni = fcd.get('datum_narozeni',None),
datum_souhlasu_udaje = date.today(), datum_souhlasu_udaje = date.today(),
datum_registrace = date.today(), datum_registrace = date.today(),
ulice = form.cleaned_data.get('ulice',''), ulice = fcd.get('ulice',''),
mesto = form.cleaned_data.get('mesto',''), mesto = fcd.get('mesto',''),
psc = form.cleaned_data.get('psc',''), psc = fcd.get('psc',''),
poznamka = str(form.cleaned_data) poznamka = str(fcd)
) )
if form.cleaned_data.get('spam',False): if fcd.get('spam',False):
o.datum_souhlasu_zasilani = date.today() o.datum_souhlasu_zasilani = date.today()
if form.cleaned_data.get('stat','') in ('CZ','SK'): if fcd.get('stat','') in ('CZ','SK'):
o.stat = form.cleaned_data['stat'] o.stat = fcd['stat']
else: else:
pass # Unknown country - log it
#TODO jak budeme resit jine staty? msg = "Unknown country {}".format(fcd['stat_text'])
err_logger.warn(msg,form_hash=form_hash)
o.save() o.save()
o.user = u o.user = u
o.save() o.save()
r = Resitel( r = Resitel(
rok_maturity = form.cleaned_data['rok_maturity'], rok_maturity = fcd['rok_maturity'],
zasilat = form.cleaned_data['zasilat'] zasilat = fcd['zasilat']
) )
r.save() r.save()
r.osoba = o r.osoba = o
if form.cleaned_data.get('skola'): if fcd.get('skola'):
r.skola = form.cleaned_data['skola'] r.skola = fcd['skola']
else: else:
pass # Unknown school - log it
#TODO doplnit skolu, kdyz neni v seznamu msg = "Unknown school {}, {}".format(fcd['skola_nazev'],fcd['skola_adresa'])
err_logger.warn(msg,form_hash=form_hash)
r.save() r.save()
# TODO logovat jednotlive validni formulare do souboru
print(form.cleaned_data)
logger.info(form.cleaned_data)
return HttpResponseRedirect('/thanks/') return HttpResponseRedirect('/thanks/')
# if a GET (or any other method) we'll create a blank form # if a GET (or any other method) we'll create a blank form

Loading…
Cancel
Save