Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Refactor checkout address related methods

Artur Smęt9272b4dc678d
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index 800f5d4b92..04533ca2ea 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -192,29 +192,36 @@ class Checkout(object):
return Address.objects.are_identical(
self.shipping_address, self.billing_address)
- def _save_address(self, address, is_billing=False, is_shipping=False,
- create_copy=False):
- if self.user.is_authenticated() and address.id is None:
- address = User.objects.store_address(
- self.user, address, shipping=is_shipping, billing=is_billing)
- elif address.id is None:
- address.save()
- if create_copy:
- address.pk = None
- address.user = None
- address.save()
- return address
+ def _add_to_user_address_book(self, address, is_billing=False,
+ is_shipping=False):
+ if self.user.is_authenticated():
+ User.objects.store_address(
+ self.user, address, shipping=is_shipping,
+ billing=is_billing)
+
+ def _get_address_copy(self, address):
+ address.user = None
+ address.pk = None
+ address.save()
return address
+ def _save_order_billing_address(self):
+ return self._get_address_copy(self.billing_address)
+
+ def _save_order_shipping_address(self):
+ return self._get_address_copy(self.shipping_address)
+
@transaction.atomic
def create_order(self):
if self.is_shipping_required:
- shipping_address = self._save_address(
- self.shipping_address, is_shipping=True, create_copy=True)
+ shipping_address = self._save_order_shipping_address()
+ self._add_to_user_address_book(
+ self.shipping_address, is_shipping=True)
else:
shipping_address = None
- billing_address = self._save_address(
- self.billing_address, is_billing=True, create_copy=True)
+ billing_address = self._save_order_billing_address()
+ self._add_to_user_address_book(
+ self.shipping_address, is_billing=True)
order_data = {
'billing_address': billing_address,
diff --git a/tests/test_checkout_integration.py b/tests/test_checkout_integration.py
index 9e803a8fd9..e3231fcd75 100644
--- a/tests/test_checkout_integration.py
+++ b/tests/test_checkout_integration.py
@@ -114,6 +114,8 @@ def test_checkout_flow(product_in_stock, client):
payment = order.payments.latest('pk')
assert payment.status == 'preauth'
assert order.status == OrderStatus.NEW
+ latest_address = Address.objects.latest('pk')
+ assert Address.objects.are_identical(order.shipping_address, latest_address)
def test_checkout_flow_authenticated_user(product_in_stock, client,