2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.auth.models import User
|
2006-06-29 00:37:02 +08:00
|
|
|
from django.contrib.auth import authenticate
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django.template import Context, loader
|
|
|
|
from django.core import validators
|
2006-12-16 02:00:50 +08:00
|
|
|
from django import oldforms
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
from django.utils.translation import ugettext as _
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-12-16 02:00:50 +08:00
|
|
|
class UserCreationForm(oldforms.Manipulator):
|
2006-08-04 12:18:12 +08:00
|
|
|
"A form that creates a user, with no privileges, from the given username and password."
|
|
|
|
def __init__(self):
|
|
|
|
self.fields = (
|
2007-08-05 13:14:46 +08:00
|
|
|
oldforms.TextField(field_name='username', length=30, max_length=30, is_required=True,
|
2006-08-04 12:18:12 +08:00
|
|
|
validator_list=[validators.isAlphaNumeric, self.isValidUsername]),
|
2007-08-05 13:14:46 +08:00
|
|
|
oldforms.PasswordField(field_name='password1', length=30, max_length=60, is_required=True),
|
|
|
|
oldforms.PasswordField(field_name='password2', length=30, max_length=60, is_required=True,
|
2006-09-26 11:42:27 +08:00
|
|
|
validator_list=[validators.AlwaysMatchesOtherField('password1', _("The two password fields didn't match."))]),
|
2006-08-04 12:18:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def isValidUsername(self, field_data, all_data):
|
|
|
|
try:
|
|
|
|
User.objects.get(username=field_data)
|
|
|
|
except User.DoesNotExist:
|
|
|
|
return
|
2006-09-26 11:42:27 +08:00
|
|
|
raise validators.ValidationError, _('A user with that username already exists.')
|
2006-08-04 12:18:12 +08:00
|
|
|
|
|
|
|
def save(self, new_data):
|
|
|
|
"Creates the user."
|
|
|
|
return User.objects.create_user(new_data['username'], '', new_data['password1'])
|
|
|
|
|
2006-12-16 02:00:50 +08:00
|
|
|
class AuthenticationForm(oldforms.Manipulator):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Base class for authenticating users. Extend this to get a form that accepts
|
|
|
|
username/password logins.
|
|
|
|
"""
|
|
|
|
def __init__(self, request=None):
|
|
|
|
"""
|
|
|
|
If request is passed in, the manipulator will validate that cookies are
|
|
|
|
enabled. Note that the request (a HttpRequest object) must have set a
|
|
|
|
cookie with the key TEST_COOKIE_NAME and value TEST_COOKIE_VALUE before
|
|
|
|
running this validator.
|
|
|
|
"""
|
|
|
|
self.request = request
|
|
|
|
self.fields = [
|
2007-08-05 13:14:46 +08:00
|
|
|
oldforms.TextField(field_name="username", length=15, max_length=30, is_required=True,
|
2006-05-02 09:31:56 +08:00
|
|
|
validator_list=[self.isValidUser, self.hasCookiesEnabled]),
|
2007-08-05 13:14:46 +08:00
|
|
|
oldforms.PasswordField(field_name="password", length=15, max_length=30, is_required=True),
|
2006-05-02 09:31:56 +08:00
|
|
|
]
|
|
|
|
self.user_cache = None
|
|
|
|
|
|
|
|
def hasCookiesEnabled(self, field_data, all_data):
|
|
|
|
if self.request and not self.request.session.test_cookie_worked():
|
|
|
|
raise validators.ValidationError, _("Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in.")
|
|
|
|
|
|
|
|
def isValidUser(self, field_data, all_data):
|
2006-06-29 00:37:02 +08:00
|
|
|
username = field_data
|
|
|
|
password = all_data.get('password', None)
|
|
|
|
self.user_cache = authenticate(username=username, password=password)
|
2006-06-02 12:42:10 +08:00
|
|
|
if self.user_cache is None:
|
2006-05-02 09:31:56 +08:00
|
|
|
raise validators.ValidationError, _("Please enter a correct username and password. Note that both fields are case-sensitive.")
|
2006-06-02 12:42:10 +08:00
|
|
|
elif not self.user_cache.is_active:
|
|
|
|
raise validators.ValidationError, _("This account is inactive.")
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def get_user_id(self):
|
|
|
|
if self.user_cache:
|
|
|
|
return self.user_cache.id
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_user(self):
|
|
|
|
return self.user_cache
|
|
|
|
|
2006-12-16 02:00:50 +08:00
|
|
|
class PasswordResetForm(oldforms.Manipulator):
|
2006-05-02 09:31:56 +08:00
|
|
|
"A form that lets a user request a password reset"
|
|
|
|
def __init__(self):
|
|
|
|
self.fields = (
|
2006-12-16 02:00:50 +08:00
|
|
|
oldforms.EmailField(field_name="email", length=40, is_required=True,
|
2006-05-02 09:31:56 +08:00
|
|
|
validator_list=[self.isValidUserEmail]),
|
|
|
|
)
|
|
|
|
|
|
|
|
def isValidUserEmail(self, new_data, all_data):
|
|
|
|
"Validates that a user exists with the given e-mail address"
|
2007-06-20 04:04:54 +08:00
|
|
|
self.users_cache = list(User.objects.filter(email__iexact=new_data))
|
|
|
|
if len(self.users_cache) == 0:
|
2006-10-17 08:45:46 +08:00
|
|
|
raise validators.ValidationError, _("That e-mail address doesn't have an associated user account. Are you sure you've registered?")
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-07-28 01:48:35 +08:00
|
|
|
def save(self, domain_override=None, email_template_name='registration/password_reset_email.html'):
|
2006-05-02 09:31:56 +08:00
|
|
|
"Calculates a new password randomly and sends it to the user"
|
|
|
|
from django.core.mail import send_mail
|
2007-06-20 04:04:54 +08:00
|
|
|
for user in self.users_cache:
|
|
|
|
new_pass = User.objects.make_random_password()
|
|
|
|
user.set_password(new_pass)
|
|
|
|
user.save()
|
|
|
|
if not domain_override:
|
|
|
|
current_site = Site.objects.get_current()
|
|
|
|
site_name = current_site.name
|
|
|
|
domain = current_site.domain
|
|
|
|
else:
|
|
|
|
site_name = domain = domain_override
|
|
|
|
t = loader.get_template(email_template_name)
|
|
|
|
c = {
|
|
|
|
'new_password': new_pass,
|
|
|
|
'email': user.email,
|
|
|
|
'domain': domain,
|
|
|
|
'site_name': site_name,
|
|
|
|
'user': user,
|
|
|
|
}
|
2007-10-27 12:34:50 +08:00
|
|
|
send_mail(_('Password reset on %s') % site_name, t.render(Context(c)), None, [user.email])
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2006-12-16 02:00:50 +08:00
|
|
|
class PasswordChangeForm(oldforms.Manipulator):
|
2006-05-02 09:31:56 +08:00
|
|
|
"A form that lets a user change his password."
|
|
|
|
def __init__(self, user):
|
|
|
|
self.user = user
|
|
|
|
self.fields = (
|
2007-08-05 13:14:46 +08:00
|
|
|
oldforms.PasswordField(field_name="old_password", length=30, max_length=30, is_required=True,
|
2006-05-02 09:31:56 +08:00
|
|
|
validator_list=[self.isValidOldPassword]),
|
2007-08-05 13:14:46 +08:00
|
|
|
oldforms.PasswordField(field_name="new_password1", length=30, max_length=30, is_required=True,
|
2006-09-25 21:53:41 +08:00
|
|
|
validator_list=[validators.AlwaysMatchesOtherField('new_password2', _("The two 'new password' fields didn't match."))]),
|
2007-08-05 13:14:46 +08:00
|
|
|
oldforms.PasswordField(field_name="new_password2", length=30, max_length=30, is_required=True),
|
2006-05-02 09:31:56 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def isValidOldPassword(self, new_data, all_data):
|
|
|
|
"Validates that the old_password field is correct."
|
|
|
|
if not self.user.check_password(new_data):
|
2006-09-25 21:53:41 +08:00
|
|
|
raise validators.ValidationError, _("Your old password was entered incorrectly. Please enter it again.")
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def save(self, new_data):
|
|
|
|
"Saves the new password."
|
|
|
|
self.user.set_password(new_data['new_password1'])
|
|
|
|
self.user.save()
|
2006-12-30 15:16:25 +08:00
|
|
|
|
|
|
|
class AdminPasswordChangeForm(oldforms.Manipulator):
|
|
|
|
"A form used to change the password of a user in the admin interface."
|
|
|
|
def __init__(self, user):
|
|
|
|
self.user = user
|
|
|
|
self.fields = (
|
2007-08-05 13:14:46 +08:00
|
|
|
oldforms.PasswordField(field_name='password1', length=30, max_length=60, is_required=True),
|
|
|
|
oldforms.PasswordField(field_name='password2', length=30, max_length=60, is_required=True,
|
2006-12-30 15:16:25 +08:00
|
|
|
validator_list=[validators.AlwaysMatchesOtherField('password1', _("The two password fields didn't match."))]),
|
|
|
|
)
|
|
|
|
|
|
|
|
def save(self, new_data):
|
|
|
|
"Saves the new password."
|
|
|
|
self.user.set_password(new_data['password1'])
|
|
|
|
self.user.save()
|