Guest User

Untitled

a guest
Apr 24th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. from django import newforms as forms
  2. from users.models import User
  3. import hashlib
  4.  
  5. class UserRegistration(forms.ModelForm):
  6. email = forms.EmailField()
  7. passwd = forms.CharField(min_length = 6, widget = forms.PasswordInput)
  8. confirm_passwd = forms.CharField(widget = forms.PasswordInput)
  9.  
  10. class Meta:
  11. model = User
  12. fields = ('email', 'passwd')
  13.  
  14. def passwd(self):
  15. passwd = self.cleaned_data.get('passwd', None)
  16.  
  17. if not passwd or len(passwd) < 6:
  18. raise forms.ValidationError('Your password must be at least 6 characters long.')
  19.  
  20. passwd = hashlib.sha1(passwd).hexdigest()
  21.  
  22. return passwd
  23.  
  24.  
  25. def clean_confirm_passwd(self):
  26. """Make sure confirmed passwd == passwd"""
  27. confirm_passwd = hashlib.sha1(self.cleaned_data.get('confirm_passwd', None)).hexdigest()
  28.  
  29. if confirm_passwd == self.cleaned_data.get('passwd', ''):
  30.  
  31. return confirm_passwd
  32.  
  33. raise forms.ValidationError('Your passwords do not match.')
  34.  
  35. def clean_email(self):
  36. """Make sure an e-mail address is unique"""
  37.  
  38. email = self.cleaned_data['email']
  39.  
  40. try:
  41. User.objects.get(email = email)
  42.  
  43. except (User.DoesNotExist):
  44. return email
  45.  
  46. else:
  47. raise forms.ValidationError('This email address is already in use.')
Add Comment
Please, Sign In to add comment