diff options
Diffstat (limited to 'accounts')
-rw-r--r-- | accounts/templates/accounts/signup.html | 7 | ||||
-rw-r--r-- | accounts/views.py | 57 |
2 files changed, 48 insertions, 16 deletions
diff --git a/accounts/templates/accounts/signup.html b/accounts/templates/accounts/signup.html index 0decf9e..36ea568 100644 --- a/accounts/templates/accounts/signup.html +++ b/accounts/templates/accounts/signup.html @@ -11,6 +11,7 @@ <form method="post"> {% csrf_token %} {{ form.as_p }} + <p>La password deve essere di almeno 8 caretteri e contenere lettere</p> <br> <label> <input name="terms_accept" type="checkbox" class="filled-in"/> @@ -27,9 +28,9 @@ {% endblock %} {% block script %} -{% if error %} document.addEventListener('DOMContentLoaded', function() { - M.toast({html: '{{ error_text }}', classes: 'orange'}) +{% for error in errors %} + M.toast({html: '{{ error }}', classes: 'orange'}) +{% endfor %} }); -{% endif %} {% endblock %}
\ No newline at end of file diff --git a/accounts/views.py b/accounts/views.py index af7527e..06b3306 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -8,6 +8,9 @@ from django.http import FileResponse from django.contrib.auth.decorators import login_required from django.views.decorators.debug import sensitive_variables from django.http import HttpResponseRedirect +from django import forms +from django.contrib.auth.models import User +from django.core.exceptions import ValidationError from client.models import UserCode @@ -22,33 +25,61 @@ from pdf2image.exceptions import ( PDFSyntaxError ) +# override to remove help text +class RegisterForm(UserCreationForm): + def __init__(self, *args, **kwargs): + super(UserCreationForm, self).__init__(*args, **kwargs) + + for fieldname in ['username', 'password1', 'password2']: + self.fields[fieldname].help_text = None + @sensitive_variables("raw_passsword") def signup(request): + out_errors = [] # signup form with terms if request.method == 'POST': + # get form object + form = RegisterForm(request.POST) + + # check if terms are accepted if "terms_accept" not in request.POST: - # if terms not accepted return error and form again - form = UserCreationForm() - context = { - "form": form, - "error": True, - "error_text": "Accettare i termini e condizioni prego" - } - return render(request, 'accounts/signup.html', context) - # terms accepted create user in db - form = UserCreationForm(request.POST) - if form.is_valid(): + out_errors.append("Accettare i termini e condizioni prego") + + # if form is valid and terms were accepted save user + if form.is_valid() and len(out_errors) == 0: form.save() username = form.cleaned_data.get('username') raw_password = form.cleaned_data.get('password1') user = authenticate(username=username, password=raw_password) login(request, user) return HttpResponseRedirect('/') + else: + # get errors from form and add toasts + errors = form.errors.as_data() + for field in errors.keys(): + if field == "username": + out_errors.append("Il nome utente può contenere solo lettere e numeri") + else: + password_errors = errors["password2"] + for err in password_errors: + if err.code == "password_mismatch": + out_errors.append("Le due password non sono uguali") + elif err.code == "password_too_similar": + out_errors.append("La password è troppo simile all'username") + elif err.code == "password_too_short": + out_errors.append("La password è troppo corta") + elif err.code == "password_too_common": + out_errors.append("La password è troppo comune") + elif err.code == "password_entirely_numeric": + out_errors.append("La password deve contenere lettere") + + else: + # create empty form to be filled + form = RegisterForm() - # create empty form to be filled - form = UserCreationForm() context = { "form": form, + "errors": out_errors, } return render(request, 'accounts/signup.html', context) |