Commit beb229f0aedac06c6007d44001842fb4aa323118
Exists in
master
and in
4 other branches
Merge branch 'loggin_upon_existing_email' into 'master'
Loggin upon existing email Done issue #47, waiting for review.
Showing
2 changed files
with
67 additions
and
3 deletions
Show diff stats
colab/accounts/forms.py
| @@ -3,19 +3,21 @@ | @@ -3,19 +3,21 @@ | ||
| 3 | from collections import OrderedDict | 3 | from collections import OrderedDict |
| 4 | 4 | ||
| 5 | from django import forms | 5 | from django import forms |
| 6 | +from django.conf import settings | ||
| 6 | from django.contrib.auth import authenticate, get_user_model | 7 | from django.contrib.auth import authenticate, get_user_model |
| 7 | from django.contrib.auth.forms import ReadOnlyPasswordHashField | 8 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 8 | from django.contrib.auth.tokens import default_token_generator | 9 | from django.contrib.auth.tokens import default_token_generator |
| 9 | from django.contrib.sites.shortcuts import get_current_site | 10 | from django.contrib.sites.shortcuts import get_current_site |
| 11 | +from django.core.urlresolvers import reverse | ||
| 10 | from django.template import loader | 12 | from django.template import loader |
| 11 | from django.utils.encoding import force_bytes | 13 | from django.utils.encoding import force_bytes |
| 12 | from django.utils.http import urlsafe_base64_encode | 14 | from django.utils.http import urlsafe_base64_encode |
| 13 | from django.utils.text import capfirst | 15 | from django.utils.text import capfirst |
| 14 | from django.utils.translation import ugettext_lazy as _ | 16 | from django.utils.translation import ugettext_lazy as _ |
| 17 | +from django.utils.safestring import mark_safe | ||
| 15 | 18 | ||
| 16 | from conversejs.models import XMPPAccount | 19 | from conversejs.models import XMPPAccount |
| 17 | 20 | ||
| 18 | -from django.conf import settings | ||
| 19 | 21 | ||
| 20 | from .utils.validators import validate_social_account | 22 | from .utils.validators import validate_social_account |
| 21 | from .utils import mailman | 23 | from .utils import mailman |
| @@ -43,10 +45,10 @@ class UserForm(forms.ModelForm): | @@ -43,10 +45,10 @@ class UserForm(forms.ModelForm): | ||
| 43 | # Forces username to be lowercase always | 45 | # Forces username to be lowercase always |
| 44 | widget=forms.TextInput(attrs={'style': 'text-transform: lowercase;'}), | 46 | widget=forms.TextInput(attrs={'style': 'text-transform: lowercase;'}), |
| 45 | ) | 47 | ) |
| 46 | - required = ('first_name', 'last_name', 'username') | 48 | + required = ('first_name', 'last_name', 'username', 'email') |
| 47 | 49 | ||
| 48 | class Meta: | 50 | class Meta: |
| 49 | - fields = ('first_name', 'last_name', 'username') | 51 | + fields = ('first_name', 'last_name', 'username', 'email') |
| 50 | model = User | 52 | model = User |
| 51 | 53 | ||
| 52 | def __init__(self, *args, **kwargs): | 54 | def __init__(self, *args, **kwargs): |
| @@ -59,6 +61,20 @@ class UserForm(forms.ModelForm): | @@ -59,6 +61,20 @@ class UserForm(forms.ModelForm): | ||
| 59 | if field_name in UserForm.required: | 61 | if field_name in UserForm.required: |
| 60 | field.required = True | 62 | field.required = True |
| 61 | 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 | + | ||
| 62 | def clean_username(self): | 78 | def clean_username(self): |
| 63 | username = self.cleaned_data["username"].strip() | 79 | username = self.cleaned_data["username"].strip() |
| 64 | if not username: | 80 | if not username: |
| @@ -0,0 +1,48 @@ | @@ -0,0 +1,48 @@ | ||
| 1 | +""" | ||
| 2 | +Test Form class. | ||
| 3 | +Objective: Test parameters, and behavior. | ||
| 4 | +""" | ||
| 5 | +from re import search | ||
| 6 | + | ||
| 7 | +from django.test import TestCase | ||
| 8 | + | ||
| 9 | +from colab.accounts.forms import UserForm | ||
| 10 | +from colab.accounts.models import User | ||
| 11 | + | ||
| 12 | + | ||
| 13 | +class FormTest(TestCase): | ||
| 14 | + | ||
| 15 | + def setUp(self): | ||
| 16 | + user = User() | ||
| 17 | + user.username = "USERtestCoLaB" | ||
| 18 | + user.set_password("123colab4") | ||
| 19 | + user.email = "usertest@colab.com.br" | ||
| 20 | + user.id = 1 | ||
| 21 | + user.twitter = "usertestcolab" | ||
| 22 | + user.facebook = "usertestcolab" | ||
| 23 | + user.first_name = "USERtestCoLaB" | ||
| 24 | + user.last_name = "COLAB" | ||
| 25 | + user.save() | ||
| 26 | + | ||
| 27 | + def create_form_data(self): | ||
| 28 | + form_data = {'email': 'usertest@colab.com.br', | ||
| 29 | + 'first_name': 'colabName', | ||
| 30 | + 'last_name': 'secondName', | ||
| 31 | + 'username': 'colab', | ||
| 32 | + 'password1': '123colab4', | ||
| 33 | + 'password2': '123colab4'} | ||
| 34 | + form = UserForm(data=form_data) | ||
| 35 | + return form | ||
| 36 | + | ||
| 37 | + def test_already_registered_email(self): | ||
| 38 | + form = self.create_form_data() | ||
| 39 | + self.assertFalse(form.is_valid()) | ||
| 40 | + | ||
| 41 | + def test_registered_email_message(self): | ||
| 42 | + tryToFind = r"<a href='login'>sign in<a/>" | ||
| 43 | + form = self.create_form_data() | ||
| 44 | + matched = search(tryToFind, str(form)) | ||
| 45 | + self.assertIsNotNone(matched) | ||
| 46 | + | ||
| 47 | + def tearDown(self): | ||
| 48 | + pass |