def test_checkout_with_4_line_shipping_address(self): # login as our customer logged_in = self.client.login(username=self.username, password=self.password) self.assertEqual(logged_in, True) # test that our Netherlands form has only 4 address line fields nl_form_class = form_factory("NL") nl_form = nl_form_class() self.assertEqual('state' in nl_form.fields, False) self.assertEqual('code' in nl_form.fields, True) # check initial database quantities self.assertEquals(Address.objects.count(), 2) self.assertEquals(Customer.objects.count(), 1) self.assertEquals(Order.objects.count(), 0) # check we have no invoice or shipping phone or email prior to checkout our_customer = Customer.objects.all()[0] self.assertEqual(our_customer.selected_invoice_address.phone, None) self.assertEqual(our_customer.selected_invoice_address.email, None) self.assertEqual(our_customer.selected_shipping_address.phone, None) self.assertEqual(our_customer.selected_shipping_address.email, None) checkout_data = {'invoice-firstname': 'bob', 'invoice-lastname': 'builder', 'invoice-line1': 'de company', 'invoice-line2': 'de street', 'invoice-city': 'de area', 'invoice-state': 'de town', 'invoice-code': '1234AB', 'invoice-country': "NL", 'invoice-email': '*****@*****.**', 'invoice-phone': '1234567', 'shipping-firstname': 'hans', 'shipping-lastname': 'schmidt', 'shipping-line1': 'orianenberger strasse', 'shipping-line2': 'de town', 'shipping-city': 'stuff', 'shipping-state': 'BE', 'shipping-code': '1234AB', 'shipping-country': "NL", 'shipping-email': '*****@*****.**', 'shipping-phone': '7654321', 'payment_method': self.by_invoice.id, } checkout_post_response = self.client.post(reverse('lfs_checkout'), checkout_data) self.assertRedirects(checkout_post_response, reverse('lfs_thank_you'), status_code=302, target_status_code=200,) # check database quantities post-checkout self.assertEquals(Address.objects.count(), 4) self.assertEquals(Customer.objects.count(), 1) self.assertEquals(Order.objects.count(), 1) # check our customer details post checkout our_customer = Customer.objects.all()[0] self.assertEqual(our_customer.selected_invoice_address.phone, "1234567") self.assertEqual(our_customer.selected_invoice_address.email, "*****@*****.**") self.assertEqual(our_customer.selected_shipping_address.phone, '7654321') self.assertEqual(our_customer.selected_shipping_address.email, "*****@*****.**")
def address_inline(request, prefix, form): """displays the invoice address with localized fields """ template_name = "lfs/customer/" + prefix + "_address_inline.html" country_code = get_country_code(request, prefix) if country_code != '': shop = lfs.core.utils.get_default_shop(request) countries = None if prefix == INVOICE_PREFIX: countries = shop.invoice_countries.all() else: countries = shop.shipping_countries.all() customer = customer_utils.get_or_create_customer(request) address_form_class = form_factory(country_code) if request.method == 'POST': if POSTAL_ADDRESS_L10N == True: address_form = address_form_class(prefix=prefix, data=request.POST,) else: address_form = PostalAddressForm(prefix=prefix, data=request.POST,) if countries is not None: address_form.fields["country"].choices = [(c.code.upper(), c.name) for c in countries] save_address(request, customer, prefix) else: # If there are addresses intialize the form. initial = {} customer_selected_address = None if hasattr(customer, 'selected_' + prefix + '_address'): customer_selected_address = getattr(customer, 'selected_' + prefix + '_address') if customer_selected_address is not None: initial.update({ "line1": customer_selected_address.company_name, "line2": customer_selected_address.street, "city": customer_selected_address.city, "state": customer_selected_address.state, "code": customer_selected_address.zip_code, "country": customer_selected_address.country.code.upper(), }) address_form = address_form_class(prefix=prefix, initial=initial) else: address_form = address_form_class(prefix=prefix) address_form.fields["country"].initial = country_code if countries is not None: address_form.fields["country"].choices = [(c.code.upper(), c.name) for c in countries] # Removes fields from address form if requested via settings. for i in range(1, 6): address_settings = getattr(settings, "POSTAL_ADDRESS_LINE%s" % i, None) try: if address_settings and address_settings[2] == False: del address_form.fields["line%s" % i] except IndexError: pass # if request via ajax don't display validity errors if request.is_ajax(): address_form._errors = {} return render_to_string(template_name, RequestContext(request, { "address_form": address_form, "form": form, }))
def test_get_mx_address(self): """ Tests that we get the correct widget for Mexico """ mx_form_class = form_factory("mx") self.assertNotEqual(mx_form_class, None) # only use required fields test_data = { 'line1': 'Avenida Reforma', 'line2': '1110', 'line3': 'Centro', 'city': 'Puebla', 'state': 'Puebla', 'code': '12345' } form = mx_form_class(data=test_data) self.assertEqual(form.fields['line1'].label.lower(), 'street') self.assertEqual(form.fields['line2'].label.lower(), 'number') self.assertEqual(form.fields['city'].label.lower(), 'city') self.assertEqual(form.fields['state'].label.lower(), 'state') self.assertEqual(form.fields['code'].label.lower(), 'zip code') from localflavor.mx.forms import MXStateSelect, MXZipCodeField self.assertIsInstance(form.fields['state'].widget, MXStateSelect) self.assertIsInstance(form.fields['code'], MXZipCodeField)
def address_inline(request, prefix, form): """displays the invoice address with localized fields """ template_name = "lfs/customer/" + prefix + "_address_inline.html" country_code = get_country_code(request, prefix) if country_code != '': shop = lfs.core.utils.get_default_shop(request) countries = None if prefix == INVOICE_PREFIX: countries = shop.invoice_countries.all() else: countries = shop.shipping_countries.all() customer = customer_utils.get_or_create_customer(request) address_form_class = form_factory(country_code) if request.method == 'POST': if POSTAL_ADDRESS_L10N == True: address_form = address_form_class(prefix=prefix, data=request.POST,) else: address_form = PostalAddressForm(prefix=prefix, data=request.POST,) if countries is not None: address_form.fields["country"].choices = [(c.code.upper(), c.name) for c in countries] else: # If there are addresses intialize the form. initial = {} customer_selected_address = None if hasattr(customer, 'selected_' + prefix + '_address'): customer_selected_address = getattr(customer, 'selected_' + prefix + '_address') if customer_selected_address is not None: initial.update({ "line1": customer_selected_address.line1, "line2": customer_selected_address.line2, "city" : customer_selected_address.city, "state": customer_selected_address.state, "code": customer_selected_address.zip_code, "country": customer_selected_address.country.code.upper(), }) address_form = address_form_class(prefix=prefix, initial=initial) else: address_form = address_form_class(prefix=prefix) address_form.fields["country"].initial = country_code if countries is not None: address_form.fields["country"].choices = [(c.code.upper(), c.name) for c in countries] # Removes fields from address form if requested via settings. for i in range(1, 6): address_settings = getattr(settings, "POSTAL_ADDRESS_LINE%s" % i, None) try: if address_settings and address_settings[2] == False: del address_form.fields["line%s" % i] except IndexError: pass # if request via ajax don't display validity errors if request.is_ajax(): address_form._errors = {} return render_to_string(template_name, RequestContext(request, { "address_form": address_form, "form": form, "settings": settings, }))
def address_inline(request, prefix="", country_code=None, template_name="postal/form.html"): """ Displays postal address with localized fields """ country_prefix = "country" prefix = request.POST.get('prefix', prefix) if prefix: country_prefix = prefix + '-country' country_code = request.POST.get(country_prefix, country_code) form_class = form_factory(country_code=country_code) if request.method == "POST": data = {} for (key, val) in request.POST.items(): if val is not None and len(val) > 0: data[key] = val data.update({country_prefix: country_code}) form = form_class(prefix=prefix, initial=data) else: form = form_class(prefix=prefix) return render_to_string( template_name, RequestContext(request, { "form": form, "prefix": prefix, }))
def address_inline(request, prefix="", country_code=None, template_name="postal/form.html"): """ Displays postal address with localized fields """ country_prefix = "country" prefix = request.POST.get('prefix', prefix) if prefix: country_prefix = prefix + '-country' country_code = request.POST.get(country_prefix, country_code) form_class = form_factory(country_code=country_code) if request.method == "POST": data = {} for (key, val) in request.POST.items(): if val is not None and len(val) > 0: data[key] = val data.update({country_prefix: country_code}) form = form_class(prefix=prefix, initial=data) else: form = form_class(prefix=prefix) return render_to_string(template_name, RequestContext(request, { "form": form, "prefix": prefix, }))
def render(self, request, country_iso=None): """ Renders the postal and the additional address form. """ if country_iso is None: country_iso = self.address.country.code.upper() form_model = form_factory(country_iso) postal_form = form_model(initial=self.get_address_as_dict(), data=self.data, prefix=self.type) countries = self.get_countries(request) postal_form.fields["country"].choices = [(c.code.upper(), c.name) for c in countries] address_form_model = self.get_form_model() address_form = address_form_model(instance=self.address, data=self.data, prefix=self.type) templates = ["lfs/addresses/address_form.html"] templates.insert(0, "lfs/addresses/%s_address_form.html" % self.type) template = select_template(templates) return template.render( RequestContext(request, { "postal_form": postal_form, "address_form": address_form, }))
def is_valid(self): """ Returns True if the postal and the additional form is valid. """ if self.type == "shipping" and self.data.get("no_shipping"): return True if self.data: form_model = form_factory("%s-country" % self.type) else: form_model = form_factory(self.address.country.code.upper()) postal_form = form_model(data=self.data, initial=self.get_address_as_dict(), prefix=self.type) address_form_model = self.get_form_model() address_form = address_form_model(data=self.data, instance=self.address, prefix=self.type) return postal_form.is_valid() and address_form.is_valid()
def is_valid(self): """ Returns True if the postal and the additional form is valid. """ if self.type == CHECKOUT_NOT_REQUIRED_ADDRESS and self.data.get("no_%s" % CHECKOUT_NOT_REQUIRED_ADDRESS): return True if self.data: form_model = form_factory(self.data.get("%s-country" % self.type, self.address.country.code.upper())) else: form_model = form_factory(self.address.country.code.upper()) postal_form = form_model(data=self.data, initial=self.get_address_as_dict(), prefix=self.type) address_form_model = self.get_form_model() address_form = address_form_model(data=self.data, instance=self.address, prefix=self.type) return postal_form.is_valid() and address_form.is_valid()
def test_4_line_address(self): netherlands_form_class = form_factory("nl") self.assertNotEqual(netherlands_form_class, None) test_data = {'code': '1234AB'} form = netherlands_form_class(data=test_data) self.assertEqual(form.fields['line1'].label.lower(), "street") self.assertEqual(form.fields['line2'].label.lower(), "area") self.assertEqual(form.fields['city'].label.lower(), "town/city") self.assertEqual(form.fields.get('state'), None) self.assertEqual(form.fields['code'].label.lower(), "zip code")
def read(self, request): iso_code = request.GET.get('country', '') json = {} form_class = form_factory(country_code=iso_code) form_obj = form_class() for k, v in form_obj.fields.items(): if k not in json.keys(): json[k] = {} json[k]['label'] = unicode(v.label) json[k]['widget'] = v.widget.render(k, "", attrs={'id': 'id_' + k}) return json
def test_incorrect_country_code(self): """ Tests that we don't throw an exception for an incorrect country code """ no_country_form_class = form_factory("xx") self.assertNotEqual(no_country_form_class, None) form = no_country_form_class() self.assertEqual(form.fields['line1'].label.lower(), "street") self.assertEqual(form.fields['line2'].label.lower(), "area") self.assertEqual(form.fields['city'].label.lower(), "city") self.assertEqual(form.fields['state'].label.lower(), "state") self.assertEqual(form.fields['code'].label.lower(), "zip code")
def test_get_de_address(self): """ Tests that we get the correct widget for Germny """ german_form_class = form_factory("de") self.assertNotEqual(german_form_class, None) # only use required fields test_data = {'code': '12345',} form = german_form_class(data=test_data) self.assertEqual(form.fields['line1'].label.lower(), "street") self.assertEqual(form.fields.has_key('line2'), False) self.assertEqual(form.fields['city'].label.lower(), "city") self.assertEqual(form.fields['code'].label.lower(), "zip code")
def save_address(request, customer, prefix): # get the shop shop = muecke.core.utils.get_default_shop(request) # get the country for the address country_iso = request.POST.get(prefix + "-country", shop.default_country.code) # check have we a valid address form_class = form_factory(country_iso) valid_address = False form_obj = form_class(request.POST, prefix=prefix) if form_obj.is_valid(): valid_address = True customer_selected_address = None address_attribute = 'selected_' + prefix + '_address' existing_address = False if hasattr(customer, address_attribute): customer_selected_address = getattr(customer, address_attribute) if customer_selected_address is not None: existing_address = True customer_selected_address.line1 = request.POST.get( prefix + "-line1", "") customer_selected_address.line2 = request.POST.get( prefix + "-line2", "") customer_selected_address.city = request.POST.get( prefix + "-city", "") customer_selected_address.state = request.POST.get( prefix + "-state", "") customer_selected_address.zip_code = request.POST.get( prefix + "-code", "") customer_selected_address.country = Country.objects.get( code=country_iso.lower()) customer_selected_address.save() if not existing_address: # no address exists for customer so create one customer_selected_address = Address.objects.create( customer=customer, line1=request.POST.get(prefix + "-line1", ""), line2=request.POST.get(prefix + "-line2", ""), city=request.POST.get(prefix + "-city", ""), state=request.POST.get(prefix + "-state", ""), zip_code=request.POST.get(prefix + "-code", ""), country=Country.objects.get(code=country_iso.lower())) setattr(customer, address_attribute, customer_selected_address) customer.save() return valid_address
def test_get_ie_address(self): """ Tests that we get the correct widget for Ireland """ irish_form_class = form_factory("ie") self.assertNotEqual(irish_form_class, None) # only use required fields test_data = {'line1': 'street', 'city': 'Tullamore', 'state': 'offaly', } form = irish_form_class(data=test_data) self.assertEqual(form.fields['line1'].label.lower(), "street") self.assertEqual(form.fields['line2'].label.lower(), "area") self.assertEqual(form.fields['city'].label.lower(), "town/city") self.assertEqual(form.fields['state'].label.lower(), "county")
def test_get_co_address(self): """ Tests that we get the correct widget for Colombia """ co_form_class = form_factory("co") self.assertNotEqual(co_form_class, None) test_data = { 'line1': 'Diagonal 25 G', 'line2': '#95 a 55', 'state': 'Bogota D.C.', } form = co_form_class(data=test_data) self.assertEqual(form.fields['line1'].label.lower(), "street") self.assertEqual(form.fields['line2'].label.lower(), "number") self.assertEqual(form.fields['city'].label.lower(), "city") self.assertEqual(form.fields['state'].label.lower(), "department") self.assertIsInstance(form.fields['code'].widget, forms.HiddenInput)
def test_no_localisation(self): postal.settings.POSTAL_ADDRESS_L10N = False postal.settings.POSTAL_ADDRESS_LINE1 = ('a', False) postal.settings.POSTAL_ADDRESS_LINE2 = ('b', False) postal.settings.POSTAL_ADDRESS_CITY = ('c', False) postal.settings.POSTAL_ADDRESS_STATE = ('d', False) postal.settings.POSTAL_ADDRESS_CODE = ('e', False) reload(postal.forms) reload(postal.library) noloc_form_class = form_factory("nl") self.assertNotEqual(noloc_form_class, None) test_data = {'code': '1234AB'} form = noloc_form_class(data=test_data) self.assertEqual(form.fields['line1'].label, "a") self.assertEqual(form.fields['line2'].label, "b") self.assertEqual(form.fields['city'].label, "c") self.assertEqual(form.fields['state'].label, 'd') self.assertEqual(form.fields['code'].label, "e")
def test_get_it_address(self): """ Tests that we get the correct widget for Italy """ italian_form_class = form_factory("it") self.assertNotEqual(italian_form_class, None) # only use required fields test_data = { 'street': 'Piazza Duomo', 'code': '20100', 'city': 'Milano', 'state': 'MI' } form = italian_form_class(data=test_data) self.assertEqual(form.fields['line1'].label.lower(), "street") self.assertEqual(form.fields['line2'].label.lower(), "area") self.assertEqual(form.fields['state'].label.lower(), "province") self.assertEqual(form.fields['city'].label.lower(), "city") self.assertEqual(form.fields['code'].label.lower(), "zip code")
def test_get_ar_address(self): """ Tests that we get the correct widget for Argentina """ form_class = form_factory("ar") self.assertNotEqual(form_class, None) # only use required fields test_data = { 'line1': 'Maipu', 'line2': '270', 'city': 'Ciudad de Buenos Aires', 'state': 'B', 'code': 'C1006ACT', } form = form_class(data=test_data) self.assertEqual(form.fields['line1'].label.lower(), "street") self.assertEqual(form.fields['line2'].label.lower(), "number") self.assertEqual(form.fields['city'].label.lower(), "city") self.assertEqual(form.fields['code'].label.lower(), "zip code")
def save_address(request, customer, prefix): # get the shop shop = lfs.core.utils.get_default_shop(request) # get the country for the address country_iso = request.POST.get(prefix + "-country", shop.default_country.code) # check have we a valid address form_class = form_factory(country_iso) valid_address = False form_obj = form_class(request.POST, prefix=prefix) if form_obj.is_valid(): valid_address = True customer_selected_address = None address_attribute = 'selected_' + prefix + '_address' existing_address = False if hasattr(customer, address_attribute): customer_selected_address = getattr(customer, address_attribute) if customer_selected_address is not None: existing_address = True customer_selected_address.line1 = request.POST.get(prefix + "-line1", "") customer_selected_address.line2 = request.POST.get(prefix + "-line2", "") customer_selected_address.city = request.POST.get(prefix + "-city", "") customer_selected_address.state = request.POST.get(prefix + "-state", "") customer_selected_address.zip_code = request.POST.get(prefix + "-code", "") customer_selected_address.country = Country.objects.get(code=country_iso.lower()) customer_selected_address.save() if not existing_address: # no address exists for customer so create one customer_selected_address = Address.objects.create(customer=customer, line1=request.POST.get(prefix + "-line1", ""), line2=request.POST.get(prefix + "-line2", ""), city=request.POST.get(prefix + "-city", ""), state=request.POST.get(prefix + "-state", ""), zip_code=request.POST.get(prefix + "-code", ""), country=Country.objects.get(code=country_iso.lower())) setattr(customer, address_attribute, customer_selected_address) customer.save() return valid_address
def render(self, request, country_iso=None): """ Renders the postal and the additional address form. """ if country_iso is None: country_iso = self.address.country.code.upper() form_model = form_factory(country_iso) postal_form = form_model(initial=self.get_address_as_dict(), data=self.data, prefix=self.type) countries = self.get_countries(request) postal_form.fields["country"].choices = [(c.code.upper(), c.name) for c in countries] address_form_model = self.get_form_model() address_form = address_form_model(instance=self.address, data=self.data, prefix=self.type) templates = ["lfs/addresses/address_form.html"] templates.insert(0, "lfs/addresses/%s_address_form.html" % self.type) template = select_template(templates) return template.render(RequestContext(request, { "postal_form": postal_form, "address_form": address_form, }))
def test_ar_widgets(self): """ Tests that we get the correct widget for Argentina """ # enable L10N postal_settings.POSTAL_ADDRESS_L10N = True form_class = form_factory("ar") self.assertNotEqual(form_class, None) # only use required fields test_data = { 'line1': 'Maipu', 'line2': '270', 'city': 'Ciudad de Buenos Aires', 'state': 'B', 'code': 'C1006ACT', } form = form_class(data=test_data) from localflavor.ar.forms import ARProvinceSelect, ARPostalCodeField self.assertIsInstance(form.fields['state'].widget, ARProvinceSelect) self.assertIsInstance(form.fields['code'], ARPostalCodeField) self.assertEqual(form.fields['country'].initial, 'AR')
def addresses(request, template_name="muecke/customer/addresses.html"): """Provides a form to edit addresses and bank account. """ customer = muecke.customer.utils.get_customer(request) shop = muecke.core.utils.get_default_shop(request) if request.method == "POST": # Validate invoice address prefix = "invoice" country_iso = request.POST.get(prefix + "-country", shop.default_country.code) form_class = form_factory(country_iso) invoice_form = form_class(request.POST, prefix=prefix) # Validate shipping address prefix = "shipping" country_iso = request.POST.get(prefix + "-country", shop.default_country.code) form_class = form_factory(country_iso) shipping_form = form_class(request.POST, prefix=prefix) form = AddressForm(request.POST) if form.is_valid() and invoice_form.is_valid( ) and shipping_form.is_valid(): save_address(request, customer, INVOICE_PREFIX) save_address(request, customer, SHIPPING_PREFIX) customer.selected_invoice_address.customer = customer customer.selected_invoice_address.firstname = form.cleaned_data[ 'invoice_firstname'] customer.selected_invoice_address.company_name = form.cleaned_data[ 'invoice_company_name'] customer.selected_invoice_address.lastname = form.cleaned_data[ 'invoice_lastname'] customer.selected_invoice_address.phone = form.cleaned_data[ 'invoice_phone'] customer.selected_invoice_address.email = form.cleaned_data[ 'invoice_email'] customer.selected_invoice_address.save() customer.selected_shipping_address.customer = customer customer.selected_shipping_address.firstname = form.cleaned_data[ 'shipping_firstname'] customer.selected_shipping_address.lastname = form.cleaned_data[ 'shipping_lastname'] customer.selected_shipping_address.company_name = form.cleaned_data[ 'shipping_company_name'] customer.selected_shipping_address.phone = form.cleaned_data[ 'shipping_phone'] customer.selected_shipping_address.email = form.cleaned_data[ 'shipping_email'] customer.selected_shipping_address.save() return HttpResponseRedirect(reverse("muecke_my_addresses")) else: initial = {} if customer: if customer.selected_invoice_address is not None: initial.update({ "invoice_firstname": customer.selected_invoice_address.firstname, "invoice_lastname": customer.selected_invoice_address.lastname, "invoice_phone": customer.selected_invoice_address.phone, "invoice_email": customer.selected_invoice_address.email, "invoice_company_name": customer.selected_invoice_address.company_name, }) if customer.selected_shipping_address is not None: initial.update({ "shipping_firstname": customer.selected_shipping_address.firstname, "shipping_lastname": customer.selected_shipping_address.lastname, "shipping_phone": customer.selected_shipping_address.phone, "shipping_email": customer.selected_shipping_address.email, "shipping_company_name": customer.selected_shipping_address.company_name, }) form = AddressForm(initial=initial) return render_to_response( template_name, RequestContext( request, { "form": form, "shipping_address_inline": address_inline(request, "shipping", form), "invoice_address_inline": address_inline(request, "invoice", form), }))
def one_page_checkout(request, checkout_form=OnePageCheckoutForm, template_name="lfs/checkout/one_page_checkout.html"): """One page checkout form. """ # If the user is not authenticated and the if only authenticate checkout # allowed we rediret to authentication page. shop = lfs.core.utils.get_default_shop(request) if request.user.is_anonymous() and \ shop.checkout_type == CHECKOUT_TYPE_AUTH: return HttpResponseRedirect(reverse("lfs_checkout_login")) customer = customer_utils.get_or_create_customer(request) if request.method == "POST": form = checkout_form(request.POST) toc = True if shop.confirm_toc: if "confirm_toc" not in request.POST: toc = False if form.errors is None: form.errors = {} form.errors["confirm_toc"] = _(u"Please confirm our terms and conditions") # Validate invoice address prefix="invoice" country_iso = request.POST.get(prefix + "-country", shop.default_country.code) form_class = form_factory(country_iso) invoice_form = form_class(request.POST, prefix=prefix) # Validate shipping address valid_shipping_form = True if not request.POST.get("no_shipping"): prefix="shipping" country_iso = request.POST.get(prefix + "-country", shop.default_country.code) form_class = form_factory(country_iso) shipping_form = form_class(request.POST, prefix=prefix) valid_shipping_form = shipping_form.is_valid() if form.is_valid() and invoice_form.is_valid() and valid_shipping_form and toc: # save invoice details customer.selected_invoice_address.firstname = request.POST.get("invoice_firstname") customer.selected_invoice_address.lastname = request.POST.get("invoice_lastname") customer.selected_invoice_address.phone = request.POST.get("invoice_phone") customer.selected_invoice_address.email = request.POST.get("invoice_email") customer.selected_invoice_address.company_name = request.POST.get("invoice_company_name") # Create or update invoice address valid_invoice_address = save_address(request, customer, INVOICE_PREFIX) if valid_invoice_address == False: form._errors.setdefault(NON_FIELD_ERRORS, ErrorList([_(u"Invalid invoice address")])) else: # If the shipping address differs from invoice firstname we create # or update the shipping address. valid_shipping_address = True if not form.cleaned_data.get("no_shipping"): # save shipping details customer.selected_shipping_address.firstname = request.POST.get("shipping_firstname") customer.selected_shipping_address.lastname = request.POST.get("shipping_lastname") customer.selected_shipping_address.phone = request.POST.get("shipping_phone") customer.selected_shipping_address.email = request.POST.get("shipping_email") customer.selected_shipping_address.company_name = request.POST.get("shipping_company_name") valid_shipping_address = save_address(request, customer, SHIPPING_PREFIX) if valid_shipping_address == False: form._errors.setdefault(NON_FIELD_ERRORS, ErrorList([_(u"Invalid shipping address")])) else: # Payment method customer.selected_payment_method_id = request.POST.get("payment_method") if int(form.data.get("payment_method")) == DIRECT_DEBIT: bank_account = BankAccount.objects.create( account_number=form.cleaned_data.get("account_number"), bank_identification_code=form.cleaned_data.get("bank_identification_code"), bank_name=form.cleaned_data.get("bank_name"), depositor=form.cleaned_data.get("depositor"), ) customer.selected_bank_account = bank_account # Save the selected information to the customer customer.save() # process the payment method ... result = lfs.payment.utils.process_payment(request) if result["accepted"]: return HttpResponseRedirect(result.get("next_url", reverse("lfs_thank_you"))) else: if "message" in result: form._errors[result.get("message_location")] = result.get("message") else: # form is not valid # save invoice details customer.selected_invoice_address.firstname = request.POST.get("invoice_firstname") customer.selected_invoice_address.lastname = request.POST.get("invoice_lastname") customer.selected_invoice_address.phone = request.POST.get("invoice_phone") customer.selected_invoice_address.email = request.POST.get("invoice_email") customer.selected_invoice_address.company_name = request.POST.get("invoice_company_name") # If the shipping address differs from invoice firstname we create # or update the shipping address. if not form.data.get("no_shipping"): # save shipping details customer.selected_shipping_address.firstname = request.POST.get("shipping_firstname") customer.selected_shipping_address.lastname = request.POST.get("shipping_lastname") customer.selected_shipping_address.phone = request.POST.get("shipping_phone") customer.selected_shipping_address.email = request.POST.get("shipping_email") customer.selected_shipping_address.company_name = request.POST.get("shipping_company_name") customer.save() # Payment method customer.selected_payment_method_id = request.POST.get("payment_method") # 1 = Direct Debit if customer.selected_payment_method_id: if int(customer.selected_payment_method_id) == DIRECT_DEBIT: bank_account = BankAccount.objects.create( account_number=form.data.get("account_number"), bank_identification_code=form.data.get("bank_identification_code"), bank_name=form.data.get("bank_name"), depositor=form.data.get("depositor"), ) customer.selected_bank_account = bank_account # Save the selected information to the customer customer.save() else: # If there are addresses intialize the form. initial = {} invoice_address = customer.selected_invoice_address initial.update({ "invoice_firstname": invoice_address.firstname, "invoice_lastname": invoice_address.lastname, "invoice_phone": invoice_address.phone, "invoice_email": invoice_address.email, "invoice_country": invoice_address.country, "invoice_company_name": invoice_address.company_name, }) shipping_address = customer.selected_shipping_address initial.update({ "shipping_firstname": shipping_address.firstname, "shipping_lastname": shipping_address.lastname, "shipping_phone": shipping_address.phone, "shipping_email": shipping_address.email, "shipping_company_name": shipping_address.company_name, "no_shipping": True, }) form = checkout_form(initial=initial) cart = cart_utils.get_cart(request) if cart is None: return HttpResponseRedirect(reverse('lfs_cart')) # Payment try: selected_payment_method_id = request.POST.get("payment_method") selected_payment_method = PaymentMethod.objects.get(pk=selected_payment_method_id) except PaymentMethod.DoesNotExist: selected_payment_method = lfs.payment.utils.get_selected_payment_method(request) valid_payment_methods = lfs.payment.utils.get_valid_payment_methods(request) valid_payment_method_ids = [m.id for m in valid_payment_methods] display_bank_account = any([pm.type == lfs.payment.settings.PM_BANK for pm in valid_payment_methods]) display_credit_card = any([pm.type == lfs.payment.settings.PM_CREDIT_CARD for pm in valid_payment_methods]) response = render_to_response(template_name, RequestContext(request, { "form": form, "cart_inline": cart_inline(request), "shipping_inline": shipping_inline(request), "invoice_address_inline": address_inline(request, INVOICE_PREFIX, form), "shipping_address_inline": address_inline(request, SHIPPING_PREFIX, form), "payment_inline": payment_inline(request, form), "selected_payment_method": selected_payment_method, "display_bank_account": display_bank_account, "display_credit_card": display_credit_card, "voucher_number": lfs.voucher.utils.get_current_voucher_number(request), "settings": settings, })) return response
def test_checkout_for_ireland(self): """ Ireland doesn't have zip_code """ ie = Country.objects.get(code="ie") shop = get_default_shop() shop.shipping_countries.add(ie) shop.save() # login as our customer logged_in = self.client.login(username=self.username, password=self.password) self.assertEqual(logged_in, True) # test that our Netherlands form has only 4 address line fields ie_form_class = form_factory("IE") ie_form = ie_form_class() self.assertEqual('zip_code' in ie_form.fields, False) self.assertEqual('city' in ie_form.fields, True) # check initial database quantities self.assertEquals(Address.objects.count(), 2) self.assertEquals(Customer.objects.count(), 1) self.assertEquals(Order.objects.count(), 0) # check we have no invoice or shipping phone or email prior to checkout our_customer = Customer.objects.all()[0] self.assertEqual(our_customer.selected_invoice_address.phone, None) self.assertEqual(our_customer.selected_invoice_address.email, None) self.assertEqual(our_customer.selected_shipping_address.phone, None) self.assertEqual(our_customer.selected_shipping_address.email, None) checkout_data = { 'invoice-firstname': 'bob', 'invoice-lastname': 'builder', 'invoice-line1': 'ie street', 'invoice-line2': 'ie area', 'invoice-city': 'ie city', 'invoice-state': 'carlow', 'invoice-country': "IE", 'invoice-email': '*****@*****.**', 'invoice-phone': '1234567', 'shipping-firstname': 'hans', 'shipping-lastname': 'schmidt', 'shipping-line1': 'orianenberger strasse', 'shipping-line2': 'ie area', 'shipping-city': 'ie city', 'shipping-state': 'carlow', 'shipping-country': "IE", 'shipping-email': '*****@*****.**', 'shipping-phone': '7654321', 'payment_method': self.by_invoice.id, } checkout_post_response = self.client.post(reverse('lfs_checkout'), checkout_data) self.assertRedirects( checkout_post_response, reverse('lfs_thank_you'), status_code=302, target_status_code=200, ) # check database quantities post-checkout self.assertEquals(Address.objects.count(), 4) self.assertEquals(Customer.objects.count(), 1) self.assertEquals(Order.objects.count(), 1) # check our customer details post checkout our_customer = Customer.objects.all()[0] self.assertEqual(our_customer.selected_invoice_address.phone, "1234567") self.assertEqual(our_customer.selected_invoice_address.email, "*****@*****.**") self.assertEqual(our_customer.selected_shipping_address.phone, '7654321') self.assertEqual(our_customer.selected_shipping_address.email, "*****@*****.**")
def addresses(request, template_name="lfs/customer/addresses.html"): """Provides a form to edit addresses and bank account. """ customer = lfs.customer.utils.get_customer(request) shop = lfs.core.utils.get_default_shop(request) if request.method == "POST": # Validate invoice address prefix="invoice" country_iso = request.POST.get(prefix + "-country", shop.default_country.code) form_class = form_factory(country_iso) invoice_form = form_class(request.POST, prefix=prefix) # Validate shipping address prefix="shipping" country_iso = request.POST.get(prefix + "-country", shop.default_country.code) form_class = form_factory(country_iso) shipping_form = form_class(request.POST, prefix=prefix) form = AddressForm(request.POST) if form.is_valid() and invoice_form.is_valid() and shipping_form.is_valid(): save_address(request, customer, INVOICE_PREFIX) save_address(request, customer, SHIPPING_PREFIX) customer.selected_invoice_address.customer = customer customer.selected_invoice_address.firstname = form.cleaned_data['invoice_firstname'] customer.selected_invoice_address.company_name = form.cleaned_data['invoice_company_name'] customer.selected_invoice_address.lastname = form.cleaned_data['invoice_lastname'] customer.selected_invoice_address.phone = form.cleaned_data['invoice_phone'] customer.selected_invoice_address.email = form.cleaned_data['invoice_email'] customer.selected_invoice_address.save() customer.selected_shipping_address.customer = customer customer.selected_shipping_address.firstname = form.cleaned_data['shipping_firstname'] customer.selected_shipping_address.lastname = form.cleaned_data['shipping_lastname'] customer.selected_shipping_address.company_name = form.cleaned_data['shipping_company_name'] customer.selected_shipping_address.phone = form.cleaned_data['shipping_phone'] customer.selected_shipping_address.email = form.cleaned_data['shipping_email'] customer.selected_shipping_address.save() return HttpResponseRedirect(reverse("lfs_my_addresses")) else: initial = {} if customer: if customer.selected_invoice_address is not None: initial.update({"invoice_firstname": customer.selected_invoice_address.firstname, "invoice_lastname": customer.selected_invoice_address.lastname, "invoice_phone": customer.selected_invoice_address.phone, "invoice_email": customer.selected_invoice_address.email, "invoice_company_name": customer.selected_invoice_address.company_name, }) if customer.selected_shipping_address is not None: initial.update({"shipping_firstname": customer.selected_shipping_address.firstname, "shipping_lastname": customer.selected_shipping_address.lastname, "shipping_phone": customer.selected_shipping_address.phone, "shipping_email": customer.selected_shipping_address.email, "shipping_company_name": customer.selected_shipping_address.company_name, }) form = AddressForm(initial=initial) return render_to_response(template_name, RequestContext(request, { "form": form, "shipping_address_inline": address_inline(request, "shipping", form), "invoice_address_inline": address_inline(request, "invoice", form), }))
def one_page_checkout(request, checkout_form=OnePageCheckoutForm, template_name="muecke/checkout/one_page_checkout.html"): """One page checkout form. """ # If the user is not authenticated and the if only authenticate checkout # allowed we rediret to authentication page. shop = muecke.core.utils.get_default_shop(request) if request.user.is_anonymous() and \ shop.checkout_type == CHECKOUT_TYPE_AUTH: return HttpResponseRedirect(reverse("muecke_checkout_login")) customer = customer_utils.get_or_create_customer(request) if request.method == "POST": form = checkout_form(request.POST) toc = True if shop.confirm_toc: if "confirm_toc" not in request.POST: toc = False if form.errors is None: form.errors = {} form.errors["confirm_toc"] = _( u"Please confirm our terms and conditions") # Validate invoice address prefix = "invoice" country_iso = request.POST.get(prefix + "-country", shop.default_country.code) form_class = form_factory(country_iso) invoice_form = form_class(request.POST, prefix=prefix) # Validate shipping address valid_shipping_form = True if not request.POST.get("no_shipping"): prefix = "shipping" country_iso = request.POST.get(prefix + "-country", shop.default_country.code) form_class = form_factory(country_iso) shipping_form = form_class(request.POST, prefix=prefix) valid_shipping_form = shipping_form.is_valid() if form.is_valid() and invoice_form.is_valid( ) and valid_shipping_form and toc: # save invoice details customer.selected_invoice_address.firstname = request.POST.get( "invoice_firstname") customer.selected_invoice_address.lastname = request.POST.get( "invoice_lastname") customer.selected_invoice_address.phone = request.POST.get( "invoice_phone") customer.selected_invoice_address.email = request.POST.get( "invoice_email") customer.selected_invoice_address.company_name = request.POST.get( "invoice_company_name") # Create or update invoice address valid_invoice_address = save_address(request, customer, INVOICE_PREFIX) if valid_invoice_address == False: form._errors.setdefault( NON_FIELD_ERRORS, ErrorList([_(u"Invalid invoice address")])) else: # If the shipping address differs from invoice firstname we create # or update the shipping address. valid_shipping_address = True if not form.cleaned_data.get("no_shipping"): # save shipping details customer.selected_shipping_address.firstname = request.POST.get( "shipping_firstname") customer.selected_shipping_address.lastname = request.POST.get( "shipping_lastname") customer.selected_shipping_address.phone = request.POST.get( "shipping_phone") customer.selected_shipping_address.email = request.POST.get( "shipping_email") customer.selected_shipping_address.company_name = request.POST.get( "shipping_company_name") valid_shipping_address = save_address( request, customer, SHIPPING_PREFIX) if valid_shipping_address == False: form._errors.setdefault( NON_FIELD_ERRORS, ErrorList([_(u"Invalid shipping address")])) else: # Payment method customer.selected_payment_method_id = request.POST.get( "payment_method") if int(form.data.get("payment_method")) == DIRECT_DEBIT: bank_account = BankAccount.objects.create( account_number=form.cleaned_data.get( "account_number"), bank_identification_code=form.cleaned_data.get( "bank_identification_code"), bank_name=form.cleaned_data.get("bank_name"), depositor=form.cleaned_data.get("depositor"), ) customer.selected_bank_account = bank_account # Save the selected information to the customer customer.save() # process the payment method ... result = muecke.payment.utils.process_payment(request) if result["accepted"]: return HttpResponseRedirect( result.get("next_url", reverse("muecke_thank_you"))) else: if "message" in result: form._errors[result.get( "message_location")] = result.get("message") else: # form is not valid # save invoice details customer.selected_invoice_address.firstname = request.POST.get( "invoice_firstname") customer.selected_invoice_address.lastname = request.POST.get( "invoice_lastname") customer.selected_invoice_address.phone = request.POST.get( "invoice_phone") customer.selected_invoice_address.email = request.POST.get( "invoice_email") customer.selected_invoice_address.company_name = request.POST.get( "invoice_company_name") # If the shipping address differs from invoice firstname we create # or update the shipping address. if not form.data.get("no_shipping"): # save shipping details customer.selected_shipping_address.firstname = request.POST.get( "shipping_firstname") customer.selected_shipping_address.lastname = request.POST.get( "shipping_lastname") customer.selected_shipping_address.phone = request.POST.get( "shipping_phone") customer.selected_shipping_address.email = request.POST.get( "shipping_email") customer.selected_shipping_address.company_name = request.POST.get( "shipping_company_name") customer.save() # Payment method customer.selected_payment_method_id = request.POST.get( "payment_method") # 1 = Direct Debit if customer.selected_payment_method_id: if int(customer.selected_payment_method_id) == DIRECT_DEBIT: bank_account = BankAccount.objects.create( account_number=form.data.get("account_number"), bank_identification_code=form.data.get( "bank_identification_code"), bank_name=form.data.get("bank_name"), depositor=form.data.get("depositor"), ) customer.selected_bank_account = bank_account # Save the selected information to the customer customer.save() else: # If there are addresses intialize the form. initial = {} invoice_address = customer.selected_invoice_address initial.update({ "invoice_firstname": invoice_address.firstname, "invoice_lastname": invoice_address.lastname, "invoice_phone": invoice_address.phone, "invoice_email": invoice_address.email, "invoice_country": invoice_address.country, "invoice_company_name": invoice_address.company_name, }) shipping_address = customer.selected_shipping_address initial.update({ "shipping_firstname": shipping_address.firstname, "shipping_lastname": shipping_address.lastname, "shipping_phone": shipping_address.phone, "shipping_email": shipping_address.email, "shipping_company_name": shipping_address.company_name, "no_shipping": True, }) form = checkout_form(initial=initial) cart = cart_utils.get_cart(request) if cart is None: return HttpResponseRedirect(reverse('muecke_cart')) # Payment try: selected_payment_method_id = request.POST.get("payment_method") selected_payment_method = PaymentMethod.objects.get( pk=selected_payment_method_id) except PaymentMethod.DoesNotExist: selected_payment_method = muecke.payment.utils.get_selected_payment_method( request) valid_payment_methods = muecke.payment.utils.get_valid_payment_methods( request) valid_payment_method_ids = [m.id for m in valid_payment_methods] display_bank_account = any([ pm.type == muecke.payment.settings.PM_BANK for pm in valid_payment_methods ]) display_credit_card = any([ pm.type == muecke.payment.settings.PM_CREDIT_CARD for pm in valid_payment_methods ]) response = render_to_response( template_name, RequestContext( request, { "form": form, "cart_inline": cart_inline(request), "shipping_inline": shipping_inline(request), "invoice_address_inline": address_inline(request, INVOICE_PREFIX, form), "shipping_address_inline": address_inline(request, SHIPPING_PREFIX, form), "payment_inline": payment_inline(request, form), "selected_payment_method": selected_payment_method, "display_bank_account": display_bank_account, "display_credit_card": display_credit_card, "voucher_number": muecke.voucher.utils.get_current_voucher_number(request), "settings": settings, })) return response