Skip to content

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

Improve address form

Michał Ociepka9735b7792022
diff --git a/saleor/userprofile/forms.py b/saleor/userprofile/forms.py
index 9ea1dcb8a7..d82dd8fe54 100644
--- a/saleor/userprofile/forms.py
+++ b/saleor/userprofile/forms.py
@@ -1,9 +1,34 @@
+from collections import defaultdict
+
from django import forms
from .models import Address
class AddressForm(forms.ModelForm):
+
+ AUTOCOMPLETE_MAPPING = (
+ ('first_name', 'given-name'),
+ ('last_name', 'family-name'),
+ ('company_name', 'organization'),
+ ('street_address_1', 'address-line1'),
+ ('street_address_2', 'address-line2'),
+ ('city', 'address-level2'),
+ ('postal_code', 'postal-code'),
+ ('country_area', 'address-level1'),
+ ('country', 'country'),
+ ('city_area', 'address-level3'),
+ ('phone', 'tel'),
+ ('email', 'email')
+ )
+
class Meta:
model = Address
exclude = []
+
+ def __init__(self, *args, **kwargs):
+ super(AddressForm, self).__init__(*args, **kwargs)
+ autocomplete_dict = defaultdict(
+ lambda: 'off', self.AUTOCOMPLETE_MAPPING)
+ for field_name, field in self.fields.items():
+ field.widget.attrs['autocomplete'] = autocomplete_dict[field_name]
diff --git a/saleor/userprofile/migrations/0003_auto_20151104_1102.py b/saleor/userprofile/migrations/0003_auto_20151104_1102.py
new file mode 100644
index 0000000000..334e4872ee
--- /dev/null
+++ b/saleor/userprofile/migrations/0003_auto_20151104_1102.py
@@ -0,0 +1,24 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('userprofile', '0002_auto_20150907_0602'),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name='address',
+ name='city_area',
+ field=models.CharField(max_length=128, verbose_name='city administrative area', blank=True),
+ ),
+ migrations.AlterField(
+ model_name='address',
+ name='postal_code',
+ field=models.CharField(max_length=20, verbose_name='postal code', blank=True),
+ ),
+ ]
diff --git a/saleor/userprofile/models.py b/saleor/userprofile/models.py
index b5ac45071c..88777a6662 100644
--- a/saleor/userprofile/models.py
+++ b/saleor/userprofile/models.py
@@ -48,9 +48,12 @@ class Address(models.Model):
city = models.CharField(
pgettext_lazy('Address field', 'city'),
max_length=256)
+ city_area = models.CharField(
+ pgettext_lazy('Address field', 'city administrative area'),
+ max_length=128, blank=True)
postal_code = models.CharField(
pgettext_lazy('Address field', 'postal code'),
- max_length=20)
+ max_length=20, blank=True)
country = models.CharField(
pgettext_lazy('Address field', 'country'),
choices=COUNTRY_CHOICES, max_length=2)