Advertisement
Guest User

Untitled

a guest
Jul 9th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. class UserProfileCreationForm(UserCreationForm):
  2. username = forms.CharField(label="Username", max_length=40, min_length=5)
  3. email = forms.EmailField(label="Email address", max_length=40)
  4. first_name = forms.CharField(label='First name', max_length=40, required=False)
  5. last_name = forms.CharField(label='Last name', max_length=40, required=False)
  6.  
  7. password1 = forms.CharField(label="Password", widget=forms.PasswordInput, min_length=5)
  8. password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)
  9.  
  10. class Meta():
  11. model = UserProfile
  12. fields = (
  13. 'username', 'email', 'type_of_user', 'first_name', 'last_name', 'password1', 'password2','IBAN',
  14.  
  15. )
  16.  
  17. def clean_password1(self):
  18. password = self.cleaned_data.get('password1')
  19. if len(password) < 8:
  20. raise ValidationError('Password has to be of size at least 8 characters')
  21. return super(UserProfileCreationForm, self).clean_password1()
  22.  
  23. def clean_password2(self):
  24. password1 = self.cleaned_data.get("password1")
  25. password2 = self.cleaned_data.get("password2")
  26. if password1 and password2 and password1 != password2:
  27. raise forms.ValidationError("Password mismatch")
  28. return password2
  29.  
  30. def save(self, commit=True):
  31. try:
  32. with transaction.atomic():
  33. user = User(username=self.cleaned_data['username'],
  34. first_name=self.cleaned_data['first_name'],
  35. last_name=self.cleaned_data['last_name'],
  36. email=self.cleaned_data['email'])
  37. user.save()
  38. user.set_password(self.cleaned_data["password1"])
  39.  
  40. user_profile = UserProfile(user=user,
  41. IBAN=self.cleaned_data['IBAN'],
  42. type_of_user=self.cleaned_data['type_of_user']
  43. )
  44. user_profile.save()
  45. except:
  46. raise #TODO: HANDLE THE EXCEPTION
  47.  
  48. return user
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement