class UserCreationForm(AuthUserCreationForm): """ Accepts one password field and populates the others. password fields with the value of that password field Adds appropriate classes to authentication form. """ password = CharField(label=_t("Password"), widget=PasswordInput(attrs={'class': 'input-large'}), validators=hue_get_password_validators()) def __init__(self, data=None, *args, **kwargs): if data and 'password' in data: data = data.copy() data['password1'] = data['password'] data['password2'] = data['password'] super(UserCreationForm, self).__init__(data=data, *args, **kwargs)
class OrganizationUserCreationForm(DjangoUserCreationForm): password = CharField(label=_t("Password"), widget=PasswordInput(attrs={'class': 'input-large'}), validators=hue_get_password_validators()) def __init__(self, data=None, *args, **kwargs): if data and 'password' in data: data = data.copy() data['password1'] = data['password'] data['password2'] = data['password'] super(OrganizationUserCreationForm, self).__init__(data=data, *args, **kwargs) class Meta(DjangoUserCreationForm.Meta): model = User fields = ('email', )
class UserChangeForm(django.contrib.auth.forms.UserChangeForm): """ This is similar, but not quite the same as djagno.contrib.auth.forms.UserChangeForm and UserCreationForm. """ GENERIC_VALIDATION_ERROR = _("Username or password is invalid.") username = forms.RegexField( label=_t("Username"), max_length=30, regex='^%s$' % (get_username_re_rule(), ), # Could use UnicodeUsernameValidator() help_text=_t( "Required. 30 characters or fewer. No whitespaces or colons."), error_messages={'invalid': _t("Whitespaces and ':' not allowed")}) password1 = forms.CharField(label=_t("New Password"), widget=forms.PasswordInput, required=False, validators=hue_get_password_validators()) password2 = forms.CharField(label=_t("Password confirmation"), widget=forms.PasswordInput, required=False, validators=hue_get_password_validators()) password_old = forms.CharField(label=_t("Current password"), widget=forms.PasswordInput, required=False) ensure_home_directory = forms.BooleanField( label=_t("Create home directory"), help_text=_t("Create home directory if one doesn't already exist."), initial=True, required=False) language = forms.ChoiceField(label=_t("Language Preference"), choices=LANGUAGES, required=False) unlock_account = forms.BooleanField( label=_t("Unlock Account"), help_text=_t("Unlock user's account for login."), initial=False, required=False) class Meta(django.contrib.auth.forms.UserChangeForm.Meta): model = User fields = [ "username", "first_name", "last_name", "email", "ensure_home_directory" ] def __init__(self, *args, **kwargs): super(UserChangeForm, self).__init__(*args, **kwargs) if self.instance.id and 'username' in self.fields: self.fields['username'].widget.attrs['readonly'] = True if 'desktop.auth.backend.LdapBackend' in desktop_conf.AUTH.BACKEND.get( ): self.fields['password1'].widget.attrs['readonly'] = True self.fields['password2'].widget.attrs['readonly'] = True self.fields['password_old'].widget.attrs['readonly'] = True self.fields['first_name'].widget.attrs['readonly'] = True self.fields['last_name'].widget.attrs['readonly'] = True self.fields['email'].widget.attrs['readonly'] = True if 'is_active' in self.fields: self.fields['is_active'].widget.attrs['readonly'] = True if 'is_superuser' in self.fields: self.fields['is_superuser'].widget.attrs['readonly'] = True if 'unlock_account' in self.fields: self.fields['unlock_account'].widget.attrs['readonly'] = True if 'groups' in self.fields: self.fields['groups'].widget.attrs['readonly'] = True if ENABLE_ORGANIZATIONS.get(): organization = self.instance.organization if self.instance.id else get_user_request_organization( ) self.fields['groups'].choices = [ (group.id, group.name) for group in organization.organizationgroup_set.all() ] def clean_username(self): username = self.cleaned_data["username"] if self.instance.username == username: return username try: User._default_manager.get(username=username) except User.DoesNotExist: return username raise forms.ValidationError(_("Username already exists."), code='duplicate_username') def clean_password(self): return self.cleaned_data["password"] def clean_password2(self): password1 = self.cleaned_data.get("password1", "") password2 = self.cleaned_data["password2"] if password1 != password2: raise forms.ValidationError(_t("Passwords do not match.")) return password2 def clean_password1(self): password = self.cleaned_data.get("password1", "") if self.instance.id is None and password == "": raise forms.ValidationError( _("You must specify a password when creating a new user.")) return self.cleaned_data.get("password1", "") def clean_password_old(self): if self.instance.id is not None: password1 = self.cleaned_data.get("password1", "") password_old = self.cleaned_data.get("password_old", "") if password1 != '' and not self.instance.check_password( password_old): raise forms.ValidationError(self.GENERIC_VALIDATION_ERROR) return self.cleaned_data.get("password_old", "") def save(self, commit=True): """ Update password if it's set. """ user = super(UserChangeForm, self).save(commit=False) if self.cleaned_data["password1"]: user.set_password(self.cleaned_data["password1"]) if commit: user.save() # groups must be saved after the user self.save_m2m() return user