class UserChangeForm(django.contrib.auth.forms.UserChangeForm): """ This is similar, but not quite the same as djagno.contrib.auth.forms.UserChangeForm and UserCreationForm. """ username = forms.RegexField( label=_t("Username"), max_length=30, regex='^%s$' % (get_username_re_rule(),), 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("Password"), widget=forms. PasswordInput, required=False, validators=get_password_validators()) password2 = forms.CharField(label=_t("Password confirmation"), widget=forms.PasswordInput, required=False, validators=get_password_validators()) password_old = forms.CharField(label=_t("Previous 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) class Meta(django.contrib.auth.forms.UserChangeForm.Meta): 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: self.fields['username'].widget.attrs['readonly'] = True if desktop_conf.AUTH.BACKEND.get() == 'desktop.auth.backend.LdapBackend': self.fields['password1'].widget.attrs['readonly'] = True self.fields['password2'].widget.attrs['readonly'] = True self.fields['password_old'].widget.attrs['readonly'] = True 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(_("The old password does not match the current password.")) 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
class loginForm(forms.Form): username = forms.CharField(max_length=30, ) password = forms.CharField(max_length=30, widget=forms.PasswordInput())
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
class SignupForm(UserCreationForm): username = forms.CharField(max_length=254, widget=forms.TextInput({ 'class': 'form-control', 'placeholder': 'Username' })) first_name = forms.CharField(max_length=30, required=False, help_text='Optional.', widget=forms.TextInput({ 'class': 'form-control', 'placeholder': 'First Name' })) last_name = forms.CharField(max_length=30, required=False, help_text='Optional.', widget=forms.TextInput({ 'class': 'form-control', 'placeholder': 'Last Name' })) email = forms.EmailField(max_length=254, required=True, widget=forms.TextInput({ 'class': 'form-control', 'placeholder': 'Email address' })) password1 = forms.CharField(label=_("Password"), widget=forms.PasswordInput({ 'class': 'form-control', 'placeholder': 'Create password' })) password2 = forms.CharField(label=_("Password"), widget=forms.PasswordInput({ 'class': 'form-control', 'placeholder': 'Repeat password' })) class Meta: model = User fields = ( 'username', 'first_name', 'last_name', 'email', 'password1', 'password2', ) def clean_email(self): # Get the email email = self.cleaned_data.get('email') # Check to see if any users already exist with this email as a username. try: match = User.objects.get(email=email) except User.DoesNotExist: # Unable to find a user, this is fine return email # A user was found with this as a username, raise an error. raise forms.ValidationError( 'This email address is already in use.')
class CsvRowValidationForm(forms.Form): employee_id = forms.IntegerField(required=True) last_name = forms.CharField(required=True) first_name = forms.CharField(required=True) email = forms.EmailField(required=True) password = forms.CharField(required=True)
class SearchForm(forms.Form): search_query = forms.CharField(max_length=100)
class LoginForm(forms.Form): username = forms.CharField() password = forms.CharField(widget=forms.PasswordInput)