[damned-lies] Use proper login Form to login users



commit 6c72f322a220d9b652fe78f823d921aacec02967
Author: Claude Paroz <claude 2xlibre net>
Date:   Mon Apr 2 11:16:21 2018 +0200

    Use proper login Form to login users

 common/views.py                       |   38 ++++++++++++++++----------------
 people/forms.py                       |    5 ++++
 templates/login.html                  |   11 ++++-----
 templates/login/login_popup_form.html |    4 +--
 4 files changed, 30 insertions(+), 28 deletions(-)
---
diff --git a/common/views.py b/common/views.py
index 45fdc0f..3b2100b 100644
--- a/common/views.py
+++ b/common/views.py
@@ -10,9 +10,10 @@ from django.utils.translation import ugettext as _
 
 from people.models import Person, obfuscate_email
 from teams.models import Role
-from people.forms import RegistrationForm
+from people.forms import LoginForm, RegistrationForm
 from common.utils import get_user_locale
 
+
 def index(request):
     """ Homepage view """
     curlang = get_user_locale(request)
@@ -23,6 +24,7 @@ def index(request):
     }
     return render(request, 'index.html', context)
 
+
 def about(request):
     translator_credits = _("translator-credits")
     if translator_credits == "translator-credits":
@@ -35,6 +37,7 @@ def about(request):
     }
     return render(request, 'about.html', context)
 
+
 def site_login(request):
     """ Site-specific login page. Not named 'login' to not confuse with auth.login """
     def redirect(referer):
@@ -45,28 +48,23 @@ def site_login(request):
 
     referer = request.META.get('HTTP_REFERER', None)
     openid_path = ''
+    login_form = LoginForm(request, data=request.POST)
     if request.method == 'POST':
         if request.POST.get('referer', None):
             referer = request.POST['referer']
 
-        if 'username' in request.POST:
-            username = request.POST['username']
-            password = request.POST['password']
-            user = authenticate(username=username, password=password)
-            if user is not None:
-                if user.is_active:
-                    login(request, user)
-                    messages.success(request, _("You have been successfully logged in."))
-                    if Role.objects.filter(person__username=user.username).count() < 1:
-                        message = _("You have not joined any translation team yet. You can do it from <a 
href=\"%(url)s\">your profile</a>.") % {
-                            'url': reverse('person_team_join'),
-                        }
-                        messages.info(request, message)
-                    return redirect(referer)
-                else:
-                    messages.error(request, _("We’re sorry, but your account has been disabled."))
-            else:
-                messages.error(request, _("Login unsuccessful. Please verify your username and password."))
+        if login_form.is_valid():
+            user = login_form.get_user()
+            login(request, user)
+            if Role.objects.filter(person__username=user.username).count() < 1:
+                message = _("You have not joined any translation team yet. "
+                            "You can do it from <a href=\"%(url)s\">your profile</a>.") % {
+                    'url': reverse('person_team_join'),
+                }
+                messages.info(request, message)
+            return redirect(referer)
+        else:
+            messages.error(request, _("Login unsuccessful. Please verify your username and password."))
 
     if 'django_openid_auth' in settings.INSTALLED_APPS:
         openid_path = '/openid/login/'
@@ -75,9 +73,11 @@ def site_login(request):
         'openid_path': openid_path,
         'referer': referer,
         'next': referer,
+        'form': login_form,
     }
     return render(request, 'login.html', context)
 
+
 def site_register(request):
     openid_path = ''
     if request.method == 'POST':
diff --git a/people/forms.py b/people/forms.py
index fb2b45f..84de239 100644
--- a/people/forms.py
+++ b/people/forms.py
@@ -4,6 +4,7 @@ from urllib.request import urlopen
 
 from django import forms
 from django.conf import settings
+from django.contrib.auth.forms import AuthenticationForm
 from django.core.exceptions import ValidationError
 from django.urls import reverse
 from django.utils.encoding import force_bytes
@@ -90,6 +91,10 @@ class RegistrationForm(forms.Form):
         return new_user
 
 
+class LoginForm(AuthenticationForm):
+    pass
+
+
 class DetailForm(forms.ModelForm):
     class Meta:
         model = Person
diff --git a/templates/login.html b/templates/login.html
index dab6671..a468d41 100644
--- a/templates/login.html
+++ b/templates/login.html
@@ -17,14 +17,13 @@
 <form action="{% url 'login' %}" method="post" id="login-form" class="login">
   {% csrf_token %}
   <div class="form-row">
-    <label for="id_username">{% trans 'Username:' %}</label> <input type="text" name="username" 
id="id_username" />
+    {{ form.username.errors }}
+    {{ form.username.label_tag }} {{ form.username }}
   </div>
   <div class="form-row">
-    <label for="id_password">{% trans 'Password:' %}</label> <input type="password" name="password" 
id="id_password" />
-    <span class="help"><a href="/password_reset/">{% trans 'Have you forgotten your password?' %}</a></span>
-
-    <input type="hidden" name="this_is_the_login_form" value="1" />
-    <input type="hidden" name="post_data" value="{{ post_data }}" />
+    {{ form.password.errors }}
+    {{ form.password.label_tag }} {{ form.password }}
+    <span class="help"><a href="{% url 'password_reset' %}">{% trans 'Have you forgotten your password?' 
%}</a></span>
     <input type="hidden" name="referer" value="{{ referer|default:"" }}" />
   </div>
   <div class="submit-row">
diff --git a/templates/login/login_popup_form.html b/templates/login/login_popup_form.html
index 931858b..718806c 100644
--- a/templates/login/login_popup_form.html
+++ b/templates/login/login_popup_form.html
@@ -10,14 +10,12 @@
             <div class="form-group">
                 <input type="password" name="password" id="id_password" class="form-control" placeholder="{% 
trans 'Password' %}"/>
             </div>
-            <input type="hidden" name="this_is_the_login_form" value="1" />
-            <input type="hidden" name="post_data" value="{{ post_data }}" />
             <input type="hidden" name="referer" value="{{ referer|default:"" }}" />
             <div class="form-group">
                 <button type="submit" class="btn btn-primary btn-block">{% trans "Log in" %}</button>
             </div>
             <div class="form-group">
-                <a href="/password_reset/" class="">{% trans 'Forgot your password?' %}</a>
+                <a href="{% url 'password_reset' %}">{% trans 'Forgot your password?' %}</a>
             </div>
             <li class="divider"></li>
             <p>{% trans "Don’t have an account?" %}</p>


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