From 0f7b401a163f5281b40787eb96319f9bb4ba1dee Mon Sep 17 00:00:00 2001 From: "Tomas \"Jethro\" Pokorny" Date: Sun, 1 Sep 2019 22:59:05 +0200 Subject: [PATCH] Prihlaseni a odhlaseni uzivatele --- seminar/forms.py | 28 +++++++++++++++--- seminar/templates/seminar/login.html | 33 ++++++++++++++++++++++ seminar/templates/seminar/prihlaska.html | 18 ++++++++++-- seminar/urls.py | 7 +++-- seminar/views.py | 36 ++++++++++++++++++++++-- 5 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 seminar/templates/seminar/login.html diff --git a/seminar/forms.py b/seminar/forms.py index 4455515e..830203a2 100644 --- a/seminar/forms.py +++ b/seminar/forms.py @@ -5,17 +5,35 @@ from .models import Skola, Resitel 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): - 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', 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') - # 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í', choices = ((True,'muž'),(False,'žena')), required=True) email = forms.EmailField(label='E-mail',max_length=256, required=True) @@ -56,6 +74,8 @@ class PrihlaskaForm(forms.Form): def clean(self): super().clean() 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')): diff --git a/seminar/templates/seminar/login.html b/seminar/templates/seminar/login.html new file mode 100644 index 00000000..dbed7a17 --- /dev/null +++ b/seminar/templates/seminar/login.html @@ -0,0 +1,33 @@ +{% extends "seminar/zadani/base.html" %} +{% load staticfiles %} + + +{% block content %} +

+ {% block nadpis1a %}{% block nadpis1b %} + Přihlášení + {% endblock %}{% endblock %} +

+{% if login_error %} +{{login_error}} +{% endif %} +
+ {% csrf_token %} + {{form.non_field_errors}} +
    +
  • + {% include "seminar/prihlaska_field.html" with field=form.username %} +
  • +
  • + {% include "seminar/prihlaska_field.html" with field=form.password %} +
  • +
+ +
+ +Zapomněl jsem heslo
+Zaregistrovat
+ + +{% endblock %} + diff --git a/seminar/templates/seminar/prihlaska.html b/seminar/templates/seminar/prihlaska.html index d15c04d0..c77bd4fc 100644 --- a/seminar/templates/seminar/prihlaska.html +++ b/seminar/templates/seminar/prihlaska.html @@ -20,13 +20,25 @@ {{form.non_field_errors}}
  • - {% include "seminar/prihlaska_field.html" with field=form.jmeno %} + Přihlašovací údaje
  • - {% include "seminar/prihlaska_field.html" with field=form.prijmeni %} + {% include "seminar/prihlaska_field.html" with field=form.username %}
  • - {% include "seminar/prihlaska_field.html" with field=form.username %} + {% include "seminar/prihlaska_field.html" with field=form.password %} +
  • +
  • + {% include "seminar/prihlaska_field.html" with field=form.password_check %} +
  • +
  • + Osobní údaje +
  • +
  • + {% include "seminar/prihlaska_field.html" with field=form.jmeno %} +
  • +
  • + {% include "seminar/prihlaska_field.html" with field=form.prijmeni %}
  • {% include "seminar/prihlaska_field.html" with field=form.pohlavi_muz%} diff --git a/seminar/urls.py b/seminar/urls.py index a8d4ff76..b7e39f47 100644 --- a/seminar/urls.py +++ b/seminar/urls.py @@ -3,6 +3,7 @@ from django.contrib.auth.decorators import user_passes_test from . import views, export from .utils import staff_member_required 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) @@ -97,10 +98,12 @@ urlpatterns = [ staff_member_required(views.texUploadView), 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('autocomplete/skola/',views.SkolaAutocomplete.as_view(), name='autocomplete_skola') # Ceka na autocomplete v3 # path('autocomplete/organizatori/', # staff_member_required(views.OrganizatorAutocomplete.as_view()), diff --git a/seminar/views.py b/seminar/views.py index c7bff438..ee4dbc3a 100644 --- a/seminar/views.py +++ b/seminar/views.py @@ -9,7 +9,7 @@ from django.utils.translation import ugettext as _ from django.http import Http404,HttpResponseBadRequest,HttpResponseRedirect from django.db.models import Q 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 dal import autocomplete @@ -17,7 +17,7 @@ from .models import Problem, Cislo, Reseni, Nastaveni, Rocnik, Soustredeni, Orga #from .models import VysledkyZaCislo, VysledkyKCisluZaRocnik, VysledkyKCisluOdjakziva from . import utils from .unicodecsv import UnicodeWriter -from .forms import PrihlaskaForm +from .forms import PrihlaskaForm, LoginForm from datetime import timedelta, date, datetime from django.utils import timezone @@ -952,6 +952,33 @@ def texDownloadView(request, rocnik, cislo): return JsonResponse(response) ## 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): logger = logging.getLogger('seminar.prihlaska') @@ -980,7 +1007,10 @@ def prihlaskaView(request): pass 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() o = Osoba(