2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2010-12-02 08:44:35 +08:00
|
|
|
from django import forms
|
|
|
|
|
|
|
|
from django.contrib.auth import authenticate
|
|
|
|
from django.contrib.auth.forms import AuthenticationForm
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.utils.translation import ugettext_lazy, ugettext as _
|
|
|
|
|
2011-09-22 12:16:21 +08:00
|
|
|
ERROR_MESSAGE = ugettext_lazy("Please enter the correct username and password "
|
|
|
|
"for a staff account. Note that both fields are case-sensitive.")
|
2010-12-02 08:44:35 +08:00
|
|
|
|
|
|
|
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.")})
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
username = self.cleaned_data.get('username')
|
|
|
|
password = self.cleaned_data.get('password')
|
|
|
|
message = ERROR_MESSAGE
|
|
|
|
|
|
|
|
if username and password:
|
|
|
|
self.user_cache = authenticate(username=username, password=password)
|
|
|
|
if self.user_cache is None:
|
2012-06-08 00:08:47 +08:00
|
|
|
if '@' in username:
|
2010-12-02 08:44:35 +08:00
|
|
|
# Mistakenly entered e-mail address instead of username? Look it up.
|
|
|
|
try:
|
|
|
|
user = User.objects.get(email=username)
|
|
|
|
except (User.DoesNotExist, User.MultipleObjectsReturned):
|
|
|
|
# Nothing to do here, moving along.
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if user.check_password(password):
|
|
|
|
message = _("Your e-mail address is not your username."
|
|
|
|
" Try '%s' instead.") % user.username
|
|
|
|
raise forms.ValidationError(message)
|
|
|
|
elif not self.user_cache.is_active or not self.user_cache.is_staff:
|
|
|
|
raise forms.ValidationError(message)
|
|
|
|
self.check_for_test_cookie()
|
|
|
|
return self.cleaned_data
|