Guest User

Untitled

a guest
Mar 8th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. class C2CUserForm(forms.ModelForm):
  2.  
  3.     password = forms.CharField(label=_('password'), widget=forms.PasswordInput)
  4.     password2 = forms.CharField(label=_('password confirmation'), widget=forms.PasswordInput)
  5.  
  6.     class Meta:
  7.         model = User
  8.         fields = ['first_name', 'last_name', 'email', 'password', 'password2']
  9.  
  10.     def clean_email(self):
  11.         email = self.cleaned_data['email']
  12.         try:
  13.             user = User.objects.get(email=email)
  14.         except User.DoesNotExist:
  15.             return email
  16.         raise forms.ValidationError(u'An account with %s as email already exists' % email)
  17.  
  18.     def clean(self):
  19.         password = self.cleaned_data['password']
  20.         password2 = self.cleaned_data['password2']
  21.  
  22.         if password != password2:
  23.             raise forms.ValidationError(_('Both passwords mismatch'))
  24.  
  25.         return self.cleaned_data
  26.  
  27.     def save(self, *args, **kwargs):
  28.  
  29.         # username = email
  30.         self.instance.username = self.instance.email
  31.  
  32.         # set password
  33.         obj = super(C2CUserForm, self).save(*args, **kwargs)
  34.         obj.set_password(self.cleaned_data['password'])
  35.         obj.is_active= True
  36.         obj.save()
  37.  
  38.         obj.groups.add(Group.objects.get(name=C2CUserProfile.ORGANISM))
  39.  
  40.         return obj
Add Comment
Please, Sign In to add comment