Skip to content

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

Move copying address method to model

Anna Siwik957e5c581ec9
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index 653bf301cc..799a77f1e1 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -257,10 +257,10 @@ class Checkout:
billing=is_billing)
def _save_order_billing_address(self):
- return Address.objects.copy(self.billing_address)
+ return self.billing_address.get_copy()
def _save_order_shipping_address(self):
- return Address.objects.copy(self.shipping_address)
+ return self.shipping_address.get_copy()
@transaction.atomic
def create_order(self):
diff --git a/saleor/userprofile/models.py b/saleor/userprofile/models.py
index 18a2ee58b5..9d7760520c 100644
--- a/saleor/userprofile/models.py
+++ b/saleor/userprofile/models.py
@@ -78,6 +78,11 @@ class Address(models.Model):
"""Return `True` if `addr1` and `addr2` are the same address."""
return Address.objects.as_data(self) == Address.objects.as_data(other)
+ def get_copy(self):
+ """Return a new instance of the same address."""
+ data = Address.objects.as_data(self)
+ return Address.objects.create(**data)
+
class UserManager(BaseUserManager):
diff --git a/tests/test_userprofile.py b/tests/test_userprofile.py
index 9f765f10f5..a27e928d61 100644
--- a/tests/test_userprofile.py
+++ b/tests/test_userprofile.py
@@ -153,20 +153,26 @@ def test_format_address_all_options(billing_address):
assert str(billing_address.phone) not in rendered_html
+def test_copy_address(billing_address):
+ copied_address = billing_address.get_copy()
+ assert copied_address.pk != billing_address.pk
+ assert copied_address == billing_address
+
+
def test_compare_addresses(billing_address):
- copied_address = models.Address.objects.copy(billing_address)
+ copied_address = billing_address.get_copy()
assert billing_address == copied_address
def test_compare_addresses_with_country_object(billing_address):
- copied_address = models.Address.objects.copy(billing_address)
+ copied_address = billing_address.get_copy()
copied_address.country = Country('PL')
copied_address.save()
assert billing_address == copied_address
def test_compare_addresses_different_country(billing_address):
- copied_address = models.Address.objects.copy(billing_address)
+ copied_address = billing_address.get_copy()
copied_address.country = Country('FR')
copied_address.save()
assert billing_address != copied_address