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