diff --git a/django/contrib/admin/forms.py b/django/contrib/admin/forms.py index 6814fc9083..05788d9cb1 100644 --- a/django/contrib/admin/forms.py +++ b/django/contrib/admin/forms.py @@ -4,30 +4,23 @@ from django import forms from django.contrib.auth import authenticate from django.contrib.auth.forms import AuthenticationForm -from django.utils.translation import ugettext_lazy - -ERROR_MESSAGE = ugettext_lazy("Please enter the correct %(username)s and password " - "for a staff account. Note that both fields may be case-sensitive.") +from django.utils.translation import ugettext_lazy as _ class AdminAuthenticationForm(AuthenticationForm): """ A custom authentication form used in the admin app. - """ - this_is_the_login_form = forms.BooleanField(widget=forms.HiddenInput, initial=1, - error_messages={'required': ugettext_lazy("Please log in again, because your session has expired.")}) + error_messages = { + 'invalid_login': _("Please enter the correct %(username)s and password " + "for a staff account. Note that both fields may be " + "case-sensitive."), + } - def clean(self): - username = self.cleaned_data.get('username') - password = self.cleaned_data.get('password') - message = ERROR_MESSAGE - params = {'username': self.username_field.verbose_name} - - if username and password: - self.user_cache = authenticate(username=username, password=password) - if self.user_cache is None: - raise forms.ValidationError(message, code='invalid', params=params) - elif not self.user_cache.is_active or not self.user_cache.is_staff: - raise forms.ValidationError(message, code='invalid', params=params) - return self.cleaned_data + def confirm_login_allowed(self, user): + if not user.is_active or not user.is_staff: + raise forms.ValidationError( + self.error_messages['invalid_login'], + code='invalid_login', + params={'username': self.username_field.verbose_name} + ) diff --git a/django/contrib/admin/sites.py b/django/contrib/admin/sites.py index 3d12b25d36..b83c2e3203 100644 --- a/django/contrib/admin/sites.py +++ b/django/contrib/admin/sites.py @@ -15,8 +15,6 @@ from django.utils.translation import ugettext_lazy, ugettext as _ from django.views.decorators.cache import never_cache from django.conf import settings -LOGIN_FORM_KEY = 'this_is_the_login_form' - class AlreadyRegistered(Exception): pass @@ -193,8 +191,6 @@ class AdminSite(object): cacheable=True. """ def inner(request, *args, **kwargs): - if LOGIN_FORM_KEY in request.POST and request.user.is_authenticated(): - auth_logout(request) if not self.has_permission(request): if request.path == reverse('admin:logout', current_app=self.name): index_path = reverse('admin:index', current_app=self.name) diff --git a/django/contrib/admin/templates/admin/login.html b/django/contrib/admin/templates/admin/login.html index 1371514d43..1c8ae35c35 100644 --- a/django/contrib/admin/templates/admin/login.html +++ b/django/contrib/admin/templates/admin/login.html @@ -12,14 +12,14 @@ {% block breadcrumbs %}{% endblock %} {% block content %} -{% if form.errors and not form.non_field_errors and not form.this_is_the_login_form.errors %} +{% if form.errors and not form.non_field_errors %}
{% if form.errors.items|length == 1 %}{% trans "Please correct the error below." %}{% else %}{% trans "Please correct the errors below." %}{% endif %}
{% endif %} -{% if form.non_field_errors or form.this_is_the_login_form.errors %} -{% for error in form.non_field_errors|add:form.this_is_the_login_form.errors %} +{% if form.non_field_errors %} +{% for error in form.non_field_errors %}{{ error }}
@@ -29,13 +29,12 @@