def test_address_list_page_returns_only_owned(self): user1 = CustomUser.objects.create_user( email='*****@*****.**', name='test user1', password='******', ) user2 = CustomUser.objects.create_user( email='*****@*****.**', name='test user2', password='******', ) address1 = Address( user=user1, city='swax', ) address2 = Address( user=user2, city='swaxasd', ) self.client.login(email='*****@*****.**', password='******') response = self.client.get(reverse('accounts:address-list')) self.assertEqual(response.status_code, 200) address_list = Address.objects.filter(user=user1) self.assertEqual(list(response.context['object_list']), list(address_list))
def test_correct_coordinates(self): try: address = Address(position=Point(48.8613232, 2.3631101), address1='11, rue debelleyme', patron_id=1, country='FR', city='Paris', zipcode='75003') address.full_clean() except ValidationError, e: self.fail(e)
def post(self, request, *args, **kwargs): form = CheckoutForm(request.POST) cart = Cart.objects.get(user=request.user, ordered=False) use_default_ship = request.POST.get('use_default_ship') if not use_default_ship and not form.is_valid(): pass else: if use_default_ship: address = Address.objects.get(user=request.user, save_ship=True) elif form.is_valid(): # update default shipping address for this user save_ship = form.cleaned_data.get('save_ship') if save_ship: try: previous_saved = Address.objects.get(user=request.user, save_ship=True) previous_saved.save_ship = False previous_saved.save() except ObjectDoesNotExist: pass address = Address( user=request.user, first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), email=form.cleaned_data.get('email'), address=form.cleaned_data.get('address'), address2=form.cleaned_data.get('address2'), city=form.cleaned_data.get('city'), state=form.cleaned_data.get('state'), country=form.cleaned_data.get('country'), zip=form.cleaned_data.get('zip'), save_ship=save_ship, ) address.save() # get + update or create order, don't forget to save order try: order = Order.objects.get(user=request.user, ordered=False) order.address = address except ObjectDoesNotExist: order = Order( user=request.user, cart=cart, address=address, ref_code=create_ref_code(), ) order.save() # payment_option = form.cleaned_data.get('payment_option') return redirect("orders:payment") context = {'form': form} return render(request, self.template_name, context)
def test_within(self): self.assertTrue(Address.within('a', 'a')) self.assertTrue(Address.within('a/aa', 'a')) self.assertFalse(Address.within('a', 'a/aa')) self.assertTrue(Address.within('foo/bar/noo', 'foo/bar/noo')) self.assertTrue(Address.within('foo/bar/noo', 'foo/bar')) self.assertTrue(Address.within('foo/bar/noo', 'foo')) self.assertFalse(Address.within('foo/bar/noo', 'food')) self.assertFalse(Address.within('foo/bar/noo', 'bar/foo')) self.assertFalse(Address.within('foo/bar/noo', 'bar'))
def make_address(self, user): return Address(patron=user, address1=self.address1, address2=self.address2, city=self.city, zipcode=self.zipcode, state=self.state)
def test_longitude_too_low(self): address = Address(position=Point(40, -270), address1='11, rue debelleyme', patron_id=1, country='FR', city='Paris', zipcode='75003') self.assertRaises(ValidationError, address.full_clean)
def test_address_string_representation(self): address = Address( user=self.user, zip_code='187', city='Sumbawanga', ) self.assertEqual(str(address), self.user.name)
def add_customer(request): form = RegistrationFormAdmin() if request.method == "POST": form = RegistrationFormAdmin(request.POST) if form.is_valid(): username = generate_username( form.cleaned_data['first_name'], form.cleaned_data['last_name'], ) random_password = get_pronounceable_password() user = User.objects.create_user(username, form.cleaned_data['email'], random_password) user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] user.is_active = True user.save() userprofile = UserProfile() userprofile.title = form.cleaned_data['title'] userprofile.date_of_birth = form.cleaned_data['date_of_birth'] userprofile.phone = form.cleaned_data['phone'] userprofile.terms = form.cleaned_data['terms'] userprofile.user = user userprofile.save() address = Address() address.line_1 = form.cleaned_data['line_1'] address.line_2 = form.cleaned_data['line_2'] address.city = form.cleaned_data['city'] address.county = form.cleaned_data['county'] address.postcode = form.cleaned_data['postcode'] address.user = user address.save() send_admin_welcome_email(user, random_password) return HttpResponseRedirect('/admin/accounts/userprofile/') else: form = RegistrationFormAdmin(request.POST) return render_to_response('admin/accounts/add_customer.html', {'form': form}, context_instance=RequestContext(request))
def add_customer(request): form = RegistrationFormAdmin() if request.method == "POST": form = RegistrationFormAdmin(request.POST) if form.is_valid(): username = generate_username(form.cleaned_data['first_name'], form.cleaned_data['last_name'], ) random_password = get_pronounceable_password() user = User.objects.create_user( username, form.cleaned_data['email'], random_password ) user.first_name = form.cleaned_data['first_name'] user.last_name = form.cleaned_data['last_name'] user.is_active = True user.save() userprofile = UserProfile() userprofile.title = form.cleaned_data['title'] userprofile.date_of_birth = form.cleaned_data['date_of_birth'] userprofile.phone = form.cleaned_data['phone'] userprofile.terms = form.cleaned_data['terms'] userprofile.user = user userprofile.save() address = Address() address.line_1 = form.cleaned_data['line_1'] address.line_2 = form.cleaned_data['line_2'] address.city = form.cleaned_data['city'] address.county = form.cleaned_data['county'] address.postcode = form.cleaned_data['postcode'] address.user = user address.save() send_admin_welcome_email(user, random_password) return HttpResponseRedirect('/admin/accounts/userprofile/') else: form = RegistrationFormAdmin(request.POST) return render_to_response('admin/accounts/add_customer.html', {'form': form}, context_instance=RequestContext(request))
def edit_customer(request, profile_id): if profile_id is not None: user_profile = get_object_or_404(UserProfile, pk=profile_id) user = user_profile.user try: address = Address.objects.get(user=user) except Address.DoesNotExist: address = None if request.POST: userprofile_form = UserProfileForm(request.POST, ) if userprofile_form.is_valid(): user_profile.title = userprofile_form.cleaned_data['title'] user_profile.date_of_birth = userprofile_form.cleaned_data[ 'date_of_birth'] user_profile.phone = userprofile_form.cleaned_data['phone'] user_profile.save() address_form = AddressForm(request.POST) if address_form.is_valid(): if not address: address = Address() address.user = user address.line_1 = address_form.cleaned_data['line_1'] address.line_2 = address_form.cleaned_data['line_2'] address.city = address_form.cleaned_data['city'] address.county = address_form.cleaned_data['county'] address.postcode = address_form.cleaned_data['postcode'] address.save() return render_to_response( 'admin/accounts/edit_customer.html', { 'userprofile_form': UserProfileForm(instance=user_profile), 'address_form': AddressForm(instance=address), 'user_profile': user_profile, }, RequestContext(request))
def edit_customer(request, profile_id): if profile_id is not None: user_profile = get_object_or_404(UserProfile, pk=profile_id) user = user_profile.user try: address = Address.objects.get(user=user) except Address.DoesNotExist: address = None if request.POST: userprofile_form = UserProfileForm(request.POST,) if userprofile_form.is_valid(): user_profile.title = userprofile_form.cleaned_data['title'] user_profile.date_of_birth = userprofile_form.cleaned_data['date_of_birth'] user_profile.phone = userprofile_form.cleaned_data['phone'] user_profile.save() address_form = AddressForm(request.POST) if address_form.is_valid(): if not address: address = Address() address.user = user address.line_1 = address_form.cleaned_data['line_1'] address.line_2 = address_form.cleaned_data['line_2'] address.city = address_form.cleaned_data['city'] address.county = address_form.cleaned_data['county'] address.postcode = address_form.cleaned_data['postcode'] address.save() return render_to_response('admin/accounts/edit_customer.html', { 'userprofile_form': UserProfileForm(instance=user_profile), 'address_form': AddressForm(instance=address), 'user_profile' : user_profile, }, RequestContext(request) )
def test_address_string_representation(self): user = User.objects.create_user( email='*****@*****.**', name='test user1', password='******', ) address = Address( user=user, name='Someone Moro', address1='Majengo - Sumbawanga', address2='', zip_code='187', city='Sumbawanga', country='TZ', ) self.assertEqual( str(address), 'Someone Moro, Majengo - Sumbawanga, , 187, Sumbawanga, TZ')
def post(self, request, *args, **kwargs): userprofile = request.user.userprofile form = PaymentForm(request.POST) order = Order.objects.get(user=request.user, ordered=False) cart = order.cart token = request.POST.get('stripeToken') use_same_address = request.POST.get('use_same_address') use_default_bill = request.POST.get('use_default_bill') if not use_same_address and not use_default_bill and not form.is_valid( ): pass else: if use_same_address: address = order.address elif use_default_bill: address = userprofile.default_bill elif form.is_valid(): address = Address( user=request.user, first_name=form.cleaned_data.get('first_name'), last_name=form.cleaned_data.get('last_name'), email=form.cleaned_data.get('email'), address=form.cleaned_data.get('address'), address2=form.cleaned_data.get('address2'), city=form.cleaned_data.get('city'), state=form.cleaned_data.get('state'), country=form.cleaned_data.get('country'), zip=form.cleaned_data.get('zip'), ) address.save() # update default billing address for this user save_bill = form.cleaned_data.get('save_bill') print(save_bill) print(form.cleaned_data) if save_bill: userprofile.default_bill = address userprofile.save() try: charge = stripe.Charge.create( amount=int(cart.get_total_price() * 100), #cents currency="cad", source=token, ) payment = Payment( user=request.user, charge_id=charge['id'], amount=cart.get_total_price(), ordered_date=timezone.now(), address=address, ) payment.save() order.payment = payment order.ordered = True order.save() cart.ordered = True cart.save() for order_item in cart.items.all(): order_item.ordered = True order_item.save() messages.success(request, "Your order has been successfully placed.") return redirect('/') except stripe.error.CardError as e: messages.error(request, e.error.message) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) except stripe.error.RateLimitError as e: messages.error(request, e.error.message) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) except stripe.error.InvalidRequestError as e: messages.error(request, e.error.message) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) except stripe.error.AuthenticationError as e: messages.error(request, e.error.message) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) except stripe.error.APIConnectionError as e: messages.error(request, e.error.message) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) except stripe.error.StripeError as e: messages.error(request, e.error.message) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) except Exception as e: messages.error( request, str(e) + "A serious error occurred. We have been notifed.") return HttpResponseRedirect(request.META.get('HTTP_REFERER')) messages.error(self.request, "Invalid data received.") context = {'form': form} return render(request, self.template_name, context)
def test_find(self): self.assertEqual(Address.find('aaa'), self.address_1) self.assertEqual(Address.find('aaa/ddd'), self.address_1) self.assertEqual(Address.find('aaa/bbb'), self.address_2) self.assertEqual(Address.find('foo'), None)
def login_register(request, *args): next_url = '/' message = None if request.GET.has_key('next'): next_url = request.GET.get('next') if request.method == 'POST': if "register" in request.POST: registration_form = RegistrationForm(request.POST) if registration_form.is_valid(): username = generate_username( registration_form.cleaned_data['first_name'], registration_form.cleaned_data['last_name']) user = User.objects.create_user( username, registration_form.cleaned_data['email'], registration_form.cleaned_data['password'], ) user.first_name = registration_form.cleaned_data['first_name'] user.last_name = registration_form.cleaned_data['last_name'] user.is_active = True user.save() user_profile = UserProfile() user_profile.title = registration_form.cleaned_data['title'] user_profile.date_of_birth = registration_form.cleaned_data[ 'date_of_birth'] user_profile.phone = registration_form.cleaned_data['phone'] user_profile.user = user user_profile.terms = registration_form.cleaned_data['terms'] user_profile.save() # Billing address address = Address() address.line_1 = registration_form.cleaned_data['line_1'] address.line_2 = registration_form.cleaned_data['line_2'] address.city = registration_form.cleaned_data['city'] address.county = registration_form.cleaned_data['county'] address.postcode = registration_form.cleaned_data['postcode'] address.user = user address.save() messages.info( request, "Congratulations, you have successfully registered with Pleasures All Mine." ) send_welcome_email(user_profile.user) user = authenticate( username=registration_form.cleaned_data['email'], password=registration_form.cleaned_data['password']) login_after_registration(request, user) return HttpResponseRedirect(next_url) else: registration_form = RegistrationForm(data=request.POST) messages.warning( request, "Sorry, but you missed something, please fill all required fields." ) if 'login' in request.POST: login_form = LoginForm(data=request.POST) if login_form.is_valid(): user = authenticate( username=login_form.cleaned_data['username'], password=login_form.cleaned_data['password']) if user is not None: if not request.POST.get('remember_me', None): request.session.set_expiry(0) messages.success( request, "Welcome to Pleasures All Mine, you have successfully logged in." ) return login( request, authentication_form=LoginForm, ) else: login_form = LoginForm() registration_form = RegistrationForm() messages.warning( request, "Sorry, but you missed something, please fill all required fields." ) else: messages.warning( request, "Sorry, but the username or password you have entered is incorrect, please try again." ) login_form = LoginForm() registration_form = RegistrationForm() else: login_form = LoginForm() else: registration_form = RegistrationForm() login_form = LoginForm() return render_to_response( 'accounts/login_register.html', { 'registration_form': registration_form, 'login_form': login_form, 'message': message, 'next_url': next_url, }, RequestContext(request))
def post(self, *args, **kwargs): form = CheckoutForm(self.request.POST or None) try: order = Order.objects.get(user=self.request.user, ordered=False) if form.is_valid(): use_default_shipping = form.cleaned_data.get( 'use_default_shipping') if use_default_shipping: print("Using the defualt shipping address") address_qs = Address.objects.filter(user=self.request.user, address_type='S', default=True) if address_qs.exists(): shipping_address = address_qs[0] order.shipping_address = shipping_address order.save() else: messages.info(self.request, "No default shipping address available") return redirect('payments:checkout') else: shipping_address1 = form.cleaned_data.get( 'shipping_address') shipping_address2 = form.cleaned_data.get( 'shipping_address2') shipping_number = form.cleaned_data.get('shipping_number') shipping_city = form.cleaned_data.get('shipping_city') shipping_zip = form.cleaned_data.get('shipping_zip') if is_valid_form([ shipping_address1, shipping_number, shipping_city, shipping_zip ]): print('shipping-valid') shipping_address = Address( user=self.request.user, street_address=shipping_address1, apartment_address=shipping_address2, number=shipping_number, city=shipping_city, zip=shipping_zip, address_type='S') shipping_address.save() order.shipping_address = shipping_address order.save() set_default_shipping = form.cleaned_data.get( 'set_default_shipping') if set_default_shipping: shipping_address.default = True shipping_address.save() else: messages.info( self.request, "Please fill in the required shipping address fields" ) use_default_billing = form.cleaned_data.get( 'use_default_billing') same_billing_address = form.cleaned_data.get( 'same_billing_address') if same_billing_address: billing_address = shipping_address billing_address.pk = None billing_address.save() billing_address.address_type = 'B' billing_address.save() order.billing_address = billing_address order.save() elif use_default_billing: print("Using the defualt billing address") address_qs = Address.objects.filter(user=self.request.user, address_type='B', default=True) if address_qs.exists(): billing_address = address_qs[0] order.billing_address = billing_address order.save() else: messages.info(self.request, "No default billing address available") return redirect('payments:checkout') else: print("User is entering a new billing address") billing_address1 = form.cleaned_data.get('billing_address') billing_address2 = form.cleaned_data.get( 'billing_address2') billing_number = form.cleaned_data.get('billing_number') billing_city = form.cleaned_data.get('billing_city') billing_zip = form.cleaned_data.get('billing_zip') if is_valid_form([ billing_address1, billing_number, billing_city, billing_zip ]): print('billing-valid') billing_address = Address( user=self.request.user, street_address=billing_address1, apartment_address=billing_address2, city=billing_city, number=billing_number, zip=billing_zip, address_type='B') billing_address.save() order.billing_address = billing_address order.save() set_default_billing = form.cleaned_data.get( 'set_default_billing') if set_default_billing: billing_address.default = True billing_address.save() else: messages.info( self.request, "Please fill in the required billing address fields" ) payment_option = form.cleaned_data.get('payment_option') if payment_option == 'O': return redirect('payments:payment', payment_option='online') elif payment_option == 'C': return redirect('payments:payment', payment_option='cod') else: messages.warning(self.request, "Invalid payment option selected") return redirect('payments:checkout') except ObjectDoesNotExist: messages.warning(self.request, "You do not have an active order") return redirect("orders:order-summary")
def login_register(request, *args): next_url = '/' message = None if request.GET.has_key('next'): next_url = request.GET.get('next') if request.method == 'POST': if "register" in request.POST: registration_form = RegistrationForm(request.POST) if registration_form.is_valid(): username = generate_username( registration_form.cleaned_data['first_name'], registration_form.cleaned_data['last_name'] ) user = User.objects.create_user( username, registration_form.cleaned_data['email'], registration_form.cleaned_data['password'], ) user.first_name = registration_form.cleaned_data['first_name'] user.last_name = registration_form.cleaned_data['last_name'] user.is_active = True user.save() user_profile = UserProfile() user_profile.title = registration_form.cleaned_data['title'] user_profile.date_of_birth = registration_form.cleaned_data['date_of_birth'] user_profile.phone = registration_form.cleaned_data['phone'] user_profile.user = user user_profile.terms = registration_form.cleaned_data['terms'] user_profile.save() # Billing address address = Address() address.line_1 = registration_form.cleaned_data['line_1'] address.line_2 = registration_form.cleaned_data['line_2'] address.city = registration_form.cleaned_data['city'] address.county = registration_form.cleaned_data['county'] address.postcode = registration_form.cleaned_data['postcode'] address.user = user address.save() messages.info(request, "Congratulations, you have successfully registered with Pleasures All Mine.") send_welcome_email(user_profile.user) user = authenticate( username=registration_form.cleaned_data['email'], password=registration_form.cleaned_data['password'] ) login_after_registration(request, user) return HttpResponseRedirect(next_url) else: registration_form = RegistrationForm(data=request.POST) messages.warning(request, "Sorry, but you missed something, please fill all required fields.") if 'login' in request.POST: login_form = LoginForm(data=request.POST) if login_form.is_valid(): user = authenticate(username=login_form.cleaned_data['username'], password=login_form.cleaned_data['password']) if user is not None: if not request.POST.get('remember_me', None): request.session.set_expiry(0) messages.success(request, "Welcome to Pleasures All Mine, you have successfully logged in.") return login(request, authentication_form=LoginForm,) else: login_form = LoginForm() registration_form = RegistrationForm() messages.warning(request, "Sorry, but you missed something, please fill all required fields.") else: messages.warning(request, "Sorry, but the username or password you have entered is incorrect, please try again.") login_form = LoginForm() registration_form = RegistrationForm() else: login_form = LoginForm() else: registration_form = RegistrationForm() login_form = LoginForm() return render_to_response( 'accounts/login_register.html', { 'registration_form': registration_form, 'login_form': login_form, 'message': message, 'next_url': next_url, }, RequestContext(request) )