def register(request): user = None if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): # update based on your billing method (subscription vs one time) customer = Customer.create( email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan="gold", ) # customer = stripe.Charge.create( # description=form.cleaned_data['email'], # card=form.cleaned_data['stripe_token'], # amount="5000", # currency="usd" # ) cd = form.cleaned_data try: with transaction.atomic(): user = User.create( cd['name'], cd['email'], cd['password'], cd['last_4_digits'], stripe_id='' ) if customer: user.stripe_id = customer.id user.save() else: UnpaidUsers(email=cd['email']).save() except IntegrityError: import traceback form.addError(cd['email'] + ' is already a member' + traceback.format_exc()) user = None else: request.session['user'] = user.pk return HttpResponseRedirect('/') else: form = UserForm() return render_to_response( 'payments/register.html', { 'form': form, 'months': list(range(1, 12)), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': list(range(2011, 2036)), }, context_instance=RequestContext(request) )
def register(request): user = None # checks if the http request is POST if request.method == 'POST': form = UserForm(request.POST) # form validation if form.is_valid(): #Uses stripe API to create a customer in stripe # update based on your billing method (subscription vs one time) # pdb.set_trace() customer = stripe.Customer.create( email=form.cleaned_data['email'], description=form.cleaned_data['name'], # card=form.cleaned_data['stripe_token'], card={ 'number': '4242424242424242', 'exp_month': 10, 'exp_year': 2018, }, plan="gold", ) # customer = stripe.Charge.create( # description = form.cleaned_data['email'], # card = form.cleand_data['stripe_token'], # amount = "5000" # currency = "usd" # ) user = User( name=form.cleaned_data['name'], email=form.cleaned_data['email'], last_4_digits=form.cleaned_data['last_4_digits'], stripe_id=customer.id, ) # set_password takes care of password hasing # ensure encrypted password user.set_password(form.cleaned_data['password']) try: user.save() except IntegrityError: form.addError(user.email + ' is already a member') else: request.session['user'] = user.pk return HttpResponseRedirect('/') else: form = UserForm() return render_to_response( 'register.html', { 'form': form, 'months': range(1, 13), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': range(2011, 2037), }, context_instance=RequestContext(request) )
def register(request): user = None if request.method == "POST": # We only talk AJAX posts now if not request.is_ajax(): return HttpResponseBadRequest("I only speak AJAX nowadays") data = json.loads(request.body.decode()) form = UserForm(data) if form.is_valid(): try: customer = Customer.create( "subscription", email=form.cleaned_data["email"], description=form.cleaned_data["name"], card=form.cleaned_data["stripe_token"], plan="gold", ) except Exception as exp: form.addError(exp) cd = form.cleaned_data try: with transaction.atomic(): user = User.create(cd["name"], cd["email"], cd["password"], cd["last_4_digits"], stripe_id="") if customer: user.stripe_id = customer.id user.save() else: UnpaidUsers(email=cd["email"]).save() except IntegrityError: resp = json.dumps({"status": "fail", "errors": cd["email"] + " is already a member"}) else: request.session["user"] = user.pk resp = json.dumps({"status": "ok", "url": "/"}) return HttpResponse(resp, content_type="application/json") else: # form not valid resp = json.dumps({"status": "form-invalid", "errors": form.errors}) return HttpResponse(resp, content_type="application/json") else: form = UserForm() return render_to_response( "payments/register.html", { "form": form, "months": list(range(1, 12)), "publishable": settings.STRIPE_PUBLISHABLE, "soon": soon(), "user": user, "years": list(range(2011, 2036)), }, context_instance=RequestContext(request), )
def test_user_form_passwords_match(self): form = UserForm({'name' : 'jj', 'email': '*****@*****.**', 'password' : '1234', 'ver_password' : '1234', 'last_4_digits' : '3333', 'stripe_token': '1'}) self.assertTrue(form.is_valid()) #this will throw an error if it doesn't clean correctly self.assertIsNotNone(form.clean())
def test_user_form_passwords_dont_match_throws_errors(self): form= UserForm( {'name':'jj', 'email':'*****@*****.**', 'password':'******', 'ver_password':'******',#incorrect password 'last_4_digits':'3333','stripe_token':'1' }) # Is the data valid? self.assertFalse(form.is_valid()) self.assertRaisesMessage(forms.ValidationError,"Passwords do not match",form.clean)
def register(request): user = None if request.method == 'POST': form = UserForm(request.POST) print("@@@@@@@@@@@@@@@@@@@@@@@@@@@") print(request) print("@@@@@@@@@@@@@@@@@@@@@@@@@@@") print(form) if form.is_valid(): #update based on your billing method (subscription vs one time) customer = stripe.Customer.create( email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan="gold", ) # customer = stripe.Charge.create( # description = form.cleaned_data['email'], # card = form.cleaned_data['stripe_token'], # amount="5000", # currency="usd" # ) user = User( name=form.cleaned_data['name'], email=form.cleaned_data['email'], last_4_digits=form.cleaned_data['last_4_digits'], stripe_id=customer.id, ) #ensure encrypted password user.set_password(form.cleaned_data['password']) try: user.save() except IntegrityError: form.addError(user.email + ' is already a member') else: request.session['user'] = user.pk return redirect('/') else: form = UserForm() return render( request, 'register.html', { 'form': form, 'months': range(1, 12), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': range(2011, 2036), }, )
def test_user_form_passwords_dont_match_throws_error(self): form = UserForm({'name' : 'jj', 'email' : '*****@*****.**', 'password' : '123', 'ver_password' : '1234', 'last_4_digits' : '3333', 'stripe_token': '1', 'sub_type' : 'yearly'}) self.assertFalse(form.is_valid()) self.assertRaisesMessage(forms.ValidationError, 'Passwords do not match', form.clean)
def register(request): user = None if request.method == 'POST': form = UserForm(request.POST) print form.is_valid() if form.is_valid(): print 'form passed validation' customer = stripe.Customer.create( email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan='gold', ) cd = form.cleaned_data try: user = User.create(cd['name'], cd['email'], cd['password'], cd['last_4_digits'], customer.id) except IntegrityError: form.addError(cd['email'] + ' is already a member') else: request.session['user'] = user.pk return HttpResponseRedirect('/') else: form = UserForm() return render_to_response('register.html', { 'form': form, 'months': range(1, 12), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': range(2016, 2036) }, context_instance=RequestContext(request))
def test_registering_user_twice_cause_error_msg(self): # create a user with same email so we get an integrity error user = User(name='test_user', email='*****@*****.**') user.save() # now create the request used to test the view self.request.session = {} self.request.method = 'POST' self.request.POST = { 'email': '*****@*****.**', 'name': 'test_user', 'stripe_token': '4242424242424242', 'last_4_digits': '4242', 'password': '******', 'ver_password': '******', } # create our expected form expected_form = UserForm(self.request.POST) expected_form.is_valid() expected_form.addError('[email protected] is already a member') # create the expected html html = render_to_response( 'register.html', { 'form': expected_form, 'months': range(1, 12), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': None, 'years': range(2011, 2036), } ) # mock out stripe so we don't hit their server with mock.patch('stripe.Customer') as stripe_mock: config = {'create.return_value': mock.Mock()} stripe_mock.configure_mock(**config) # run the test resp = register(self.request) # verify that we did things correctly self.assertEquals(resp.status_code, 200) self.assertEquals(self.request.session, {}) # assert there is only one record in the database. users = User.objects.filter(email='*****@*****.**') #self.assertEquals(len(users), 1) # check actual return self.assertEquals(resp.content, html.content)
def test_user_form_passwords_match(self): form=UserForm( { 'name':'jj', 'email':'*****@*****.**', 'password':'******', 'ver_password':'******', 'last_4_digits':'3333', 'stripe_token':'1' } ) # Is the data valid? -- if not print out the errors self.assertTrue(form.is_valid(),form.errors) #this will throw an error if the form doesn't clean correctly self.assertIsNotNone(form.clean())
def test_registering_user_twice_cause_error_msg(self): try: with transaction.atomic(): user = User(name = 'pyRock', email = '*****@*****.**') user.save() self.request.session = {} self.request.method = 'POST' self.request.POST = { 'email': '*****@*****.**', 'name': 'pyRock', 'stripe_token': '...', 'last_4_digits': '4242', 'password': '******', 'ver_password': '******', } expected_form = UserForm(self.request.POST) expected_form.is_valid() expected_form.addError('[email protected] is already a member') html = render_to_response( 'register.html', { 'form': expected_form, 'months': list(range(1, 12)), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': None, 'years': list(range(2011, 2036)), } ) with mock.patch('stripe.Customer') as stripe_mock: config = {'create.return_value': mock.Mock()} stripe_mock.configure_mock(**config) resp = register(self.request) users = User.objects.filter(email = '*****@*****.**') #self.assertEquals(len(users), 1) self.assertEqual(resp.status_code, 200) self.assertEqual(self.request.session, {}) except IntegrityError: pass
def register(request): print "USERFORM = " + str(UserForm) user = None if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): # subscription billing print "YO!" print "STRIPE_PUBLISHABLE = " + str(settings.STRIPE_PUBLISHABLE) print "stripe_token = " + form.cleaned_data['stripe_token'] customer = stripe.Customer.create( email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan="gold", ) user = User( name=form.cleaned_data['name'], email=form.cleaned_data['email'], last_4_digits=form.cleaned_data['last_4_digits'], stripe_id=customer.id, ) user.set_password(form.cleaned_data['password']) try: user.save() except IntegrityError: print "Already a member bro!" form.addError(user.email + ' is already a member') else: print "Save user payment profile" request.session['user'] = user.pk print request.session['user'] return HttpResponseRedirect('/') else: form = UserForm() return render_to_response( 'register.html', { 'form': form, 'months': range(1, 12), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': range(2011, 2036), }, context_instance=RequestContext(request) )
def register(request): user = None if request.method == "POST": form = UserForm(request.POST) if form.is_valid(): if form.cleaned_data["sub_type"] == "monthly": # update based on your billing method(subscription vs onetime) customer = Customer.create( email=form.cleaned_data["email"], description=form.cleaned_data["name"], card=form.cleaned_data["stripe_token"], plan="gold", ) else: customer = Customer.create( email=form.cleaned_data["email"], description=form.cleaned_data["name"], card=form.cleaned_data["stripe_token"], plan="Platinum", ) cd = form.cleaned_data try: with transaction.atomic(): user = User.create(cd["name"], cd["email"], cd["password"], cd["last_4_digits"]) if customer: user.stripe_id = customer.id user.save() else: UnpaidUsers(email=cd["email"]).save() except IntegrityError: form.addError(cd["email"] + " is already a member") else: request.session["user"] = user.pk return HttpResponseRedirect("/") else: form = UserForm() return render_to_response( "payments/register.html", { "form": form, "months": list(range(1, 12)), "publishable": settings.STRIPE_PUBLISHABLE, "soon": soon(), "user": user, "years": list(range(2011, 2036)), }, context_instance=RequestContext(request), )
def register(request): user = None if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): #update based on your billing method (subscription vs one time) customer = stripe.Customer.create( email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan='gold', ) #customer = stripe.Charge.create( # description = form.cleaned_data['email'] # card=form.cleaned_data['stripe_token'], # amount="5000", # currency="usd" #) cd = form.cleaned_data try: user = User.create( cd['name'], cd['email'], cd['password'], cd['last_4_digits'], customer.id ) except IntegrityError: form.addError(user.email + ' is already a member') user = None else: request.session['user'] = user.pk return HttpResponseRedirect('/') else: form = UserForm() return render_to_response( 'register.html', { 'form':form, 'months':range(1,12), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon':soon(), 'user':user, 'years':range(2011, 2036), }, context_instance=RequestContext(request) )
def register(request): user = None if request.method == "POST": form = UserForm(request.POST) if form.is_valid(): # update based on your billing method (subscription vs one time) customer = stripe.Customer.create( email=form.cleaned_data["email"], description=form.cleaned_data["name"], card=form.cleaned_data["stripe_token"], plan="gold", ) # customer = stripe.Charge.create( # description=form.cleaned_data['email'], # card=form.cleaned_data['stripe_token'], # amount="5000", # currency="usd" # ) user = User( name=form.cleaned_data["name"], email=form.cleaned_data["email"], last_4_digits=form.cleaned_data["last_4_digits"], stripe_id=customer.id, ) # ensure encrypted password user.set_password(form.cleaned_data["password"]) try: user.save() except IntegrityError: form.addError(user.email + " is already a member") else: request.session["user"] = user.pk return HttpResponseRedirect("/") else: form = UserForm() return render_to_response( "register.html", { "form": form, "months": range(1, 13), "publishable": settings.STRIPE_PUBLISHABLE, "soon": soon(), "user": user, "years": range(2015, 2041), }, context_instance=RequestContext(request), )
def test_user_form_passwords_dont_match_throws_error(self): form = UserForm({ 'name': 'jj', 'email': '*****@*****.**', 'password': '******', 'ver_password': '******', # bad password 'last_4_digits': '3333', 'stripe_token': '1' }) # Is the data valid? self.assertFalse(form.is_valid())
def test_user_form_passwords_match(self): form = UserForm( { 'name': 'jj', 'email': '*****@*****.**', 'password': '******', 'ver_password': '******', 'last_4_digits': '3333', 'stripe_token': '1' }) self.assertTrue(form.is_valid(), form.errors) self.assertIsNotNone(form.clean())
def test_user_form_passwords_match(self): form = UserForm({ 'name': 'jj', 'email': '*****@*****.**', 'password': '******', 'ver_password': '******', 'last_4_digits': '3333', 'stripe_token': '1' }) # Is the data valid? if form.is_valid(): # Is the data clean? self.assertTrue(form.cleaned_data)
def test_user_form_passwords_match(self): form = UserForm({ 'name': 'jj', 'email': '*****@*****.**', 'password': '******', 'ver_password': '******', 'last_4_digits': '3333', 'stripe_token': '1' }) self.assertTrue(form.is_valid()) #this will throw an error if it doesn't clean correctly self.assertIsNotNone(form.clean())
def test_user_form_passwords_dont_match_throws_error(self): form = UserForm( { 'name': 'jj', 'email': '*****@*****.**', 'password': '******', # bad pass 'ver_password': '******', 'last_4_digits': '1123', 'stripe_token': '1' }) # Is the data valid? self.assertFalse(form.is_valid())
def test_user_form_passwords_dont_match_throws_error(self): form = UserForm( { "name": "jj", "email": "*****@*****.**", "password": "******", "ver_password": "******", # bad password "last_4_digits": "3333", "stripe_token": "1", } ) # Is the data valid? self.assertFalse(form.is_valid())
def test_user_trow_an_exeption_if_passwords_not_match(self): form = UserForm( { 'name':'jj', 'email':'*****@*****.**', 'password':'******', 'ver_password':'******', 'last_4_digits':'3333', 'stripe_token':'1' } ) self.assertFalse(form.is_valid()) self.assertRaisesMessage(forms.ValidationError, 'Passwords do not match', form.clean)
def test_user_form_passwords_match(self): form = UserForm( { 'name': 'jj', 'email': '*****@*****.**', 'password': '******', 'ver_password': '******', 'last_4_digits': '3333', 'stripe_token': '1'} ) # Is the data valid? if form.is_valid(): # Is the data clean? self.assertTrue(form.cleaned_data)
def test_user_form_passwords_match(self): form = UserForm( { 'name': 'jj', 'email': '*****@*****.**', 'password': '******', 'ver_password': '******', 'last_4_digits': '3333', 'stripe_token': '1' } ) self.assertFalse(form.is_valid()) self.assertRaisesMessagE(forms.ValidationError, "passwords to not match", form.clean)
def test_user_form_passwords_match(self): form = UserForm( { "name": "jj", "email": "*****@*****.**", "password": "******", "ver_password": "******", "last_4_digits": "3333", "stripe_token": "1", } ) # Is the data valid? if form.is_valid(): # Is the data clean? self.assertTrue(form.cleaned_data)
def test_user_form_passwords_match(self): form = UserForm({ 'name': 'jjj', 'email': '*****@*****.**', 'password': '******', 'ver_password': '******', 'last_4_digits': '3333', 'stripe_token': '1' }) # Is the data valid? -- if not print out the errors self.assertTrue(form.is_valid(), form.errors) # This will throw an error if the form doesn't clean correctly self.assertIsNotNone(form.clean())
def test_user_form_passwords_dont_match_throws_error(self): form = UserForm({ 'name': 'jj', 'email': '*****@*****.**', 'password': '******', 'ver_password': '******', 'last_4_digits': '3333', 'stripe_token': '1' }) self.assertFalse(form.is_valid()) from django import forms self.assertRaisesMessage(forms.ValidationError, "Passwords do not match", form.clean)
def test_user_form_passwords_dont_match_get_error(self): #from django.test import SimpleTestCase form = UserForm( { 'name': 'jj', 'email': '*****@*****.**', 'password': '******', #Oops! 'ver_password': '******', 'last_4_digits': '3333', 'stripe_token': '1' } ) self.assertFalse(form.is_valid()) self.assertRaisesMessage(forms.ValidationError, 'Passwords do not match', form.clean)
def test_user_form_passwords_dont_match_throws_error(self): form = UserForm( { "name": "jj", "email": "*****@*****.**", "password": "******", "ver_password": "******", "last_4_digits": "3333", "stripe_token": "1", "sub_type": "yearly", } ) self.assertFalse(form.is_valid()) self.assertRaisesMessage(forms.ValidationError, "Passwords do not match", form.clean)
def test_user_form_passwords_match(self): form = UserForm( { "name": "jj", "email": "*****@*****.**", "password": "******", "ver_password": "******", "last_4_digits": "3333", "stripe_token": "1", "sub_type": "yearly", } ) self.assertTrue(form.is_valid()) # this will throw an error if it doesn't clean correctly self.assertIsNotNone(form.clean())
def test_registering_user_twice_cause_error_msg(self, create_mock): # create expected html self.request.session = {} self.request.method = 'POST' self.request.POST = { 'email':'*****@*****.**', 'name':'pyRock', 'stripe_token':'...', 'last_4_digits':'4242', 'password': '******', 'ver_password': '******', } expected_form = UserForm(self.request.POST) expected_form.is_valid() expected_form.addError('[email protected] is already a member') expected_html = render_to_response( 'payments/register.html', { 'form':expected_form, 'months': range(1,13), 'publishable' : settings.STRIPE_PUBLISHABLE, 'soon':views.soon(), 'user':None, 'years':range(2014, 2036), }, #context_instance=RequestContext(self.request) ) # create tested html with mock.patch('stripe.Customer') as stripe_mock: config = {'create.return_value':mock.Mock()} stripe_mock.config_mock(**config) response = views.register(self.request) # verify that we did things correctly self.assertEquals(response.status_code, 200) self.assertEquals(self.request.session, {}) create_mock.assert_called() # check both htmls are equal self.assertEquals(expected_html.content, response.content)
def register(request): user = None if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): #update based on billing method (subscrip vs one time[add appropriate code for one time]) customer = Customer.create( email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan="gold", ) cd = form.cleaned_data try: user = User.create( cd['name'], cd['email'], cd['password'], cd['last_4_digits'], customer.id ) except IntegrityError: form.addError(cd['email'] + ' is already a member') user = None else: request.session['user'] = user.pk return HttpResponseRedirect('/') else: form = UserForm() return render_to_response( 'register.html', { 'form': form, 'months': list(range(1, 12)), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': list(range(2011, 2036)), }, context_instance=RequestContext(request) )
def test_user_form_passwords_match(self): form = UserForm( { 'name': 'jj', 'email': '*****@*****.**', 'password': '******', 'ver_password': '******', 'last_4_digits': '3333', 'stripe_token': '1' } ) # Check if data is valid, otherwise print errors self.assertTrue(form.is_valid(), form.errors) # throw error if data is not cleaned correctly self.assertIsNotNone(form.clean())
def test_user_form_passwords_match(self): form = UserForm( { 'name': 'Carlos', 'email':'*****@*****.**', 'password':'******', 'ver_password':'******', 'last_4_digits':'2323', 'stripe_token':'1' } ) # is the data valid? self.assertTrue(form.is_valid()) # this will throw an error if the form doesn't clean correctly self.assertIsNotNone(form.clean())
def register(request): user = None if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): #update based on your billing method (subscription vs one time) customer = Customer.create( email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan="gold", ) # customer = stripe.Charge.create( # description=form.cleaned_data['email'], # card=form.cleaned_data['stripe_token'], # amount="5000", # currency="usd" # ) cd = form.cleaned_data from django.db import transaction try: with transaction.atomic(): user = User.create(cd['name'], cd['email'], cd['password'], cd['last_4_digits'], stripe_id="") if customer: user.stripe_id = customer.id user.save() else: UnpaidUsers(email=cd['email']).save() except IntegrityError: import traceback form.addError(cd['email'] + ' is already a member' + traceback.format_exc()) user = None else: request.session['user'] = user.pk return HttpResponseRedirect('/') else: form = UserForm() return render_to_response( 'payments/register.html', { 'form': form, 'months': list(range(1, 12)), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': list(range(2011, 2036)), }, context_instance=RequestContext(request) )
def register(request): user = None if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): customer = stripe.Customer.create( email = form.cleaned_data['email'], description = form.cleaned_data['name'], card = form.cleaned_data['stripe_token'], plan="gold", ) user = User( name = form.cleaned_data['name'], email = form.cleaned_data['email'], last_4_digits = form.cleaned_data['last_4_digits'], stripe_id = customer.id, password = form.cleaned_data['password'] ) try: user.save() except IntegrityError: form.addError(user.email + ' is alraedy a member') else: request.session['user'] = user.pk return HttpResponseRedirect('/') else: form = UserForm() return render_to_response( 'register.html', { 'form': form, 'months': range(1, 12), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': range(2011, 2036), }, context_instance=RequestContext(request) )
def setUpClass(cls): html = render_to_response( 'register.html', { 'form': UserForm(), 'months': range(1, 12), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': None, 'years': range(2011, 2036), }) ViewTesterMixin.setupViewTester( '/register', register, html.content, )
def register(request): user = None if request.method == 'POST': form = UserForm(request.POST) if form.is_valid(): # Update based on billing method customer = stripe.Customer.create( email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan="gold", ) # customer = stripe.Charge.create( # description=form.cleaned_data['email'], # card=form.cleaned_data['stripe_token'] # amount="5000", # currency="usd", # ) user = User( name=form.cleaned_data['name'], email=form.cleaned_data['email'], last_4_digits=form.cleaned_data['last_4_digits'], stripe_id=customer.id, password=form.cleaned_data['password'], ) # user.set_password(form.cleaned_data['password']) try: user.save() except IntegrityError: form.addError(user.email + 'is already a member') else: request.session['user'] = user.pk return HttpResponseRedirect('/') else: form = UserForm() return render_to_response( 'register.html', { 'form': form, 'months': range(1, 12), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': range(2014, 2039), }, context_instance=RequestContext(request), )
def post_user(request): form = UserForm(request.data) if form.is_valid(): try: # update based on your billing method (subscription vs one time) customer = Customer.create( "subscription", email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan="gold", ) except Exception as exp: form.addError(exp) cd = form.cleaned_data try: with transaction.atomic(): user = User.create(cd['name'], cd['email'], cd['password'], cd['last_4_digits'], stripe_id='') if customer: user.stripe_id = customer.id user.save() else: UnpaidUsers(email=cd['email']).save() except IntegrityError: form.addError(cd['email'] + ' is already a member') else: request.session['user'] = user.pk resp = {"status": "ok", "url": '/'} return Response(resp, content_type="application/json") resp = {"status": "fail", "errors": form.non_field_errors()} return Response(resp) else: # for not valid resp = {"status": "form-invalid", "errors": form.errors} return Response(resp)
def post_user(request): form = UserForm(request.DATA) print("in post user") if form.is_valid(): try: # update based on your billing method (subscription vs one time) customer = Customer.create( "subscription", email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan="gold", ) except Exception as exp: form.addError(exp) cd = form.cleaned_data try: with transaction.atomic(): user = User.create( cd['name'], cd['email'], cd['password'], cd['last_4_digits']) if customer: user.stripe_id = customer.id user.save() else: UnpaidUsers(email=cd['email']).save() except IntegrityError as e: print("----------Integristy error") print(e) form.addError(cd['email'] + ' is already a member') else: request.session['user'] = user.pk resp = {"status": "ok", "url": '/'} return Response(resp, content_type="application/json") resp = {"status": "fail", "errors": form.non_field_errors()} return Response(resp) else: # for not valid resp = {"status": "form-invalid", "errors": form.errors} return Response(resp)
def register(request): user = None if request.method == 'POST': # We only talk AJAX posts now if not request.is_ajax(): return HttpResponseBadRequest("I only speak AJAX nowadays") data = json.loads(request.body.decode()) form = UserForm(data) if form.is_valid(): try: customer = Customer.create( "subscription", email=form.cleaned_data['email'], description=form.cleaned_data['name'], card=form.cleaned_data['stripe_token'], plan="gold", ) except Exception as exp: form.addError(exp) cd = form.cleaned_data try: with transaction.atomic(): user = User.create(cd['name'], cd['email'], cd['password'], cd['last_4_digits'], stripe_id="") if customer: user.stripe_id = customer.id user.save() else: UnpaidUsers(email=cd['email']).save() except IntegrityError: resp = json.dumps({ "status": "fail", "errors": cd['email'] + ' is already a member' }) else: request.session['user'] = user.pk resp = json.dumps({"status": "ok", "url": '/'}) return HttpResponse(resp, content_type="application/json") else: # form not valid resp = json.dumps({ "status": "form-invalid", "errors": form.errors }) return HttpResponse(resp, content_type="application/json") else: form = UserForm() return render_to_response('payments/register.html', { 'form': form, 'months': list(range(1, 12)), 'publishable': settings.STRIPE_PUBLISHABLE, 'soon': soon(), 'user': user, 'years': list(range(2011, 2036)), }, context_instance=RequestContext(request))