Commit 50933773088ed63ab631a86ccf806257379e2023

Authored by Sergio Oliveira
1 parent 2bcbd241

Moved email validation to user creation form

colab/accounts/forms.py
... ... @@ -45,10 +45,10 @@ class UserForm(forms.ModelForm):
45 45 # Forces username to be lowercase always
46 46 widget=forms.TextInput(attrs={'style': 'text-transform: lowercase;'}),
47 47 )
48   - required = ('first_name', 'last_name', 'username', 'email')
  48 + required = ('first_name', 'last_name', 'username')
49 49  
50 50 class Meta:
51   - fields = ('first_name', 'last_name', 'username', 'email')
  51 + fields = ('first_name', 'last_name', 'username')
52 52 model = User
53 53  
54 54 def __init__(self, *args, **kwargs):
... ... @@ -61,20 +61,6 @@ class UserForm(forms.ModelForm):
61 61 if field_name in UserForm.required:
62 62 field.required = True
63 63  
64   - def clean_email(self):
65   - email = self.cleaned_data.get('email')
66   - username = self.cleaned_data.get('username')
67   -
68   - user_qs = User.objects.filter(email=email).exclude(username=username)
69   -
70   - if email and user_qs.exists():
71   - url = reverse('login')
72   - msg = ("Email already used. Is it you?"
73   - " Please <a href='{}'>login<a/>").format(url)
74   - raise forms.ValidationError(mark_safe(msg))
75   -
76   - return email
77   -
78 64 def clean_username(self):
79 65 username = self.cleaned_data["username"].strip()
80 66 if not username:
... ... @@ -207,7 +193,10 @@ class UserCreationForm(UserForm):
207 193 A form that creates a user, with no privileges, from the given username and
208 194 password.
209 195 """
  196 +
210 197 error_messages = {
  198 + 'duplicate_email': _("Email already used. Is it you? "
  199 + " Please <a href='%(url)s'>login</a>"),
211 200 'duplicate_username': _("A user with that username already exists."),
212 201 'password_mismatch': _("The two password fields didn't match."),
213 202 }
... ... @@ -235,6 +224,21 @@ class UserCreationForm(UserForm):
235 224 model = User
236 225 fields = ("username", "first_name", "last_name", "email")
237 226  
  227 + def clean_email(self):
  228 + email = self.cleaned_data.get('email')
  229 + username = self.cleaned_data.get('username')
  230 +
  231 + user_qs = User.objects.filter(email=email).exclude(username=username)
  232 +
  233 + if email and user_qs.exists():
  234 + msg = self.error_messages.get('duplicate_email') % {
  235 + 'url': reverse('login')
  236 + }
  237 +
  238 + raise forms.ValidationError(mark_safe(msg))
  239 +
  240 + return email
  241 +
238 242 def clean_username(self):
239 243 # Since User.username is unique, this check is redundant,
240 244 # but it sets a nicer error message than the ORM. See #13147.
... ...
colab/accounts/tests/test_forms.py
... ... @@ -5,8 +5,9 @@ Objective: Test parameters, and behavior.
5 5 from re import search
6 6  
7 7 from django.test import TestCase
  8 +from django.core.urlresolvers import reverse
8 9  
9   -from colab.accounts.forms import UserForm
  10 +from colab.accounts.forms import UserCreationForm
10 11 from colab.accounts.models import User
11 12  
12 13  
... ... @@ -31,7 +32,7 @@ class FormTest(TestCase):
31 32 'username': 'colab',
32 33 'password1': '123colab4',
33 34 'password2': '123colab4'}
34   - form = UserForm(data=form_data)
  35 + form = UserCreationForm(data=form_data)
35 36 return form
36 37  
37 38 def test_already_registered_email(self):
... ... @@ -39,10 +40,11 @@ class FormTest(TestCase):
39 40 self.assertFalse(form.is_valid())
40 41  
41 42 def test_registered_email_message(self):
42   - tryToFind = r"<a href='login'>sign in<a/>"
43 43 form = self.create_form_data()
44   - matched = search(tryToFind, str(form))
45   - self.assertIsNotNone(matched)
  44 + msg = form.error_messages.get('duplicate_email') % {
  45 + 'url': reverse('login')
  46 + }
  47 + self.assertIn(msg, str(form))
46 48  
47 49 def tearDown(self):
48 50 pass
... ...
colab/utils/conf.py
1 1  
2 2 import os
3 3 import sys
4   -
  4 +import importlib
5 5 import warnings
6 6  
7 7 from django.core.exceptions import ImproperlyConfigured
8 8  
9   -import importlib
10   -
11 9  
12 10 class InaccessibleSettings(ImproperlyConfigured):
13 11 """Settings.py is Inaccessible.
... ...