Browse Source

Add Generování hesla v registraci.

middleware_test
Jonas Havelka 3 years ago
parent
commit
8572264845
  1. 13
      seminar/forms.py
  2. 4
      seminar/templates/seminar/profil/prihlaska.html
  3. 21
      seminar/views/views_all.py

13
seminar/forms.py

@ -1,5 +1,6 @@
from django import forms from django import forms
from dal import autocomplete from dal import autocomplete
from django.contrib.auth.forms import PasswordResetForm
from django.core.exceptions import ObjectDoesNotExist from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.forms import formset_factory from django.forms import formset_factory
@ -37,21 +38,11 @@ class LoginForm(forms.Form):
widget=forms.PasswordInput()) widget=forms.PasswordInput())
class PrihlaskaForm(forms.Form): class PrihlaskaForm(PasswordResetForm):
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')
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) jmeno = forms.CharField(label='Jméno', max_length=256, required=True)
prijmeni = forms.CharField(label='Příjmení', max_length=256, required=True) prijmeni = forms.CharField(label='Příjmení', max_length=256, required=True)

4
seminar/templates/seminar/profil/prihlaska.html

@ -33,8 +33,8 @@
</h4> </h4>
<table class="form"> <table class="form">
{% include "seminar/profil/prihlaska_field.html" with field=form.username %} {% include "seminar/profil/prihlaska_field.html" with field=form.username %}
{% include "seminar/profil/prihlaska_field.html" with field=form.password %} {# {% include "seminar/profil/prihlaska_field.html" with field=form.password %}#}
{% include "seminar/profil/prihlaska_field.html" with field=form.password_check %} {# {% include "seminar/profil/prihlaska_field.html" with field=form.password_check %}#}
</table> </table>
<hr> <hr>

21
seminar/views/views_all.py

@ -1,10 +1,12 @@
from django.contrib.auth.tokens import PasswordResetTokenGenerator
from django.contrib.sites.shortcuts import get_current_site
from django.shortcuts import get_object_or_404, render, redirect from django.shortcuts import get_object_or_404, render, redirect
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden, JsonResponse from django.http import HttpResponse, HttpResponseRedirect, HttpResponseForbidden, JsonResponse
from django.urls import reverse,reverse_lazy from django.urls import reverse,reverse_lazy
from django.core.exceptions import PermissionDenied, ObjectDoesNotExist from django.core.exceptions import PermissionDenied, ObjectDoesNotExist
from django.core.mail import send_mail from django.core.mail import send_mail
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from django.views import generic from django.views import generic
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.http import Http404,HttpResponseBadRequest,HttpResponseRedirect from django.http import Http404,HttpResponseBadRequest,HttpResponseRedirect
@ -1189,7 +1191,6 @@ def prihlaskaView(request):
with transaction.atomic(): with transaction.atomic():
u = User.objects.create_user( u = User.objects.create_user(
username=fcd['username'], username=fcd['username'],
password=fcd['password'],
email = fcd['email']) email = fcd['email'])
u.save() u.save()
resitel_perm = Permission.objects.filter(codename__exact='resitel').first() resitel_perm = Permission.objects.filter(codename__exact='resitel').first()
@ -1240,11 +1241,19 @@ def prihlaskaView(request):
err_logger.warn(msg + str(form_hash)) err_logger.warn(msg + str(form_hash))
r.save() r.save()
send_mail( uid = urlsafe_base64_encode(force_bytes(u.pk))
token = PasswordResetTokenGenerator().make_token(u)
url = "https://%s%s" % (
str(get_current_site(request)),
str(reverse_lazy("password_reset_confirm", args=[uid, token]))
)
u.email_user(
subject="Registrace na M&M", subject="Registrace na M&M",
message=f"Tento e-mail byl právě zaregistrován na mam.matfyz.cz.", # TODO: Dovymyslet text. message=f"Tento e-mail byl právě zaregistrován na mam.matfyz.cz. Nové heslo si nastavíš na: " + url,
# TODO: templates/seminar/registrace a django/contrib/auth/forms.py říkají, jak na to lépe
# TODO: Dovymyslet text.
from_email="registrace@mam.mff.cuni.cz", # FIXME: Chceme to mít radši tady, nebo v nastavení? from_email="registrace@mam.mff.cuni.cz", # FIXME: Chceme to mít radši tady, nebo v nastavení?
recipient_list=list([fcd['email']]),
) )
return formularOKView(request) return formularOKView(request)

Loading…
Cancel
Save