[damned-lies] Allow login by email instead of username



commit dccdcf42f3a2f5570a0a53f0dea2a86094a2e3be
Author: Claude Paroz <claude 2xlibre net>
Date:   Mon Apr 2 11:38:29 2018 +0200

    Allow login by email instead of username
    
    Fixes bug #722020

 people/forms.py      |    9 ++++++++-
 people/tests.py      |   27 ++++++++++++++++++++++++++-
 templates/login.html |    2 +-
 3 files changed, 35 insertions(+), 3 deletions(-)
---
diff --git a/people/forms.py b/people/forms.py
index 84de239..73d0b50 100644
--- a/people/forms.py
+++ b/people/forms.py
@@ -92,7 +92,14 @@ class RegistrationForm(forms.Form):
 
 
 class LoginForm(AuthenticationForm):
-    pass
+    def clean_username(self):
+        username = self.cleaned_data['username']
+        if '@' in username and not Person.objects.filter(username=username).exists():
+            try:
+                username = Person.objects.filter(email=username).first().username
+            except AttributeError:
+                pass
+        return username
 
 
 class DetailForm(forms.ModelForm):
diff --git a/people/tests.py b/people/tests.py
index 27c30c2..4182a4c 100644
--- a/people/tests.py
+++ b/people/tests.py
@@ -1,9 +1,10 @@
 import datetime
 from unittest import skipUnless
 
-from django.test import TestCase
+from django.contrib import auth
 from django.core import mail
 from django.core.exceptions import ValidationError
+from django.test import TestCase
 from django.urls import reverse
 from django.utils.safestring import SafeData
 from django.utils.translation import ugettext as _
@@ -55,6 +56,30 @@ class PeopleTestCase(TestCase):
         self.newu = Person.objects.get(username='newuser')
         self.assertTrue(self.newu.is_active)
 
+    def test_login_message(self):
+        self.pn = self._create_person()
+        response = self.client.post(
+            reverse('login'), data={'username': 'jn', 'password': 'password'}, follow=True
+        )
+        self.assertContains(
+            response,
+            'You have not joined any translation team yet. '
+            'You can do it from <a href="/users/team_join/">your profile</a>.'
+        )
+
+    def test_login_by_email(self):
+        self.pn = self._create_person()
+        response = self.client.post(
+            reverse('login'), data={'username': 'notexist devnull com', 'password': 'password'}
+        )
+        user = auth.get_user(self.client)
+        self.assertFalse(user.is_authenticated)
+        response = self.client.post(
+            reverse('login'), data={'username': 'jn devnull com', 'password': 'password'}
+        )
+        user = auth.get_user(self.client)
+        self.assertTrue(user.is_authenticated)
+
     def test_person_list(self):
         self.pn = self._create_person()
         response = self.client.get(reverse('people'))
diff --git a/templates/login.html b/templates/login.html
index a468d41..c0225ff 100644
--- a/templates/login.html
+++ b/templates/login.html
@@ -13,7 +13,7 @@
 {% url 'register' as link %}
 <p class="main_register">{% blocktrans %}If you do not own an account on this site, you can <a href='{{ link 
}}'>register</a> for a new account.{% endblocktrans %}</p>
 
-<p>{% trans 'Log in with your username and password:' %}</p>
+<p>{% trans 'Log in with your username (or email) and password:' %}</p>
 <form action="{% url 'login' %}" method="post" id="login-form" class="login">
   {% csrf_token %}
   <div class="form-row">


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]