2014-05-08 16:59:35 +00:00
|
|
|
from django import forms
|
|
|
|
from django.contrib import auth
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
|
|
|
from orchestra.core.validators import validate_password
|
|
|
|
from orchestra.forms.widgets import ReadOnlyWidget
|
|
|
|
|
|
|
|
|
|
|
|
class AccountCreationForm(auth.forms.UserCreationForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(AccountCreationForm, self).__init__(*args, **kwargs)
|
|
|
|
self.fields['password1'].validators.append(validate_password)
|
|
|
|
|
|
|
|
def clean_username(self):
|
2014-09-30 09:49:07 +00:00
|
|
|
# Since model.clean() will check this, this is redundant,
|
|
|
|
# but it sets a nicer error message than the ORM and avoids conflicts with contrib.auth
|
2014-05-08 16:59:35 +00:00
|
|
|
username = self.cleaned_data["username"]
|
2014-09-30 10:20:11 +00:00
|
|
|
account_model = self._meta.model
|
|
|
|
if hasattr(account_model, 'systemusers'):
|
|
|
|
systemuser_model = account_model.systemusers.related.model
|
2014-09-30 09:49:07 +00:00
|
|
|
if systemuser_model.objects.filter(username=username).exists():
|
|
|
|
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
|
|
|
return username
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AccountChangeForm(forms.ModelForm):
|
2014-09-30 16:06:42 +00:00
|
|
|
username = forms.CharField(required=False)
|
2014-05-08 16:59:35 +00:00
|
|
|
password = auth.forms.ReadOnlyPasswordHashField(label=_("Password"),
|
|
|
|
help_text=_("Raw passwords are not stored, so there is no way to see "
|
|
|
|
"this user's password, but you can change the password "
|
|
|
|
"using <a href=\"password/\">this form</a>."))
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(AccountChangeForm, self).__init__(*args, **kwargs)
|
|
|
|
account = kwargs.get('instance')
|
2014-09-30 16:06:42 +00:00
|
|
|
username = '<b style="font-size:small">%s</b>' % account.username
|
|
|
|
self.fields['username'].widget = ReadOnlyWidget(username)
|
2014-09-29 13:34:38 +00:00
|
|
|
self.fields['password'].initial = account.password
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
def clean_password(self):
|
|
|
|
# Regardless of what the user provides, return the initial value.
|
|
|
|
# This is done here, rather than on the field, because the
|
|
|
|
# field does not have access to the initial value
|
|
|
|
return self.fields['password'].initial
|