def obj_create(self, bundle, **kwargs): #Validate that the needed fields exist validator = CustomFormValidation(form_class=UserForm, model_type=self._meta.resource_name) errors = validator.is_valid(bundle) if isinstance(errors, ErrorDict): raise BadRequest(errors.as_text()) #Extract needed fields username, password, email = bundle.data['username'], bundle.data['password'], bundle.data['email'] data_dict = {'username' : username, 'email' : email, 'password' : password, 'password1' : password, 'password2' : password} #Pass the fields to django-allauth. We want to use its email verification setup. signup_form = SignupForm() signup_form.cleaned_data = data_dict try: try: user = signup_form.save(bundle.request) profile, created = UserProfile.objects.get_or_create(user=user) except AssertionError: #If this fails, the user has a non-unique email address. user = User.objects.get(username=username) user.delete() raise BadRequest("Email address has already been used, try another.") #Need this so that the object is added to the bundle and exists during the dehydrate cycle. html = complete_signup(bundle.request, user, "") bundle.obj = user except IntegrityError: raise BadRequest("Username is already taken, try another.") return bundle
def form_valid(self, form): if self.get_form_class() == LoginForm: success_url = self.get_success_url() return form.login(self.request, redirect_url=success_url) elif self.get_form_class() == SignupForm: user = form.save(self.request) return complete_signup(self.request, user, app_settings.EMAIL_VERIFICATION, self.get_success_url())