Ejemplo n.º 1
0
def test_address_form_for_country(country):
    data = {
        'first_name': 'John',
        'last_name': 'Doe',
        'country': country,
        'phone': '123456789'}

    form = forms.get_address_form(data, country_code=country)[0]
    errors = form.errors
    rules = i18naddress.get_validation_rules({'country_code': country})
    required = rules.required_fields
    if 'street_address' in required:
        assert 'street_address_1' in errors
    else:
        assert 'street_address_1' not in errors
    if 'city' in required:
        assert 'city' in errors
    else:
        assert 'city' not in errors
    if 'city_area' in required:
        assert 'city_area' in errors
    else:
        assert 'city_area' not in errors
    if 'country_area' in required:
        assert 'country_area' in errors
    else:
        assert 'country_area' not in errors
    if 'postal_code' in required:
        assert 'postal_code' in errors
    else:
        assert 'postal_code' not in errors
Ejemplo n.º 2
0
def test_address_form_for_country(country):
    data = {
        "first_name": "John",
        "last_name": "Doe",
        "country": country,
        "phone": "123456789",
    }

    form = forms.get_address_form(data, country_code=country)[0]
    errors = form.errors
    rules = i18naddress.get_validation_rules({"country_code": country})
    required = rules.required_fields
    if "street_address" in required:
        assert "street_address_1" in errors
    else:
        assert "street_address_1" not in errors
    if "city" in required:
        assert "city" in errors
    else:
        assert "city" not in errors
    if "city_area" in required:
        assert "city_area" in errors
    else:
        assert "city_area" not in errors
    if "country_area" in required:
        assert "country_area" in errors
    else:
        assert "country_area" not in errors
    if "postal_code" in required:
        assert "postal_code" in errors
    else:
        assert "postal_code" not in errors
Ejemplo n.º 3
0
def resolve_address_validator(info, country_code, country_area, city_area):
    if not country_code:
        client_ip = get_client_ip(info.context)
        country = get_country_by_ip(client_ip)
        if country:
            country_code = country.code
        else:
            return None
    params = {
        "country_code": country_code,
        "country_area": country_area,
        "city_area": city_area,
    }
    rules = get_validation_rules(params)
    return AddressValidationData(
        country_code=rules.country_code,
        country_name=rules.country_name,
        address_format=rules.address_format,
        address_latin_format=rules.address_latin_format,
        allowed_fields=rules.allowed_fields,
        required_fields=rules.required_fields,
        upper_fields=rules.upper_fields,
        country_area_type=rules.country_area_type,
        country_area_choices=[
            ChoiceValue(area[0], area[1]) for area in rules.country_area_choices
        ],
        city_type=rules.city_type,
        city_choices=[ChoiceValue(area[0], area[1]) for area in rules.city_choices],
        city_area_type=rules.city_type,
        city_area_choices=[
            ChoiceValue(area[0], area[1]) for area in rules.city_area_choices
        ],
        postal_code_type=rules.postal_code_type,
        postal_code_matchers=[
            compiled.pattern for compiled in rules.postal_code_matchers
        ],
        postal_code_examples=rules.postal_code_examples,
        postal_code_prefix=rules.postal_code_prefix,
    )
Ejemplo n.º 4
0
def construct_address_form(country_code, i18n_rules):
    class_name = 'AddressForm%s' % country_code
    base_class = CountryAwareAddressForm
    form_kwargs = {
        'Meta': type(str('Meta'), (base_class.Meta, object), {}),
        'formfield_callback': None}
    class_ = type(base_class)(str(class_name), (base_class, ), form_kwargs)
    update_base_fields(class_, i18n_rules)
    class_.i18n_country_code = country_code
    class_.i18n_fields_order = property(get_form_i18n_lines)
    return class_


for country in COUNTRIES.keys():
    try:
        country_rules = i18naddress.get_validation_rules(
            {'country_code': country})
    except ValueError:
        country_rules = i18naddress.get_validation_rules({})
        UNKNOWN_COUNTRIES.add(country)

COUNTRY_CHOICES = [(code, label) for code, label in COUNTRIES.items()
                   if code not in UNKNOWN_COUNTRIES]
# Sort choices list by country name
COUNTRY_CHOICES = sorted(COUNTRY_CHOICES, key=lambda choice: choice[1])

for country, label in COUNTRY_CHOICES:
    country_rules = i18naddress.get_validation_rules({'country_code': country})
    COUNTRY_FORMS[country] = construct_address_form(country, country_rules)
Ejemplo n.º 5
0
    class_name = 'AddressForm%s' % country_code
    base_class = CountryAwareAddressForm
    form_kwargs = {
        'Meta': type(str('Meta'), (base_class.Meta, object), {}),
        'formfield_callback': None
    }
    class_ = type(base_class)(str(class_name), (base_class, ), form_kwargs)
    update_base_fields(class_, i18n_rules)
    class_.i18n_country_code = country_code
    class_.i18n_fields_order = property(get_form_i18n_lines)
    return class_


for country in countries.countries.keys():
    try:
        country_rules = i18naddress.get_validation_rules(
            {'country_code': country})
    except ValueError:
        country_rules = i18naddress.get_validation_rules({})
        UNKNOWN_COUNTRIES.add(country)

COUNTRY_CHOICES = [(code, label)
                   for code, label in countries.countries.items()
                   if code not in UNKNOWN_COUNTRIES]
# Sort choices list by country name
COUNTRY_CHOICES = sorted(COUNTRY_CHOICES, key=lambda choice: choice[1])

for country, label in COUNTRY_CHOICES:
    country_rules = i18naddress.get_validation_rules({'country_code': country})
    COUNTRY_FORMS[country] = construct_address_form(country, country_rules)