Advertisement
Guest User

User Creation Form Mixin

a guest
Jul 24th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.61 KB | None | 0 0
  1. class UserCreationMixin(forms.ModelForm):
  2.  
  3.     error_messages = {
  4.         'password-mismatch': _(u'Enter the same password as before')
  5.     }
  6.  
  7.     first_name = forms.CharField(
  8.         label=_(u'First Name'),
  9.         max_length=100,
  10.         help_text=_(u'Enter your first name')
  11.     ),
  12.     last_name = forms.CharField(
  13.         label=_(u'Last Name'),
  14.         max_length=100,
  15.         help_text=_(u'Enter your last name')
  16.     ),
  17.     username = forms.CharField(
  18.         label=_(u'Username'),
  19.         min_length=8,
  20.         max_length=30,
  21.         help_text=_(u'Enter a valid username')
  22.     )
  23.     email = forms.EmailField(
  24.         label=_(u'Email Address'),
  25.         help_text=_(u'Enter a valid email address')
  26.     ),
  27.     password = forms.CharField(
  28.         label=_(u'Password'),
  29.         widget=forms.PasswordInput,
  30.         help_text=_(u'Enter a valid password')
  31.     ),
  32.     confirm_password = forms.CharField(
  33.         label=_(u'Confirm Password'),
  34.         widget=forms.PasswordInput,
  35.         help_text=_(u'Confirm your passwords match')
  36.     )
  37.  
  38.     def clean_confirm_password(self):
  39.         password = self.cleaned_data['password']
  40.         password2 = self.cleaned_data['confirm_password']
  41.  
  42.         if password and password2 and password != password2:
  43.             raise forms.ValidationError(
  44.                 UserCreationMixin.error_messages['password-mismatch'],
  45.                 code='password_mismatch'
  46.             )
  47.             return password2
  48.  
  49.         self.instance.username = self.cleaned_data['username']
  50.         password_validation.validate_password(password2, self.instance)
  51.         return password2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement