Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Move confirmation logic into quasi formset

Krzysiek Szularz42d62fec5762
diff --git a/registration/forms.py b/registration/forms.py
index ef8a6ed75c..0c3214e945 100644
--- a/registration/forms.py
+++ b/registration/forms.py
@@ -1,7 +1,7 @@
from django import forms
from django.utils.translation import ugettext_lazy as _
-from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import get_user_model
+from django.contrib.auth.forms import AuthenticationForm, SetPasswordForm
User = get_user_model()
@@ -24,3 +24,33 @@ class RegisterForm(EmailForm):
if User.objects.filter(email=email).exists():
raise forms.ValidationError('Email already registered.')
return email
+
+
+class NoPasswordForm(forms.Form):
+
+ no_password = forms.BooleanField(initial=True, widget=forms.HiddenInput())
+
+
+class EmailConfirmationFormset(object):
+
+ def __init__(self, email_confirmation, data=None):
+ self.email_confirmation = email_confirmation
+ self.set_password_form = SetPasswordForm(user=False, data=data)
+ self.no_password_form = NoPasswordForm(data=data)
+
+ def no_password(self):
+ return self.no_password_form.cleaned_data.get('no_password')
+
+ def is_valid(self):
+ if self.no_password_form.is_valid() and self.no_password():
+ return True
+ else:
+ return self.set_password_form.is_valid()
+
+ def save(self):
+ user = self.email_confirmation.get_or_create_user()
+ self.email_confirmation.delete()
+ if not self.no_password():
+ self.set_password_form.user = user
+ user = self.set_password_form.save()
+ return user
diff --git a/registration/models.py b/registration/models.py
index 6da8052b87..a608b75d6e 100644
--- a/registration/models.py
+++ b/registration/models.py
@@ -31,7 +31,7 @@ class EmailConfirmation(models.Model):
valid_until = models.DateTimeField(
default=lambda: now() + timedelta(settings.ACCOUNT_ACTIVATION_DAYS))
- def get_confirmed_user(self):
+ def get_or_create_user(self):
"""Confirm that user owns this email address and return User insatnce.
"""
if self.external_user and self.external_user.user:
diff --git a/registration/templates/registration/login.html b/registration/templates/registration/login.html
index 437496fbd4..33f5af4abe 100644
--- a/registration/templates/registration/login.html
+++ b/registration/templates/registration/login.html
@@ -34,5 +34,4 @@
<p>
<a href="{% url 'registration:register' %}">Create an account or reset password</a>
</p>
-
{% endblock %}
diff --git a/registration/templates/registration/set_password.html b/registration/templates/registration/set_password.html
index 6cb91b6ee0..c1e2d37f9a 100644
--- a/registration/templates/registration/set_password.html
+++ b/registration/templates/registration/set_password.html
@@ -1,16 +1,19 @@
{% extends "base.html" %}
+{% load bootstrap %}
+
{% block title %}Set password{% endblock %}
+
{% block content %}
- <div>
- <form method='post' action='.'>
- {% csrf_token %}
- {{ form }}
- <input type="submit" value="Set password">
- </form>
- <form method='post' action='.'>
- {% csrf_token %}
- <input type="hidden" name="nopassword">
- <input type="submit" value="I don't want to set password now">
- </form>
- </div>
+<div>
+ <form method="POST">
+ {% csrf_token %}
+ {{ formset.set_password_form|as_bootstrap }}
+ <input type="submit" value="Set password">
+ </form>
+ <form method="POST">
+ {% csrf_token %}
+ {{ formset.no_password_form }}
+ <input type="submit" value="I don't want to set password now">
+ </form>
+</div>
{% endblock %}
diff --git a/registration/tests.py b/registration/tests.py
index 17994ddffb..13922c6c10 100644
--- a/registration/tests.py
+++ b/registration/tests.py
@@ -350,7 +350,7 @@ class ConfirmationEmailTest(TestCase):
response = self.client.post(
'/account/confirm_email/%s/%s/' % (ec.id, ec.token),
- {'nopassword': None})
+ {'no_password': True})
user = User.objects.get(email=email)
self.assertFalse(EmailConfirmation.objects.filter(pk=ec.pk).exists())
@@ -391,7 +391,7 @@ class ConfirmationEmailTest(TestCase):
response = self.client.post(
'/account/confirm_email/%s/%s/' % (ec.id, ec.token),
- {'nopassword': None})
+ {'no_password': True})
self.assertFalse(EmailConfirmation.objects.filter(pk=ec.pk).exists())
self.assertTrue(User.objects.get(email=email).check_password(password))
diff --git a/registration/views.py b/registration/views.py
index 8bce9aabb6..028371623b 100644
--- a/registration/views.py
+++ b/registration/views.py
@@ -10,14 +10,13 @@ from django.contrib.auth import (
authenticate,
get_user_model,
)
-from django.contrib.auth.forms import SetPasswordForm
from django.core.urlresolvers import reverse
from django.core.mail.message import EmailMessage
from django.http import HttpResponseNotFound, HttpResponseBadRequest
from django.shortcuts import redirect
from django.template.response import TemplateResponse
-from .forms import LoginForm, EmailForm
+from .forms import LoginForm, EmailForm, EmailConfirmationFormset
from .models import ExternalUserData, EmailConfirmation
from .utils import (
facebook_callback,
@@ -62,9 +61,8 @@ def oauth_callback(request, service):
if user:
auth_login(request, user)
- messages.success(
- request,
- 'You have been successfully logged in.')
+ msg = 'You have been successfully logged in.'
+ messages.success(request, msg)
return redirect('home')
else:
request.session['confirmed_email'] = email
@@ -105,9 +103,8 @@ def register(request):
external_user.save()
user = authenticate(user=user)
auth_login(request, user)
- messages.success(
- request,
- 'You have been successfully logged in.')
+ msg = 'You have been successfully logged in.'
+ messages.success(request, msg)
else:
email_confirmation = EmailConfirmation.objects.create(
email=submitted_email, external_user=external_user)
@@ -118,7 +115,7 @@ def register(request):
messages.warning(
request,
'We have sent you a confirmation email. '
- 'Please check your email.')
+ 'Please check your inbox.')
return redirect('home')
return TemplateResponse(request, 'registration/register.html',
@@ -133,33 +130,20 @@ def confirm_email(request, pk, token):
except EmailConfirmation.DoesNotExist:
return TemplateResponse(request, 'registration/invalid_token.html')
- proceed = False
- password = None
-
if request.method == 'GET':
- form = SetPasswordForm(user=None)
+ formset = EmailConfirmationFormset(
+ email_confirmation=email_confirmation)
+
elif request.method == 'POST':
- if "nopassword" in request.POST:
- proceed = True
- else:
- form = SetPasswordForm(user=None, data=request.POST)
- if form.is_valid():
- proceed = True
- password = form.cleaned_data['new_password1']
-
- if proceed:
- user = email_confirmation.get_confirmed_user()
- if password:
- user.set_password(password)
- user.save()
- email_confirmation.delete()
-
- user = authenticate(user=user)
- auth_login(request, user)
- messages.success(
- request,
- 'You have been successfully logged in, %s' % (user.email,))
- return redirect('home')
- else:
- return TemplateResponse(
- request, 'registration/set_password.html', {'form': form})
+ formset = EmailConfirmationFormset(
+ email_confirmation=email_confirmation, data=request.POST)
+ if formset.is_valid():
+ user = formset.save()
+ user = authenticate(user=user)
+ auth_login(request, user)
+ msg = 'You have been successfully logged in.'
+ messages.success(request, msg)
+ return redirect('home')
+
+ return TemplateResponse(
+ request, 'registration/set_password.html', {'formset': formset})