From ba7992e7be1c803b1d98ef5042ad1e58a3f0c8eb Mon Sep 17 00:00:00 2001 From: Yir Date: Sun, 20 Mar 2022 22:43:38 +0100 Subject: [PATCH] Preserve registration form fields that passed error check when the registration failed. --- src/accounts/templates/accounts/register.html | 68 +++++++++---------- src/accounts/views/views.py | 61 +++++++++++++---- 2 files changed, 83 insertions(+), 46 deletions(-) diff --git a/src/accounts/templates/accounts/register.html b/src/accounts/templates/accounts/register.html index 7b39bf5..77fe4e1 100644 --- a/src/accounts/templates/accounts/register.html +++ b/src/accounts/templates/accounts/register.html @@ -2,42 +2,42 @@ {% block content %} {% load i18n %}
-
-
-
-

Register

-
-
-
- {% if registered %} -

{% trans "Welcome !" %}

- {% trans "Your account has been created." %} - {% else %} - {% if registered_failed %} - {{ registered_failed }} - {% endif %} -
- {% csrf_token %} -
-
-
-
-
- -
-
- {% endif %} -
-
-
+
+
+
+

Register

+
+
+
+ {% if registered %} +

{% trans "Welcome !" %}

+ {% trans "Your account has been created." %} + {% else %} + {% if registered_failed %} + {{ registered_failed }} + {% endif %} +
+ {% csrf_token %} +
+
+
+
+
+ +
+
+ {% endif %} +
- +
+ +
{% endblock %} diff --git a/src/accounts/views/views.py b/src/accounts/views/views.py index 392e6ba..37a97ce 100644 --- a/src/accounts/views/views.py +++ b/src/accounts/views/views.py @@ -43,32 +43,69 @@ def signup(request): user_form = UserForm() profile_form = UserProfileInfoForm() registered = False + if request.method == 'POST': - pass1 = request.POST.get('password') - if len(pass1) < 8: - return render(request,'accounts/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered_failed':"The new password must be at least %d characters long." % 8}) - first_isalpha = pass1[0].isalpha() - if not any(c.isdigit() for c in pass1) or not any(c.isalpha() for c in pass1): - return render(request,'accounts/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered_failed':_("The password must contain at least one letter and at least one digit or punctuation character.")}) + username = request.POST.get('username') + passwd = request.POST.get('password') + email = request.POST.get('email') + website = request.POST.get('portfolio_site') + + if len(passwd) < 8: + return render(request, 'accounts/register.html', { + 'user_form': user_form, + 'profile_form': profile_form, + 'registered_failed': _("The password must be at least 8 characters long."), + 'old_username': username, + 'old_email': email, + 'old_website': website + }) + + if not any(c.isdigit() for c in passwd) or not any(c.isalpha() for c in passwd): + return render(request, 'accounts/register.html', { + 'user_form': user_form, + 'profile_form': profile_form, + 'registered_failed': _("The password must contain at least one letter and at least one digit or punctuation character."), + 'old_username': username, + 'old_email': email, + 'old_website': website + }) + if User.objects.filter(email=request.POST.get('email')).exists(): - return render(request,'accounts/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered_failed':_("A user with that email already exists.")}) + return render(request, 'accounts/register.html', { + 'user_form': user_form, + 'profile_form': profile_form, + 'registered_failed': _("A user with that email already exists."), + 'old_username': username, + 'old_website': website + }) + user_form = UserForm(data=request.POST) profile_form = UserProfileInfoForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() + profile = profile_form.save(commit=False) profile.user = user profile.token = token_hex(16) profile.save() + registered = True else: - return render(request,'accounts/register.html', {'user_form':user_form, 'profile_form':profile_form, 'registered_failed':_("A user with that username already exists.")}) - return render(request,'accounts/register.html', - {'user_form':user_form, - 'profile_form':profile_form, - 'registered':registered}) + return render(request, 'accounts/register.html', { + 'user_form': user_form, + 'profile_form': profile_form, + 'registered_failed': _("A user with that username already exists."), + 'old_email': email, + 'old_website': website + }) + + return render(request, 'accounts/register.html', { + 'user_form': user_form, + 'profile_form': profile_form, + 'registered': registered + }) else: return HttpResponseRedirect(reverse('home'))