Skip to content

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

* Handle phone serialization

akjanik629172441d39
diff --git a/saleor/checkout/core.py b/saleor/checkout/core.py
index b88240895a..cd857f88a2 100644
--- a/saleor/checkout/core.py
+++ b/saleor/checkout/core.py
@@ -21,6 +21,8 @@ from ..shipping.models import ANY_COUNTRY, ShippingMethodCountry
from ..userprofile.models import Address
from ..userprofile.utils import store_user_address
+from phonenumber_field.phonenumber import PhoneNumber
+
STORAGE_SESSION_KEY = 'checkout_storage'
@@ -134,6 +136,9 @@ class Checkout(object):
@shipping_address.setter
def shipping_address(self, address):
address_data = model_to_dict(address)
+ phone_number = address_data.get('phone')
+ if phone_number:
+ address_data['phone'] = str(address_data['phone'])
address_data['country'] = smart_text(address_data['country'])
self.storage['shipping_address'] = address_data
self.modified = True
@@ -405,7 +410,6 @@ def load_checkout(view):
except KeyError:
session_data = ''
tracking_code = analytics.get_client_id(request)
-
checkout = Checkout.from_storage(
session_data, cart, request.user, tracking_code)
response = view(request, checkout, cart)
diff --git a/saleor/settings.py b/saleor/settings.py
index 76e63011ba..a6f769e7d8 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -186,7 +186,7 @@ INSTALLED_APPS = [
'django_countries',
'django_filters',
'django_celery_results',
- 'impersonate'
+ 'impersonate',
'phonenumber_field',
]
diff --git a/saleor/userprofile/migrations/0013_auto_20171120_0521.py b/saleor/userprofile/migrations/0013_auto_20171120_0521.py
new file mode 100644
index 0000000000..63e7a6fc09
--- /dev/null
+++ b/saleor/userprofile/migrations/0013_auto_20171120_0521.py
@@ -0,0 +1,21 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.5 on 2017-11-20 11:21
+from __future__ import unicode_literals
+
+from django.db import migrations
+import phonenumber_field.modelfields
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('userprofile', '0012_auto_20171117_0846'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='address',
+ name='phone',
+ field=phonenumber_field.modelfields.PhoneNumberField(blank=True, default='', max_length=128, verbose_name='phone number'),
+ ),
+ ]
diff --git a/saleor/userprofile/migrations/0014_auto_20171121_0430.py b/saleor/userprofile/migrations/0014_auto_20171121_0430.py
new file mode 100644
index 0000000000..d31780c873
--- /dev/null
+++ b/saleor/userprofile/migrations/0014_auto_20171121_0430.py
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.5 on 2017-11-21 10:30
+from __future__ import unicode_literals
+import random
+
+from django.db import migrations
+from phonenumbers import COUNTRY_CODE_TO_REGION_CODE
+
+
+REVERSED_COUNTRY_CODE = dict(
+ zip(COUNTRY_CODE_TO_REGION_CODE.values(),
+ COUNTRY_CODE_TO_REGION_CODE.keys()))
+
+
+def add_phone_number(apps, schema_editor):
+ # We get the model from the versioned app registry;
+ # if we directly import it, it'll be the wrong version
+ Address = apps.get_model("userprofile", "Address")
+ db_alias = schema_editor.connection.alias
+ for address in Address.objects.all():
+ if (address.country,) in REVERSED_COUNTRY_CODE:
+ phone_number = '+{}{}'.format(
+ REVERSED_COUNTRY_CODE[(address.country,)],
+ str(random.randint(10**9, 10**10 - 1)))
+ address.phone = phone_number
+ address.save()
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('userprofile', '0013_auto_20171120_0521'),
+ ]
+
+ operations = [
+ migrations.RunPython(add_phone_number),
+ ]
diff --git a/saleor/userprofile/models.py b/saleor/userprofile/models.py
index 04cb0691d8..d9f938af0e 100644
--- a/saleor/userprofile/models.py
+++ b/saleor/userprofile/models.py
@@ -75,7 +75,7 @@ class Address(models.Model):
pgettext_lazy('Address field', 'state or province'),
max_length=128, blank=True)
phone = PhoneNumberField(
- pgettext_lazy('Address field', 'phone number'),
+ verbose_name=pgettext_lazy('Address field', 'phone number'),
blank=True, default='')
objects = AddressManager()
@@ -100,7 +100,7 @@ class Address(models.Model):
self.first_name, self.last_name, self.company_name,
self.street_address_1, self.street_address_2, self.city,
self.postal_code, self.country, self.country_area,
- self.phone))
+ str(self.phone)))
class UserManager(BaseUserManager):