Skip to content

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

Move as_data address method to model

Anna Siwika323261c1c3e
diff --git a/saleor/userprofile/models.py b/saleor/userprofile/models.py
index e2e4f7726b..cf537bb8da 100644
--- a/saleor/userprofile/models.py
+++ b/saleor/userprofile/models.py
@@ -17,19 +17,6 @@ class PossiblePhoneNumberField(PhoneNumberField):
default_validators = [validate_possible_number]
-class AddressManager(models.Manager):
-
- def as_data(self, address):
- """Return the address as a dict suitable for passing as kwargs.
-
- Result does not contain the primary key or an associated user.
- """
- data = model_to_dict(address, exclude=['id', 'user'])
- if isinstance(data['country'], Country):
- data['country'] = data['country'].code
- return data
-
-
class Address(models.Model):
first_name = models.CharField(max_length=256, blank=True)
last_name = models.CharField(max_length=256, blank=True)
@@ -42,7 +29,6 @@ class Address(models.Model):
country = CountryField()
country_area = models.CharField(max_length=128, blank=True)
phone = PossiblePhoneNumberField(blank=True, default='')
- objects = AddressManager()
@property
def full_name(self):
@@ -64,13 +50,21 @@ class Address(models.Model):
self.phone))
def __eq__(self, other):
- """Return `True` if `addr1` and `addr2` are the same address."""
- return Address.objects.as_data(self) == Address.objects.as_data(other)
+ return self.as_data() == other.as_data()
+
+ def as_data(self):
+ """Return the address as a dict suitable for passing as kwargs.
+
+ Result does not contain the primary key or an associated user.
+ """
+ data = model_to_dict(self, exclude=['id', 'user'])
+ if isinstance(data['country'], Country):
+ data['country'] = data['country'].code
+ return data
def get_copy(self):
"""Return a new instance of the same address."""
- data = Address.objects.as_data(self)
- return Address.objects.create(**data)
+ return Address.objects.create(**self.as_data())
class UserManager(BaseUserManager):
diff --git a/saleor/userprofile/templatetags/i18n_address_tags.py b/saleor/userprofile/templatetags/i18n_address_tags.py
index 46bc9c1c6d..30aa03d7b7 100644
--- a/saleor/userprofile/templatetags/i18n_address_tags.py
+++ b/saleor/userprofile/templatetags/i18n_address_tags.py
@@ -2,14 +2,12 @@ from django import template
from django.utils.translation import pgettext
import i18naddress
-from ...userprofile.models import Address
-
register = template.Library()
@register.inclusion_tag('formatted_address.html')
def format_address(address, include_phone=True, inline=False, latin=False):
- address_data = Address.objects.as_data(address)
+ address_data = address.as_data()
address_data['name'] = pgettext(
'Address data', '%(first_name)s %(last_name)s') % address_data
address_data['country_code'] = address_data['country']
diff --git a/saleor/userprofile/utils.py b/saleor/userprofile/utils.py
index 1048fa42cc..f363062a79 100644
--- a/saleor/userprofile/utils.py
+++ b/saleor/userprofile/utils.py
@@ -1,8 +1,5 @@
-from .models import Address
-
-
def store_user_address(user, address, billing=False, shipping=False):
- data = Address.objects.as_data(address)
+ data = address.as_data()
entry = user.addresses.get_or_create(**data)[0]
changed = False
if billing and not user.default_billing_address_id:
diff --git a/tests/test_userprofile.py b/tests/test_userprofile.py
index a27e928d61..1f137ea8f7 100644
--- a/tests/test_userprofile.py
+++ b/tests/test_userprofile.py
@@ -153,6 +153,23 @@ def test_format_address_all_options(billing_address):
assert str(billing_address.phone) not in rendered_html
+def test_address_as_data(billing_address):
+ data = billing_address.as_data()
+ assert data == {
+ 'first_name': 'John',
+ 'last_name': 'Doe',
+ 'company_name': 'Mirumee Software',
+ 'street_address_1': 'Tęczowa 7',
+ 'street_address_2': '',
+ 'city': 'Wrocław',
+ 'city_area': '',
+ 'postal_code': '53-601',
+ 'country': 'PL',
+ 'country_area': '',
+ 'phone': '+48713988102'
+ }
+
+
def test_copy_address(billing_address):
copied_address = billing_address.get_copy()
assert copied_address.pk != billing_address.pk