Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Use new i18n API in shipping and summary steps
Artur Smęt1494db25fd2a
diff --git a/saleor/checkout/views/shipping.py b/saleor/checkout/views/shipping.pyindex 7b8198f8d5..f521803f8d 100644--- a/saleor/checkout/views/shipping.py+++ b/saleor/checkout/views/shipping.py@@ -2,30 +2,22 @@ from django.shortcuts import redirectfrom django.template.response import TemplateResponsefrom ..forms import AnonymousUserShippingForm, ShippingAddressesForm-from ...userprofile.forms import AddressForm+from ...userprofile.forms import AddressForm, get_address_formfrom ...userprofile.models import Address-from ...i18n_address_forms.forms import COUNTRY_FORMSdef anonymous_user_shipping_address_view(request, checkout):- data = request.POST or None- shipping_address = checkout.shipping_address- country_code = request.country.code if request.country else 'US'- address_form_class = COUNTRY_FORMS[country_code]+ address_form, preview = get_address_form(+ request.POST or None, country_code=request.country.code,+ autocomplete_type='shipping',+ initial={'country': request.country.code},+ instance=checkout.shipping_address)- if shipping_address is not None:- address_form_class = COUNTRY_FORMS[shipping_address.country.code]- address_form = address_form_class(- data, instance=checkout.shipping_address,- autocomplete_type='shipping')- else:- address_form = address_form_class(- data, autocomplete_type='shipping',- initial={'country': country_code})user_form = AnonymousUserShippingForm(- data, initial={'email': checkout.email})- if user_form.is_valid() and address_form.is_valid():+ not preview and request.POST or None, initial={'email': checkout.email}+ if not preview else request.POST.dict())+ if all([user_form.is_valid(), address_form.is_valid()]):checkout.shipping_address = address_form.instancecheckout.email = user_form.cleaned_data['email']return redirect('checkout:shipping-method')diff --git a/saleor/checkout/views/summary.py b/saleor/checkout/views/summary.pyindex ef0ae401dd..84aad28a7e 100644--- a/saleor/checkout/views/summary.py+++ b/saleor/checkout/views/summary.py@@ -3,7 +3,7 @@ from django.template.response import TemplateResponsefrom ..forms import (AnonymousUserBillingForm, BillingAddressesForm, BillingWithoutShippingAddressForm)-from ...userprofile.forms import AddressForm+from ...userprofile.forms import AddressForm, get_address_formfrom ...userprofile.models import Address@@ -18,13 +18,20 @@ def create_order(checkout):def get_billing_forms_with_shipping(data, addresses, billing_address, shipping_address):if Address.objects.are_identical(billing_address, shipping_address):- address_form = AddressForm(data, autocomplete_type='billing')+ address_form = get_address_form(+ data, country_code=shipping_address.country.code,+ autocomplete_type='billing',+ initial={'country': shipping_address.country.code},+ instance=None)[0]addresses_form = BillingAddressesForm(data, additional_addresses=addresses, initial={'address': BillingAddressesForm.SHIPPING_ADDRESS})elif billing_address.id is None:- address_form = AddressForm(- data, instance=billing_address, autocomplete_type='billing')+ address_form = get_address_form(+ data, country_code=billing_address.country.code,+ autocomplete_type='billing',+ initial={'country': billing_address.country.code},+ instance=billing_address)[0]addresses_form = BillingAddressesForm(data, additional_addresses=addresses, initial={'address': BillingAddressesForm.NEW_ADDRESS})@@ -96,7 +103,8 @@ def summary_without_shipping(request, checkout):initial={'address': billing_address.id})elif billing_address:address_form = AddressForm(- request.POST or None, autocomplete_type='billing', instance=billing_address)+ request.POST or None, autocomplete_type='billing',+ instance=billing_address)addresses_form = BillingWithoutShippingAddressForm(request.POST or None, additional_addresses=user_addresses)else:diff --git a/saleor/userprofile/i18n.py b/saleor/userprofile/i18n.pyindex a5ab2903d0..50b7897e09 100644--- a/saleor/userprofile/i18n.py+++ b/saleor/userprofile/i18n.py@@ -115,11 +115,12 @@ class CountryAwareAddressForm(AddressForm):def validate_address(self, data):try:data['country_code'] = data['country']+ data['street_address'] = '%s\n%s' % (+ data['street_address_1'], data['street_address_2'])data = i18naddress.normalize_address(data)del data['sorting_code']except i18naddress.InvalidAddress as exc:self.add_field_errors(exc.errors)- import ipdb;ipdb.set_trace()return datadef clean(self):diff --git a/saleor/userprofile/templatetags/i18n_address_tags.py b/saleor/userprofile/templatetags/i18n_address_tags.pyindex 151ec5c592..a96641a3cc 100644--- a/saleor/userprofile/templatetags/i18n_address_tags.py+++ b/saleor/userprofile/templatetags/i18n_address_tags.py@@ -10,4 +10,6 @@ register = template.Library()def format_address(address, latin=False):address_data = Address.objects.as_data(address)address_data['country_code'] = address_data['country']+ address_data['street_address'] = '%s\n%s' % (+ address_data['street_address_1'], address_data['street_address_2'])return mark_safe(i18naddress.format_address(address_data, latin))diff --git a/templates/userprofile/snippets/address-form.html b/templates/userprofile/snippets/address-form.htmlindex 9ab006a852..64301841d8 100644--- a/templates/userprofile/snippets/address-form.html+++ b/templates/userprofile/snippets/address-form.html@@ -1,15 +1,17 @@{% load bootstrap_field from bootstrap3 %}-+{% bootstrap_field address_form.phone %}{% with address_form_lines=address_form.i18n_fields_order %}- {% for line in address_form_lines %}- <div class="row">- {% for field in line %}- <div class="col-sm-4">- {% bootstrap_field field %}- </div>- {% endfor %}- </div>- {% endfor %}+ {% if address_form_lines %}+ {% for line in address_form_lines %}+ <div class="row">+ {% for field in line %}+ <div class="col-sm-4">+ {% bootstrap_field field %}+ </div>+ {% endfor %}+ </div>+ {% endfor %}+ {% endif %}{% endwith %}-{% bootstrap_field address_form.phone %}{% bootstrap_field address_form.country %}+<input type="checkbox" name="preview">