Prihlaseni a odhlaseni uzivatele
This commit is contained in:
parent
6ad9cc5e0c
commit
0f7b401a16
5 changed files with 110 additions and 12 deletions
|
@ -5,17 +5,35 @@ from .models import Skola, Resitel
|
||||||
|
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
|
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):
|
class PrihlaskaForm(forms.Form):
|
||||||
jmeno = forms.CharField(label='Jméno', max_length=256, required=True)
|
|
||||||
prijmeni = forms.CharField(label='Příjmení', max_length=256, required=True)
|
|
||||||
username = forms.CharField(label='Přihlašovací jméno',
|
username = forms.CharField(label='Přihlašovací jméno',
|
||||||
max_length=256,
|
max_length=256,
|
||||||
required=True,
|
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')
|
help_text='Tímto jménem se následně budeš přihlašovat pro odevzdání řešení a další činnosti v semináři')
|
||||||
# TODO heslo?
|
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í',
|
pohlavi_muz = forms.ChoiceField(label='Pohlaví',
|
||||||
choices = ((True,'muž'),(False,'žena')), required=True)
|
choices = ((True,'muž'),(False,'žena')), required=True)
|
||||||
email = forms.EmailField(label='E-mail',max_length=256, required=True)
|
email = forms.EmailField(label='E-mail',max_length=256, required=True)
|
||||||
|
@ -56,6 +74,8 @@ class PrihlaskaForm(forms.Form):
|
||||||
def clean(self):
|
def clean(self):
|
||||||
super().clean()
|
super().clean()
|
||||||
data = self.cleaned_data
|
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') != '':
|
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'))
|
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')):
|
if data.get('skola') and (data.get('skola_nazev') or data.get('skola_adresa')):
|
||||||
|
|
33
seminar/templates/seminar/login.html
Normal file
33
seminar/templates/seminar/login.html
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
{% extends "seminar/zadani/base.html" %}
|
||||||
|
{% load staticfiles %}
|
||||||
|
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<h1>
|
||||||
|
{% block nadpis1a %}{% block nadpis1b %}
|
||||||
|
Přihlášení
|
||||||
|
{% endblock %}{% endblock %}
|
||||||
|
</h1>
|
||||||
|
{% if login_error %}
|
||||||
|
<span class="field_error">{{login_error}}<span>
|
||||||
|
{% endif %}
|
||||||
|
<form action="/login/" method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{form.non_field_errors}}
|
||||||
|
<ul class="form">
|
||||||
|
<li>
|
||||||
|
{% include "seminar/prihlaska_field.html" with field=form.username %}
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
{% include "seminar/prihlaska_field.html" with field=form.password %}
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
<input type="submit" value="Přihlásit">
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<a href="/reset_password">Zapomněl jsem heslo</a><br>
|
||||||
|
<a href="/prihlaska">Zaregistrovat</a><br>
|
||||||
|
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
|
|
|
@ -19,15 +19,27 @@
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{form.non_field_errors}}
|
{{form.non_field_errors}}
|
||||||
<ul class="form">
|
<ul class="form">
|
||||||
|
<li>
|
||||||
|
Přihlašovací údaje
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
{% include "seminar/prihlaska_field.html" with field=form.username %}
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
{% include "seminar/prihlaska_field.html" with field=form.password %}
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
{% include "seminar/prihlaska_field.html" with field=form.password_check %}
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
Osobní údaje
|
||||||
|
</li>
|
||||||
<li>
|
<li>
|
||||||
{% include "seminar/prihlaska_field.html" with field=form.jmeno %}
|
{% include "seminar/prihlaska_field.html" with field=form.jmeno %}
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
{% include "seminar/prihlaska_field.html" with field=form.prijmeni %}
|
{% include "seminar/prihlaska_field.html" with field=form.prijmeni %}
|
||||||
</li>
|
</li>
|
||||||
<li>
|
|
||||||
{% include "seminar/prihlaska_field.html" with field=form.username %}
|
|
||||||
</li>
|
|
||||||
<li>
|
<li>
|
||||||
{% include "seminar/prihlaska_field.html" with field=form.pohlavi_muz%}
|
{% include "seminar/prihlaska_field.html" with field=form.pohlavi_muz%}
|
||||||
</li>
|
</li>
|
||||||
|
|
|
@ -3,6 +3,7 @@ from django.contrib.auth.decorators import user_passes_test
|
||||||
from . import views, export
|
from . import views, export
|
||||||
from .utils import staff_member_required
|
from .utils import staff_member_required
|
||||||
from django.views.generic.base import RedirectView
|
from django.views.generic.base import RedirectView
|
||||||
|
from django.contrib.auth import views as auth_views
|
||||||
|
|
||||||
staff_member_required = user_passes_test(lambda u: u.is_staff)
|
staff_member_required = user_passes_test(lambda u: u.is_staff)
|
||||||
|
|
||||||
|
@ -97,10 +98,12 @@ urlpatterns = [
|
||||||
staff_member_required(views.texUploadView),
|
staff_member_required(views.texUploadView),
|
||||||
name='seminar_tex_upload'
|
name='seminar_tex_upload'
|
||||||
),
|
),
|
||||||
path('prihlaska/',views.prihlaskaView),
|
path('prihlaska/',views.prihlaskaView, name='seminar_prihlaska'),
|
||||||
|
path('login/', views.loginView, name='login'),
|
||||||
|
path('logout/', views.logoutView, name='logout'),
|
||||||
|
path('autocomplete/skola/',views.SkolaAutocomplete.as_view(), name='autocomplete_skola'),
|
||||||
path('', views.TitulniStranaView.as_view(), name='titulni_strana'),
|
path('', views.TitulniStranaView.as_view(), name='titulni_strana'),
|
||||||
|
|
||||||
path('autocomplete/skola/',views.SkolaAutocomplete.as_view(), name='autocomplete_skola')
|
|
||||||
# Ceka na autocomplete v3
|
# Ceka na autocomplete v3
|
||||||
# path('autocomplete/organizatori/',
|
# path('autocomplete/organizatori/',
|
||||||
# staff_member_required(views.OrganizatorAutocomplete.as_view()),
|
# staff_member_required(views.OrganizatorAutocomplete.as_view()),
|
||||||
|
|
|
@ -9,7 +9,7 @@ from django.utils.translation import ugettext as _
|
||||||
from django.http import Http404,HttpResponseBadRequest,HttpResponseRedirect
|
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
|
from django.contrib.auth import authenticate, login, get_user_model, logout
|
||||||
from django.db import transaction
|
from django.db import transaction
|
||||||
from dal import autocomplete
|
from dal import autocomplete
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ from .models import Problem, Cislo, Reseni, Nastaveni, Rocnik, Soustredeni, Orga
|
||||||
#from .models import VysledkyZaCislo, VysledkyKCisluZaRocnik, VysledkyKCisluOdjakziva
|
#from .models import VysledkyZaCislo, VysledkyKCisluZaRocnik, VysledkyKCisluOdjakziva
|
||||||
from . import utils
|
from . import utils
|
||||||
from .unicodecsv import UnicodeWriter
|
from .unicodecsv import UnicodeWriter
|
||||||
from .forms import PrihlaskaForm
|
from .forms import PrihlaskaForm, LoginForm
|
||||||
|
|
||||||
from datetime import timedelta, date, datetime
|
from datetime import timedelta, date, datetime
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
@ -952,6 +952,33 @@ def texDownloadView(request, rocnik, cislo):
|
||||||
return JsonResponse(response)
|
return JsonResponse(response)
|
||||||
|
|
||||||
## Formulare
|
## Formulare
|
||||||
|
def loginView(request):
|
||||||
|
if request.method == 'POST':
|
||||||
|
form = LoginForm(request.POST)
|
||||||
|
if form.is_valid():
|
||||||
|
user = authenticate(request,
|
||||||
|
username=form.cleaned_data['username'],
|
||||||
|
password=form.cleaned_data['password'])
|
||||||
|
print(form.cleaned_data)
|
||||||
|
if user is not None:
|
||||||
|
login(request,user)
|
||||||
|
return HttpResponseRedirect('/')
|
||||||
|
else:
|
||||||
|
return render(request,
|
||||||
|
'seminar/login.html',
|
||||||
|
{'form': form, 'login_error': 'Neplatné jméno nebo heslo'})
|
||||||
|
|
||||||
|
else:
|
||||||
|
form = LoginForm()
|
||||||
|
return render(request, 'seminar/login.html', {'form': form})
|
||||||
|
|
||||||
|
def logoutView(request):
|
||||||
|
form = LoginForm()
|
||||||
|
if request.user.is_authenticated:
|
||||||
|
logout(request)
|
||||||
|
return render(request, 'seminar/login.html', {'form': form, 'login_error': 'Byli jste úspěšně odhlášeni'})
|
||||||
|
return render(request, 'seminar/login.html', {'form': form})
|
||||||
|
|
||||||
|
|
||||||
def prihlaskaView(request):
|
def prihlaskaView(request):
|
||||||
logger = logging.getLogger('seminar.prihlaska')
|
logger = logging.getLogger('seminar.prihlaska')
|
||||||
|
@ -980,7 +1007,10 @@ def prihlaskaView(request):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
u = User(username=form.cleaned_data['username'])
|
u = User.objects.create_user(
|
||||||
|
username=form.cleaned_data['username'],
|
||||||
|
password=form.cleaned_data['password'],
|
||||||
|
email = form.cleaned_data['email'])
|
||||||
u.save()
|
u.save()
|
||||||
|
|
||||||
o = Osoba(
|
o = Osoba(
|
||||||
|
|
Loading…
Reference in a new issue