Exemple #1
0
    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
Exemple #2
0
    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 create_allauth_user(email, username, password='', phonenumber='' ):
    # Create dictionary with given required params to generate signin_request
    post_dic = get_post_dic(email, username, password)
    # Generate signin_request that contains dictionary in POST params
    signin_request = get_signin_request(post_dic)

    # POST data as kwargs for signupForm
    signup_form_kwargs = {"files": {}, "initial": {},
                          "data": signin_request.POST}

    # SignUpForm Instatiation with post data 
    # makes SignupView.form_valid method activate
    signup_form = SignupForm(**signup_form_kwargs)
    signup_form.cleaned_data = post_dic              # Csrf token not neccesary either

    # Generates a user object from the form data
    user = signup_form.save(signin_request)
    # If phone number supplied add it to the model
    user.phone_number = phonenumber
    user.save()

    # Sends allauth.signed_up signal
    signals.user_signed_up.send(sender=user.__class__,
                                request=signin_request,
                                user=user,
                                signal_kwargs={})
    return user



# With `ACCOUNT_AUTHENTICATION_METHOD = "email"`
#allauth_regular_user = create_allauth_user("*****@*****.**")
def create_allauth_user(email, password):
    post_dic = get_post_dic(email, password)
    signin_request = get_signin_request(post_dic)

    signup_form_kwargs = {"files": {}, "initial": {},
                          "data": signin_request.POST}
    signup_form = SignupForm(**signup_form_kwargs)
    signup_form.cleaned_data = post_dic  # Csrf token not neccesary either
    user = signup_form.save(signin_request)

    # Sends sign up mail, alter to invite mail later on
    signals.user_signed_up.send(sender=user.__class__,
                                request=signin_request,
                                user=user,
                                signal_kwargs={})

    return user
def create_allauth_user(email):
    post_dic = get_post_dic(email)
    signin_request = get_signin_request(post_dic)

    signup_form_kwargs = {"files": {}, "initial": {},
                          "data": signin_request.POST}
    # From allauth.account.views.SignupView.form_valid
    signup_form = SignupForm(**signup_form_kwargs)
    signup_form.cleaned_data = post_dic # Csrf token not neccesary either
    user = signup_form.save(signin_request)

    # Sends sign up mail
    signals.user_signed_up.send(sender=user.__class__,
                                request=signin_request,
                                user=user,
                                signal_kwargs={})
    return user
def create_allauth_user(email):
    post_dic = get_post_dic(email)
    signin_request = get_signin_request(post_dic)

    signup_form_kwargs = {
        "files": {},
        "initial": {},
        "data": signin_request.POST
    }
    # From allauth.account.views.SignupView.form_valid
    signup_form = SignupForm(**signup_form_kwargs)
    signup_form.cleaned_data = post_dic  # Csrf token not neccesary either
    user = signup_form.save(signin_request)

    # Sends sign up mail
    signals.user_signed_up.send(sender=user.__class__,
                                request=signin_request,
                                user=user,
                                signal_kwargs={})
    return user