2010-01-05 11:56:19 +08:00
|
|
|
from django.contrib.auth.models import User, UNUSABLE_PASSWORD
|
2006-06-29 00:37:02 +08:00
|
|
|
from django.contrib.auth import authenticate
|
2008-08-01 04:47:53 +08:00
|
|
|
from django.contrib.auth.tokens import default_token_generator
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.sites.models import Site
|
|
|
|
from django.template import Context, loader
|
2008-07-19 09:22:26 +08:00
|
|
|
from django import forms
|
2008-07-19 07:54:34 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2008-08-01 04:47:53 +08:00
|
|
|
from django.utils.http import int_to_base36
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class UserCreationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A form that creates a user, with no privileges, from the given username and password.
|
|
|
|
"""
|
|
|
|
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^\w+$',
|
|
|
|
help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."),
|
|
|
|
error_message = _("This value must contain only letters, numbers and underscores."))
|
2008-08-02 05:18:17 +08:00
|
|
|
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
|
|
|
|
password2 = forms.CharField(label=_("Password confirmation"), widget=forms.PasswordInput)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ("username",)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2010-01-05 11:56:19 +08:00
|
|
|
def clean(self):
|
|
|
|
# Fill the password field so model validation won't complain about it
|
|
|
|
# being blank. We'll set it with the real value below.
|
|
|
|
self.instance.password = UNUSABLE_PASSWORD
|
|
|
|
super(UserCreationForm, self).clean()
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean_username(self):
|
|
|
|
username = self.cleaned_data["username"]
|
2006-08-04 12:18:12 +08:00
|
|
|
try:
|
2008-07-19 07:54:34 +08:00
|
|
|
User.objects.get(username=username)
|
2006-08-04 12:18:12 +08:00
|
|
|
except User.DoesNotExist:
|
2008-07-19 07:54:34 +08:00
|
|
|
return username
|
|
|
|
raise forms.ValidationError(_("A user with that username already exists."))
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean_password2(self):
|
2008-08-26 00:55:57 +08:00
|
|
|
password1 = self.cleaned_data.get("password1", "")
|
2008-07-19 07:54:34 +08:00
|
|
|
password2 = self.cleaned_data["password2"]
|
|
|
|
if password1 != password2:
|
|
|
|
raise forms.ValidationError(_("The two password fields didn't match."))
|
2010-01-05 11:56:19 +08:00
|
|
|
self.instance.set_password(password1)
|
2008-07-19 07:54:34 +08:00
|
|
|
return password2
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-08-26 01:10:20 +08:00
|
|
|
class UserChangeForm(forms.ModelForm):
|
|
|
|
username = forms.RegexField(label=_("Username"), max_length=30, regex=r'^\w+$',
|
|
|
|
help_text = _("Required. 30 characters or fewer. Alphanumeric characters only (letters, digits and underscores)."),
|
|
|
|
error_message = _("This value must contain only letters, numbers and underscores."))
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class AuthenticationForm(forms.Form):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
Base class for authenticating users. Extend this to get a form that accepts
|
|
|
|
username/password logins.
|
|
|
|
"""
|
2008-07-19 07:54:34 +08:00
|
|
|
username = forms.CharField(label=_("Username"), max_length=30)
|
2008-08-02 05:18:17 +08:00
|
|
|
password = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __init__(self, request=None, *args, **kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
2008-07-19 07:54:34 +08:00
|
|
|
If request is passed in, the form will validate that cookies are
|
2006-05-02 09:31:56 +08:00
|
|
|
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
|
2008-07-19 07:54:34 +08:00
|
|
|
running this validation.
|
2006-05-02 09:31:56 +08:00
|
|
|
"""
|
|
|
|
self.request = request
|
|
|
|
self.user_cache = None
|
2008-07-19 07:54:34 +08:00
|
|
|
super(AuthenticationForm, self).__init__(*args, **kwargs)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean(self):
|
|
|
|
username = self.cleaned_data.get('username')
|
|
|
|
password = self.cleaned_data.get('password')
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
if username and password:
|
|
|
|
self.user_cache = authenticate(username=username, password=password)
|
|
|
|
if self.user_cache is None:
|
|
|
|
raise forms.ValidationError(_("Please enter a correct username and password. Note that both fields are case-sensitive."))
|
|
|
|
elif not self.user_cache.is_active:
|
|
|
|
raise forms.ValidationError(_("This account is inactive."))
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
# TODO: determine whether this should move to its own method.
|
|
|
|
if self.request:
|
|
|
|
if not self.request.session.test_cookie_worked():
|
|
|
|
raise forms.ValidationError(_("Your Web browser doesn't appear to have cookies enabled. Cookies are required for logging in."))
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
return self.cleaned_data
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_user_id(self):
|
|
|
|
if self.user_cache:
|
|
|
|
return self.user_cache.id
|
|
|
|
return None
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def get_user(self):
|
|
|
|
return self.user_cache
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class PasswordResetForm(forms.Form):
|
2008-07-31 17:08:55 +08:00
|
|
|
email = forms.EmailField(label=_("E-mail"), max_length=75)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean_email(self):
|
|
|
|
"""
|
|
|
|
Validates that a user exists with the given e-mail address.
|
|
|
|
"""
|
|
|
|
email = self.cleaned_data["email"]
|
|
|
|
self.users_cache = User.objects.filter(email__iexact=email)
|
2007-06-20 04:04:54 +08:00
|
|
|
if len(self.users_cache) == 0:
|
2008-07-19 07:54:34 +08:00
|
|
|
raise forms.ValidationError(_("That e-mail address doesn't have an associated user account. Are you sure you've registered?"))
|
2009-02-27 01:11:28 +08:00
|
|
|
return email
|
2008-08-01 04:47:53 +08:00
|
|
|
|
|
|
|
def save(self, domain_override=None, email_template_name='registration/password_reset_email.html',
|
|
|
|
use_https=False, token_generator=default_token_generator):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2008-08-04 20:11:04 +08:00
|
|
|
Generates a one-use only link for resetting password and sends to the user
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.core.mail import send_mail
|
2007-06-20 04:04:54 +08:00
|
|
|
for user in self.users_cache:
|
|
|
|
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 = {
|
|
|
|
'email': user.email,
|
|
|
|
'domain': domain,
|
|
|
|
'site_name': site_name,
|
2008-08-01 04:47:53 +08:00
|
|
|
'uid': int_to_base36(user.id),
|
2007-06-20 04:04:54 +08:00
|
|
|
'user': user,
|
2008-08-01 04:47:53 +08:00
|
|
|
'token': token_generator.make_token(user),
|
|
|
|
'protocol': use_https and 'https' or 'http',
|
2008-07-19 07:54:34 +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
|
|
|
|
2008-08-01 04:47:53 +08:00
|
|
|
class SetPasswordForm(forms.Form):
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2008-08-01 04:47:53 +08:00
|
|
|
A form that lets a user change set his/her password without
|
|
|
|
entering the old password
|
2008-07-19 07:54:34 +08:00
|
|
|
"""
|
2008-08-02 05:18:17 +08:00
|
|
|
new_password1 = forms.CharField(label=_("New password"), widget=forms.PasswordInput)
|
|
|
|
new_password2 = forms.CharField(label=_("New password confirmation"), widget=forms.PasswordInput)
|
2008-08-01 04:47:53 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __init__(self, user, *args, **kwargs):
|
2006-05-02 09:31:56 +08:00
|
|
|
self.user = user
|
2008-08-01 04:47:53 +08:00
|
|
|
super(SetPasswordForm, self).__init__(*args, **kwargs)
|
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean_new_password2(self):
|
|
|
|
password1 = self.cleaned_data.get('new_password1')
|
|
|
|
password2 = self.cleaned_data.get('new_password2')
|
|
|
|
if password1 and password2:
|
|
|
|
if password1 != password2:
|
|
|
|
raise forms.ValidationError(_("The two password fields didn't match."))
|
|
|
|
return password2
|
2008-08-01 04:47:53 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def save(self, commit=True):
|
|
|
|
self.user.set_password(self.cleaned_data['new_password1'])
|
|
|
|
if commit:
|
|
|
|
self.user.save()
|
|
|
|
return self.user
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-08-01 04:47:53 +08:00
|
|
|
class PasswordChangeForm(SetPasswordForm):
|
|
|
|
"""
|
|
|
|
A form that lets a user change his/her password by entering
|
|
|
|
their old password.
|
|
|
|
"""
|
2008-08-02 05:18:17 +08:00
|
|
|
old_password = forms.CharField(label=_("Old password"), widget=forms.PasswordInput)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-08-01 04:47:53 +08:00
|
|
|
def clean_old_password(self):
|
|
|
|
"""
|
|
|
|
Validates that the old_password field is correct.
|
|
|
|
"""
|
|
|
|
old_password = self.cleaned_data["old_password"]
|
|
|
|
if not self.user.check_password(old_password):
|
|
|
|
raise forms.ValidationError(_("Your old password was entered incorrectly. Please enter it again."))
|
|
|
|
return old_password
|
|
|
|
PasswordChangeForm.base_fields.keyOrder = ['old_password', 'new_password1', 'new_password2']
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
class AdminPasswordChangeForm(forms.Form):
|
|
|
|
"""
|
|
|
|
A form used to change the password of a user in the admin interface.
|
|
|
|
"""
|
2008-08-02 05:18:17 +08:00
|
|
|
password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput)
|
|
|
|
password2 = forms.CharField(label=_("Password (again)"), widget=forms.PasswordInput)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def __init__(self, user, *args, **kwargs):
|
2006-12-30 15:16:25 +08:00
|
|
|
self.user = user
|
2008-07-19 07:54:34 +08:00
|
|
|
super(AdminPasswordChangeForm, self).__init__(*args, **kwargs)
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def clean_password2(self):
|
|
|
|
password1 = self.cleaned_data.get('password1')
|
|
|
|
password2 = self.cleaned_data.get('password2')
|
|
|
|
if password1 and password2:
|
|
|
|
if password1 != password2:
|
|
|
|
raise forms.ValidationError(_("The two password fields didn't match."))
|
|
|
|
return password2
|
2008-08-06 00:36:20 +08:00
|
|
|
|
2008-07-19 07:54:34 +08:00
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Saves the new password.
|
|
|
|
"""
|
|
|
|
self.user.set_password(self.cleaned_data["password1"])
|
|
|
|
if commit:
|
|
|
|
self.user.save()
|
|
|
|
return self.user
|