def _make_user(request, data): """ Create a new user from just their full name and email and return the user. - create a password from a random string of letters and numbers - log the user in to their new account """ full_name = data['full_name'] email = data['email'] password = generate_key(12) # register a new user user = miniregister(full_name, email, password) # log the new user in user = authenticate(username=user.username, password=password) login(request, user) # return the user return user
def test_expected_case(self, mock_welcome): """ Giving the miniregister method a full name, email, and password should create a user, create a profile, send them a welcome email, and log them in. The method should return the authenticated user. """ user = miniregister(self.full_name, self.email, self.password) ok_(isinstance(user, User), 'A user should be created and returned.') ok_(user.profile, 'A profile should be created for the user.') ok_(user.is_authenticated(), 'The user should be logged in.') mock_welcome.assert_called_once( ) # The user should get a welcome email eq_(user.first_name, 'Lou', 'The first name should be extracted from the full name.') eq_(user.last_name, 'Reed', 'The last name should be extracted from the full name.') eq_(user.username, 'LouReed', 'The username should remove the spaces from the full name.')
def post(self, request, **kwargs): """ First we validate the payment form, so we don't charge someone's card by accident. Next, we charge their card. Finally, use the validated payment form to create and return a CrowdfundRequestPayment object. """ token = request.POST.get('stripe_token') email = request.POST.get('stripe_email') payment_form = CrowdfundPaymentForm(request.POST) if payment_form.is_valid() and token: amount = payment_form.cleaned_data['stripe_amount'] # If there is no user but the show and full_name fields are filled in, # create the user with our "miniregistration" functionality and then log them in user = request.user if request.user.is_authenticated() else None registered = False show = payment_form.cleaned_data['show'] full_name = payment_form.cleaned_data['full_name'] if user is None and show and full_name: password = generate_key(12) user = miniregister(full_name, email, password) registered = True try: crowdfund = payment_form.cleaned_data['crowdfund'] crowdfund.make_payment(token, email, amount, show, user) except ( stripe.InvalidRequestError, stripe.CardError, stripe.APIConnectionError, stripe.AuthenticationError ) as payment_error: logging.warn(payment_error) return self.return_error(request, payment_error) if request.is_ajax(): data = { 'authenticated': user.is_authenticated() if user else False, 'registered': registered } return JsonResponse(data, status=200) else: messages.success(request, 'Thank you for your contribution!') return redirect(self.get_redirect_url()) return self.return_error(request)