diff options
author | Artur Smęt <artur.smet@mirumee.com> | 2016-10-14 19:24:21 +0200 |
---|---|---|
committer | Artur Smęt <artur.smet@mirumee.com> | 2016-10-25 17:04:30 +0200 |
commit | 4c7737fc448efabe4f2926c3133f13cab68c3b91 (patch) | |
tree | 6afd2bc6dc0d42426f239c1a087f54158fd63bbb /saleor/userprofile | |
parent | f0fc077ad88f9e01265f475b72205dde197a4b41 (diff) | |
download | saleor-frontend-4c7737fc448efabe4f2926c3133f13cab68c3b91.tar.gz saleor-frontend-4c7737fc448efabe4f2926c3133f13cab68c3b91.tar.bz2 saleor-frontend-4c7737fc448efabe4f2926c3133f13cab68c3b91.zip |
Move AddressForm to i18n module
Diffstat (limited to 'saleor/userprofile')
-rw-r--r-- | saleor/userprofile/forms.py | 57 |
1 files changed, 23 insertions, 34 deletions
diff --git a/saleor/userprofile/forms.py b/saleor/userprofile/forms.py index 3f861be9..2f4c5036 100644 --- a/saleor/userprofile/forms.py +++ b/saleor/userprofile/forms.py @@ -1,42 +1,31 @@ # encoding: utf-8 from __future__ import unicode_literals -from collections import defaultdict +from .i18n import AddressMetaForm, get_address_form_class, AddressForm -from django import forms -from .models import Address +def get_address_form(data, country_code, initial, instance, **kwargs): + country_form = AddressMetaForm(data, initial=initial) + preview = False + if country_form.is_valid(): + country_code = country_form.cleaned_data['country'] + preview = country_form.cleaned_data['preview'] -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') - ) + address_form_class = get_address_form_class(country_code) - class Meta: - model = Address - exclude = [] - - def __init__(self, *args, **kwargs): - autocomplete_type = kwargs.pop('autocomplete_type', None) - super(AddressForm, self).__init__(*args, **kwargs) - autocomplete_dict = defaultdict( - lambda: 'off', self.AUTOCOMPLETE_MAPPING) - for field_name, field in self.fields.items(): - if autocomplete_type: - autocomplete = '%s %s' % ( - autocomplete_type, autocomplete_dict[field_name]) - else: - autocomplete = autocomplete_dict[field_name] - field.widget.attrs['autocomplete'] = autocomplete + if not preview and instance is not None: + address_form_class = get_address_form_class( + instance.country.code) + address_form = address_form_class( + data, instance=instance, + **kwargs) + else: + initial_address = ( + initial if not preview + else data.dict() if data is not None else data) + address_form = address_form_class( + not preview and data or None, + initial=initial_address, + **kwargs) + return address_form, preview |