Example #1
0
def registration(request):
    """Render the registration page"""
    if request.user.is_authenticated:
        return redirect(reverse('index'))

    if request.method == "POST":
        registration_form = UserRegistrationForm(request.POST)

        if registration_form.is_valid():
            user = registration_form.save(commit=False) # lines 74-86 courtesy of Michael, tutor at Code Institute

            u = registration_form.cleaned_data['username']
            p = registration_form.cleaned_data['password1']
            try:
                validate_password(p,u)
            except ValidationError as e:
                registration_form.add_error('password1', e) # to be displayed with the fields errors
                return render(request, 'registration.html', {"registration_form": registration_form})    

            user.set_password(p)
            user.save()
            user = auth.authenticate(username= u, password =p) # sets the password on the user model and saves the user to the database and then logs them in

            if user:
                auth.login(user=user, request=request)
                messages.success(request, "You have successfully registered")
                return redirect(reverse('profile')) 
            else:
                messages.error(request, "Unable to register your account at this time")
    else:
        registration_form = UserRegistrationForm()
    return render(request, 'registration.html', {
        "registration_form": registration_form})
Example #2
0
def register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            try:
                customer = stripe.Customer.create(
                        email=form.cleaned_data['email'],
                        card=form.cleaned_data['stripe_id'],  # this is currently the card token/id
                        plan='REG_MONTHLY',
                )

                if customer:
                    user = form.save()  # save here to create the user and get its instance

                    # now we replace the card id with the actual user id for later
                    user.stripe_id = customer.id
                    user.subscription_end = arrow.now().replace(weeks=+4).datetime  # add 4 weeks from now
                    user.save()

                # check we saved correctly and can login
                user = auth.authenticate(email=request.POST.get('email'),
                                         password=request.POST.get('password1'))

                if user:
                    auth.login(request, user)
                    messages.success(request, "You have successfully registered")
                    return redirect(reverse('profile'))

                else:
                    messages.error(request, "unable to log you in at this time!")

            except stripe.error.CardError, e:
                form.add_error(request, "Your card was declined!")
def register(request):
    form = UserRegistrationForm(request.POST if request.POST else None)
    if request.method == "POST":
        if form.is_valid():
            messages.success(
                request,
                "Registration successfull. You can now login with your name and password."
            )
            form.save()
            return redirect('login')
        else:
            form.add_error(None,
                           'Registration failed. Check the errors below.')

    return render(request, 'register.html', {'form': form})
Example #4
0
def register(request):
    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            try:
                customer = stripe.Customer.create(
                    email=form.cleaned_data['email'],
                    card=form.cleaned_data[
                        'stripe_id'],  # this is currently the card token/id
                    plan='REG_MONTHLY',
                )

                if customer:
                    user = form.save(
                    )  # save here to create the user and get its instance

                    # now we replace the card id with the actual user id for later
                    user.stripe_id = customer.id
                    user.subscription_end = arrow.now().replace(
                        weeks=+4).datetime  # add 4 weeks from now
                    user.save()

                # check we saved correctly and can login
                user = auth.authenticate(
                    email=request.POST.get('email'),
                    password=request.POST.get('password1'))

                if user:
                    auth.login(request, user)
                    messages.success(request,
                                     "You have successfully registered")
                    return redirect(reverse('profile'))

                else:
                    messages.error(request,
                                   "unable to log you in at this time!")

            except stripe.error.CardError, e:
                form.add_error(request, "Your card was declined!")