def __init__(self, *args, **kwargs): if kwargs.get("max_length", PLZ_LENGTH) < PLZ_LENGTH: raise ValueError( f"Length for a german postal code needs to be at least " f"{PLZ_LENGTH} characters!") kwargs.setdefault("max_length", 5) super().__init__(*args, **kwargs) self.validators.append( validators.RegexValidator(regex=DEZipCodeField().regex, message=_("Invalid German PLZ code.")))
class DEPostalAddressForm(PostalAddressForm): line1 = forms.CharField(label=_(u"Street"), max_length=100) city = forms.CharField(label=_(u"City"), max_length=100) code = DEZipCodeField(label=_(u"Zip Code")) def __init__(self, *args, **kwargs): super(DEPostalAddressForm, self).__init__(*args, **kwargs) self.fields.pop('line2') self.fields.pop('state') self.fields['country'].initial = "DE" self.fields.keyOrder = ['line1', 'code', 'city', 'country']
from localflavor.be.forms import BEPostalCodeField from localflavor.ca.forms import CAPostalCodeField from localflavor.de.forms import DEZipCodeField from localflavor.fr.forms import FRZipCodeField from localflavor.nl.forms import NLZipCodeField logger = logging.getLogger(__name__) mime = magic.Magic(mime=True) # Can safely add more post code form fields here. postal_code_mapping = { 'BE': BEPostalCodeField(), 'CA': CAPostalCodeField(), 'DE': DEZipCodeField(), 'FR': FRZipCodeField(), 'NL': NLZipCodeField(), } def validate_postal_code(value, country_code): if country_code in postal_code_mapping: field = postal_code_mapping[country_code] field.clean(value) # Taken from django 1.11 # TODO: use normal validator once we have upgraded class FileExtensionValidator(object): message = _("File extension '%(extension)s' is not allowed. "
class AddressForm(ModelForm): zip = DEZipCodeField() class Meta: model = Address fields = ('street','city','state','country','zip', 'type', 'public_visible', 'contact_visible')
def __init__(self, *args, **kwargs): super(AddressForm, self).__init__(*args, **kwargs) # Overwrite model self.fields['postcode'] = DEZipCodeField()