From 50933773088ed63ab631a86ccf806257379e2023 Mon Sep 17 00:00:00 2001 From: Sergio Oliveira Date: Thu, 9 Apr 2015 19:07:25 -0300 Subject: [PATCH] Moved email validation to user creation form --- colab/accounts/forms.py | 36 ++++++++++++++++++++---------------- colab/accounts/tests/test_forms.py | 12 +++++++----- colab/utils/conf.py | 4 +--- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/colab/accounts/forms.py b/colab/accounts/forms.py index 0c6f3c5..7c96cbf 100644 --- a/colab/accounts/forms.py +++ b/colab/accounts/forms.py @@ -45,10 +45,10 @@ class UserForm(forms.ModelForm): # Forces username to be lowercase always widget=forms.TextInput(attrs={'style': 'text-transform: lowercase;'}), ) - required = ('first_name', 'last_name', 'username', 'email') + required = ('first_name', 'last_name', 'username') class Meta: - fields = ('first_name', 'last_name', 'username', 'email') + fields = ('first_name', 'last_name', 'username') model = User def __init__(self, *args, **kwargs): @@ -61,20 +61,6 @@ class UserForm(forms.ModelForm): if field_name in UserForm.required: field.required = True - def clean_email(self): - email = self.cleaned_data.get('email') - username = self.cleaned_data.get('username') - - user_qs = User.objects.filter(email=email).exclude(username=username) - - if email and user_qs.exists(): - url = reverse('login') - msg = ("Email already used. Is it you?" - " Please login").format(url) - raise forms.ValidationError(mark_safe(msg)) - - return email - def clean_username(self): username = self.cleaned_data["username"].strip() if not username: @@ -207,7 +193,10 @@ class UserCreationForm(UserForm): A form that creates a user, with no privileges, from the given username and password. """ + error_messages = { + 'duplicate_email': _("Email already used. Is it you? " + " Please login"), 'duplicate_username': _("A user with that username already exists."), 'password_mismatch': _("The two password fields didn't match."), } @@ -235,6 +224,21 @@ class UserCreationForm(UserForm): model = User fields = ("username", "first_name", "last_name", "email") + def clean_email(self): + email = self.cleaned_data.get('email') + username = self.cleaned_data.get('username') + + user_qs = User.objects.filter(email=email).exclude(username=username) + + if email and user_qs.exists(): + msg = self.error_messages.get('duplicate_email') % { + 'url': reverse('login') + } + + raise forms.ValidationError(mark_safe(msg)) + + return email + def clean_username(self): # Since User.username is unique, this check is redundant, # but it sets a nicer error message than the ORM. See #13147. diff --git a/colab/accounts/tests/test_forms.py b/colab/accounts/tests/test_forms.py index be6a323..1358e1c 100644 --- a/colab/accounts/tests/test_forms.py +++ b/colab/accounts/tests/test_forms.py @@ -5,8 +5,9 @@ Objective: Test parameters, and behavior. from re import search from django.test import TestCase +from django.core.urlresolvers import reverse -from colab.accounts.forms import UserForm +from colab.accounts.forms import UserCreationForm from colab.accounts.models import User @@ -31,7 +32,7 @@ class FormTest(TestCase): 'username': 'colab', 'password1': '123colab4', 'password2': '123colab4'} - form = UserForm(data=form_data) + form = UserCreationForm(data=form_data) return form def test_already_registered_email(self): @@ -39,10 +40,11 @@ class FormTest(TestCase): self.assertFalse(form.is_valid()) def test_registered_email_message(self): - tryToFind = r"sign in" form = self.create_form_data() - matched = search(tryToFind, str(form)) - self.assertIsNotNone(matched) + msg = form.error_messages.get('duplicate_email') % { + 'url': reverse('login') + } + self.assertIn(msg, str(form)) def tearDown(self): pass diff --git a/colab/utils/conf.py b/colab/utils/conf.py index ccf1ade..b63e306 100644 --- a/colab/utils/conf.py +++ b/colab/utils/conf.py @@ -1,13 +1,11 @@ import os import sys - +import importlib import warnings from django.core.exceptions import ImproperlyConfigured -import importlib - class InaccessibleSettings(ImproperlyConfigured): """Settings.py is Inaccessible. -- libgit2 0.21.2