Generic helpers

Forms

class localflavor.generic.forms.BICFormField(*args, **kwargs)[source]

A BIC consists of 8 (BIC8) or 11 (BIC11) alphanumeric characters.

BICs are also known as SWIFT-BIC, BIC code, SWIFT ID, SWIFT code or ISO 9362.

https://en.wikipedia.org/wiki/ISO_9362

New in version 1.1.

to_python(value)[source]

Return a string.

class localflavor.generic.forms.DateField(input_formats=None, *args, **kwargs)[source]

A date input field which uses non-US date input formats by default.

class localflavor.generic.forms.DateTimeField(input_formats=None, *args, **kwargs)[source]

A date and time input field which uses non-US date and time input formats by default.

class localflavor.generic.forms.IBANFormField(use_nordea_extensions=False, include_countries=None, *args, **kwargs)[source]

An IBAN consists of up to 34 alphanumeric characters.

To limit validation to specific countries, set the ‘include_countries’ argument with a tuple or list of ISO 3166-1 alpha-2 codes. For example, include_countries=(‘NL’, ‘BE, ‘LU’).

A list of countries that use IBANs as part of SEPA is included for convenience. To use this feature, set include_countries=IBAN_SEPA_COUNTRIES as an argument to the field.

Example:

from django import forms
from localflavor.generic.forms import IBANFormField
from localflavor.generic.countries.sepa import IBAN_SEPA_COUNTRIES

class MyForm(forms.Form):
    iban = IBANFormField(include_countries=IBAN_SEPA_COUNTRIES)

In addition to validating official IBANs, this field can optionally validate unofficial IBANs that have been catalogued by Nordea by setting the use_nordea_extensions argument to True.

https://en.wikipedia.org/wiki/International_Bank_Account_Number

New in version 1.1.

prepare_value(value)[source]

The display format for IBAN has a space every 4 characters.

to_python(value)[source]

Return a string.

class localflavor.generic.forms.SplitDateTimeField(input_date_formats=None, input_time_formats=None, *args, **kwargs)[source]

Split date and time input fields which use non-US date and time input formats by default.

Models

class localflavor.generic.models.BICField(*args, **kwargs)[source]

A BIC consists of 8 (BIC8) or 11 (BIC11) alphanumeric characters.

BICs are also known as SWIFT-BIC, BIC code, SWIFT ID, SWIFT code or ISO 9362.

https://en.wikipedia.org/wiki/ISO_9362

New in version 1.1.

formfield(**kwargs)[source]

Return a django.forms.Field instance for this field.

to_python(value)[source]

Convert the input value into the expected Python data type, raising django.core.exceptions.ValidationError if the data can’t be converted. Return the converted value. Subclasses should override this.

class localflavor.generic.models.IBANField(*args, **kwargs)[source]

An IBAN consists of up to 34 alphanumeric characters.

To limit validation to specific countries, set the ‘include_countries’ argument with a tuple or list of ISO 3166-1 alpha-2 codes. For example, include_countries=(‘NL’, ‘BE, ‘LU’).

A list of countries that use IBANs as part of SEPA is included for convenience. To use this feature, set include_countries=IBAN_SEPA_COUNTRIES as an argument to the field.

Example:

from django.db import models
from localflavor.generic.models import IBANField
from localflavor.generic.countries.sepa import IBAN_SEPA_COUNTRIES

class MyModel(models.Model):
    iban = IBANField(include_countries=IBAN_SEPA_COUNTRIES)

In addition to validating official IBANs, this field can optionally validate unofficial IBANs that have been catalogued by Nordea by setting the use_nordea_extensions argument to True.

https://en.wikipedia.org/wiki/International_Bank_Account_Number

New in version 1.1.

deconstruct()[source]

Return enough information to recreate the field as a 4-tuple:

  • The name of the field on the model, if contribute_to_class() has been run.
  • The import path of the field, including the class:e.g. django.db.models.IntegerField This should be the most portable version, so less specific may be better.
  • A list of positional arguments.
  • A dict of keyword arguments.

Note that the positional or keyword arguments must contain values of the following types (including inner values of collection types):

  • None, bool, str, int, float, complex, set, frozenset, list, tuple, dict
  • UUID
  • datetime.datetime (naive), datetime.date
  • top-level classes, top-level functions - will be referenced by their full import path
  • Storage instances - these have their own deconstruct() method

This is because the values here must be serialized into a text format (possibly new Python code, possibly JSON) and these are the only types with encoding handlers defined.

There’s no need to return the exact way the field was instantiated this time, just ensure that the resulting field is the same - prefer keyword arguments over positional ones, and omit parameters with their default values.

formfield(**kwargs)[source]

Return a django.forms.Field instance for this field.

to_python(value)[source]

Convert the input value into the expected Python data type, raising django.core.exceptions.ValidationError if the data can’t be converted. Return the converted value. Subclasses should override this.

Checksums

Common checksum routines.

localflavor.generic.checksums.luhn(candidate)[source]

Checks a candidate number for validity according to the Luhn algorithm.

Luhn algorithm is used in validation of, for example, credit cards. Both numeric and string candidates are accepted.

localflavor.generic.checksums.ean(candidate)[source]

Checks a candidate number for validity according to the EAN checksum calculation.

Note that this validator does not enforce any length checks (usually 13 or 8).

http://en.wikipedia.org/wiki/International_Article_Number_(EAN)

New in version 1.1.

Validators

class localflavor.generic.validators.BICValidator[source]

A validator for SWIFT Business Identifier Codes (ISO 9362:2009).

Validation is based on the BIC structure found on wikipedia.

https://en.wikipedia.org/wiki/ISO_9362#Structure

deconstruct()

Return a 3-tuple of class import path, positional arguments, and keyword arguments.

class localflavor.generic.validators.EANValidator(strip_nondigits=False, message=None)[source]

A generic validator for EAN like codes with the last digit being the checksum.

http://en.wikipedia.org/wiki/International_Article_Number_(EAN)

deconstruct()

Return a 3-tuple of class import path, positional arguments, and keyword arguments.

class localflavor.generic.validators.IBANValidator(use_nordea_extensions=False, include_countries=None)[source]

A validator for International Bank Account Numbers (IBAN - ISO 13616-1:2007).

deconstruct()

Return a 3-tuple of class import path, positional arguments, and keyword arguments.

static iban_checksum(value)[source]

Returns check digits for an input IBAN number.

Original checksum in input value is ignored.

class localflavor.generic.validators.VATINValidator[source]

A validator for VAT identification numbers.

Currently only supports European VIES VAT identification numbers.

See See https://en.wikipedia.org/wiki/VAT_identification_number

clean(value)[source]

Return tuple of country code and number.

deconstruct()

Return a 3-tuple of class import path, positional arguments, and keyword arguments.

localflavor.generic.validators.VATIN_COUNTRY_CODE_LENGTH = 2

Length of the country code prefix of a VAT identification number.

Codes are two letter ISO 3166-1 alpha-2 codes except for Greece that uses ISO 639-1.

localflavor.generic.validators.VATIN_PATTERN_MAP = {'AT': '^ATU\\d{8}$', 'BE': '^BE0?\\d{9}$', 'BG': '^BG\\d{9,10}$', 'CY': '^CY\\d{8}[A-Z]$', 'CZ': '^CZ\\d{8,10}$', 'DE': '^DE\\d{9}$', 'DK': '^DK\\d{8}$', 'EE': '^EE\\d{9}$', 'EL': '^EL\\d{9}$', 'ES': '^ES[A-Z0-9]\\d{7}[A-Z0-9]$', 'FI': '^FI\\d{8}$', 'FR': '^FR[A-HJ-NP-Z0-9][A-HJ-NP-Z0-9]\\d{9}$', 'GB': '^(GB(GD|HA)\\d{3}|GB\\d{9}|GB\\d{12})$', 'HR': '^HR\\d{11}$', 'HU': '^HU\\d{8}$', 'IE': '^IE\\d[A-Z0-9\\+\\*]\\d{5}[A-Z]{1,2}$', 'IT': '^IT\\d{11}$', 'LT': '^LT(\\d{9}|\\d{12})$', 'LU': '^LU\\d{8}$', 'LV': '^LV\\d{11}$', 'MT': '^MT\\d{8}$', 'NL': '^NL\\d{9}B\\d{2}$', 'PL': '^PL\\d{10}$', 'PT': '^PT\\d{9}$', 'RO': '^RO\\d{2,10}$', 'SE': '^SE\\d{10}01$', 'SI': '^SI\\d{8}$', 'SK': '^SK\\d{10}$'}

Map of country codes and regular expressions.

See https://en.wikipedia.org/wiki/VAT_identification_number

New in version 1.1.

Data

localflavor.generic.countries.iso_3166.ISO_3166_1_ALPHA2_COUNTRY_CODES = ('AD', 'AE', 'AF', 'AG', 'AI', 'AL', 'AM', 'AO', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AW', 'AX', 'AZ', 'BA', 'BB', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BL', 'BM', 'BN', 'BO', 'BQ', 'BR', 'BS', 'BT', 'BV', 'BW', 'BY', 'BZ', 'CA', 'CC', 'CD', 'CF', 'CG', 'CH', 'CI', 'CK', 'CL', 'CM', 'CN', 'CO', 'CR', 'CU', 'CV', 'CW', 'CX', 'CY', 'CZ', 'DE', 'DJ', 'DK', 'DM', 'DO', 'DZ', 'EC', 'EE', 'EG', 'EH', 'ER', 'ES', 'ET', 'FI', 'FJ', 'FK', 'FM', 'FO', 'FR', 'GA', 'GB', 'GD', 'GE', 'GF', 'GG', 'GH', 'GI', 'GL', 'GM', 'GN', 'GP', 'GQ', 'GR', 'GS', 'GT', 'GU', 'GW', 'GY', 'HK', 'HM', 'HN', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IM', 'IN', 'IO', 'IQ', 'IR', 'IS', 'IT', 'JE', 'JM', 'JO', 'JP', 'KE', 'KG', 'KH', 'KI', 'KM', 'KN', 'KP', 'KR', 'KW', 'KY', 'KZ', 'LA', 'LB', 'LC', 'LI', 'LK', 'LR', 'LS', 'LT', 'LU', 'LV', 'LY', 'MA', 'MC', 'MD', 'ME', 'MF', 'MG', 'MH', 'MK', 'ML', 'MM', 'MN', 'MO', 'MP', 'MQ', 'MR', 'MS', 'MT', 'MU', 'MV', 'MW', 'MX', 'MY', 'MZ', 'NA', 'NC', 'NE', 'NF', 'NG', 'NI', 'NL', 'NO', 'NP', 'NR', 'NU', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PK', 'PL', 'PM', 'PN', 'PR', 'PS', 'PT', 'PW', 'PY', 'QA', 'RE', 'RO', 'RS', 'RU', 'RW', 'SA', 'SB', 'SC', 'SD', 'SE', 'SG', 'SH', 'SI', 'SJ', 'SK', 'SL', 'SM', 'SN', 'SO', 'SR', 'SS', 'ST', 'SV', 'SX', 'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR', 'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG', 'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW', 'XK')

ISO 3166-1 country list. Sourced from https://www.iso.org/obp/ui on 2014-11-08

localflavor.generic.countries.sepa.IBAN_SEPA_COUNTRIES = ('AT', 'BE', 'BG', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'ES', 'FI', 'FR', 'GB', 'GI', 'GR', 'HR', 'HU', 'IE', 'IS', 'IT', 'LI', 'LT', 'LU', 'LV', 'MC', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'SE', 'SI', 'SK', 'SM')

European Payments Council list of SEPA scheme countries as of 2 Sep 2015. http://www.europeanpaymentscouncil.eu/index.cfm/knowledge-bank/epc-documents/epc-list-of-sepa-scheme-countries/