Fixed #12595 -- Fixed bad arguments handling in localflavor form fields.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16146 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel 2011-05-03 11:52:04 +00:00
parent 48cffd9e45
commit 41a1a89e4e
28 changed files with 111 additions and 104 deletions

View File

@ -28,9 +28,9 @@ class ARPostalCodeField(RegexField):
'invalid': _("Enter a postal code in the format NNNN or ANNNNAAA."), 'invalid': _("Enter a postal code in the format NNNN or ANNNNAAA."),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=8, min_length=4, *args, **kwargs):
super(ARPostalCodeField, self).__init__(r'^\d{4}$|^[A-HJ-NP-Za-hj-np-z]\d{4}\D{3}$', super(ARPostalCodeField, self).__init__(r'^\d{4}$|^[A-HJ-NP-Za-hj-np-z]\d{4}\D{3}$',
min_length=4, max_length=8, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
value = super(ARPostalCodeField, self).clean(value) value = super(ARPostalCodeField, self).clean(value)
@ -51,8 +51,8 @@ class ARDNIField(CharField):
'max_digits': _("This field requires 7 or 8 digits."), 'max_digits': _("This field requires 7 or 8 digits."),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=10, min_length=7, *args, **kwargs):
super(ARDNIField, self).__init__(max_length=10, min_length=7, *args, super(ARDNIField, self).__init__(max_length, min_length, *args,
**kwargs) **kwargs)
def clean(self, value): def clean(self, value):
@ -81,9 +81,9 @@ class ARCUITField(RegexField):
'checksum': _("Invalid CUIT."), 'checksum': _("Invalid CUIT."),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(ARCUITField, self).__init__(r'^\d{2}-?\d{8}-?\d$', super(ARCUITField, self).__init__(r'^\d{2}-?\d{8}-?\d$',
*args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
""" """

View File

@ -21,9 +21,9 @@ class ATZipCodeField(RegexField):
default_error_messages = { default_error_messages = {
'invalid': _('Enter a zip code in the format XXXX.'), 'invalid': _('Enter a zip code in the format XXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(ATZipCodeField, self).__init__(r'^\d{4}$', super(ATZipCodeField, self).__init__(r'^\d{4}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class ATStateSelect(Select): class ATStateSelect(Select):
""" """

View File

@ -21,11 +21,9 @@ class AUPostCodeField(RegexField):
'invalid': _('Enter a 4 digit postcode.'), 'invalid': _('Enter a 4 digit postcode.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=4, min_length=None, *args, **kwargs):
if 'max_length' in kwargs:
kwargs.pop('max_length')
super(AUPostCodeField, self).__init__(r'^\d{4}$', super(AUPostCodeField, self).__init__(r'^\d{4}$',
max_length=4, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class AUPhoneNumberField(Field): class AUPhoneNumberField(Field):

View File

@ -23,9 +23,9 @@ class BEPostalCodeField(RegexField):
'Enter a valid postal code in the range and format 1XXX - 9XXX.'), 'Enter a valid postal code in the range and format 1XXX - 9XXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(BEPostalCodeField, self).__init__(r'^[1-9]\d{3}$', super(BEPostalCodeField, self).__init__(r'^[1-9]\d{3}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class BEPhoneNumberField(RegexField): class BEPhoneNumberField(RegexField):
""" """
@ -50,9 +50,9 @@ class BEPhoneNumberField(RegexField):
'0xxxxxxxx or 04xxxxxxxx.'), '0xxxxxxxx or 04xxxxxxxx.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(BEPhoneNumberField, self).__init__(r'^[0]\d{1}[/. ]?\d{3}[. ]\d{2}[. ]?\d{2}$|^[0]\d{2}[/. ]?\d{2}[. ]?\d{2}[. ]?\d{2}$|^[0][4]\d{2}[/. ]?\d{2}[. ]?\d{2}[. ]?\d{2}$', super(BEPhoneNumberField, self).__init__(r'^[0]\d{1}[/. ]?\d{3}[. ]\d{2}[. ]?\d{2}$|^[0]\d{2}[/. ]?\d{2}[. ]?\d{2}[. ]?\d{2}$|^[0][4]\d{2}[/. ]?\d{2}[. ]?\d{2}[. ]?\d{2}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class BERegionSelect(Select): class BERegionSelect(Select):
""" """

View File

@ -17,9 +17,9 @@ class BRZipCodeField(RegexField):
'invalid': _('Enter a zip code in the format XXXXX-XXX.'), 'invalid': _('Enter a zip code in the format XXXXX-XXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$', super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class BRPhoneNumberField(Field): class BRPhoneNumberField(Field):
default_error_messages = { default_error_messages = {
@ -92,8 +92,8 @@ class BRCPFField(CharField):
'digits_only': _("This field requires only numbers."), 'digits_only': _("This field requires only numbers."),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=14, min_length=11, *args, **kwargs):
super(BRCPFField, self).__init__(max_length=14, min_length=11, *args, **kwargs) super(BRCPFField, self).__init__(max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
""" """

View File

@ -25,9 +25,9 @@ class CAPostalCodeField(RegexField):
'invalid': _(u'Enter a postal code in the format XXX XXX.'), 'invalid': _(u'Enter a postal code in the format XXX XXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d$', super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class CAPhoneNumberField(Field): class CAPhoneNumberField(Field):
"""Canadian phone number field.""" """Canadian phone number field."""

View File

@ -17,9 +17,9 @@ class CHZipCodeField(RegexField):
'invalid': _('Enter a zip code in the format XXXX.'), 'invalid': _('Enter a zip code in the format XXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(CHZipCodeField, self).__init__(r'^\d{4}$', super(CHZipCodeField, self).__init__(r'^\d{4}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class CHPhoneNumberField(Field): class CHPhoneNumberField(Field):
""" """

View File

@ -28,9 +28,9 @@ class CZPostalCodeField(RegexField):
'invalid': _(u'Enter a postal code in the format XXXXX or XXX XX.'), 'invalid': _(u'Enter a postal code in the format XXXXX or XXX XX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(CZPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$', super(CZPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
""" """

View File

@ -14,9 +14,9 @@ class DEZipCodeField(RegexField):
default_error_messages = { default_error_messages = {
'invalid': _('Enter a zip code in the format XXXXX.'), 'invalid': _('Enter a zip code in the format XXXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(DEZipCodeField, self).__init__(r'^\d{5}$', super(DEZipCodeField, self).__init__(r'^\d{5}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class DEStateSelect(Select): class DEStateSelect(Select):
""" """

View File

@ -20,10 +20,10 @@ class ESPostalCodeField(RegexField):
'invalid': _('Enter a valid postal code in the range and format 01XXX - 52XXX.'), 'invalid': _('Enter a valid postal code in the range and format 01XXX - 52XXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(ESPostalCodeField, self).__init__( super(ESPostalCodeField, self).__init__(
r'^(0[1-9]|[1-4][0-9]|5[0-2])\d{3}$', r'^(0[1-9]|[1-4][0-9]|5[0-2])\d{3}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class ESPhoneNumberField(RegexField): class ESPhoneNumberField(RegexField):
""" """
@ -40,9 +40,9 @@ class ESPhoneNumberField(RegexField):
'invalid': _('Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.'), 'invalid': _('Enter a valid phone number in one of the formats 6XXXXXXXX, 8XXXXXXXX or 9XXXXXXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(ESPhoneNumberField, self).__init__(r'^(6|7|8|9)\d{8}$', super(ESPhoneNumberField, self).__init__(r'^(6|7|8|9)\d{8}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class ESIdentityCardNumberField(RegexField): class ESIdentityCardNumberField(RegexField):
""" """
@ -71,14 +71,14 @@ class ESIdentityCardNumberField(RegexField):
'invalid_cif': _('Invalid checksum for CIF.'), 'invalid_cif': _('Invalid checksum for CIF.'),
} }
def __init__(self, only_nif=False, *args, **kwargs): def __init__(self, only_nif=False, max_length=None, min_length=None, *args, **kwargs):
self.only_nif = only_nif self.only_nif = only_nif
self.nif_control = 'TRWAGMYFPDXBNJZSQVHLCKE' self.nif_control = 'TRWAGMYFPDXBNJZSQVHLCKE'
self.cif_control = 'JABCDEFGHI' self.cif_control = 'JABCDEFGHI'
self.cif_types = 'ABCDEFGHKLMNPQS' self.cif_types = 'ABCDEFGHKLMNPQS'
self.nie_types = 'XT' self.nie_types = 'XT'
id_card_re = re.compile(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types, self.nif_control + self.cif_control), re.IGNORECASE) id_card_re = re.compile(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types, self.nif_control + self.cif_control), re.IGNORECASE)
super(ESIdentityCardNumberField, self).__init__(id_card_re, max_length=None, min_length=None, super(ESIdentityCardNumberField, self).__init__(id_card_re, max_length, min_length,
error_message=self.default_error_messages['invalid%s' % (self.only_nif and '_only_nif' or '')], error_message=self.default_error_messages['invalid%s' % (self.only_nif and '_only_nif' or '')],
*args, **kwargs) *args, **kwargs)
@ -144,9 +144,9 @@ class ESCCCField(RegexField):
'checksum': _('Invalid checksum for bank account number.'), 'checksum': _('Invalid checksum for bank account number.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(ESCCCField, self).__init__(r'^\d{4}[ -]?\d{4}[ -]?\d{2}[ -]?\d{10}$', super(ESCCCField, self).__init__(r'^\d{4}[ -]?\d{4}[ -]?\d{2}[ -]?\d{10}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
super(ESCCCField, self).clean(value) super(ESCCCField, self).clean(value)

View File

@ -12,9 +12,9 @@ class FIZipCodeField(RegexField):
default_error_messages = { default_error_messages = {
'invalid': _('Enter a zip code in the format XXXXX.'), 'invalid': _('Enter a zip code in the format XXXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(FIZipCodeField, self).__init__(r'^\d{5}$', super(FIZipCodeField, self).__init__(r'^\d{5}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class FIMunicipalitySelect(Select): class FIMunicipalitySelect(Select):
""" """

View File

@ -16,9 +16,9 @@ class FRZipCodeField(RegexField):
'invalid': _('Enter a zip code in the format XXXXX.'), 'invalid': _('Enter a zip code in the format XXXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(FRZipCodeField, self).__init__(r'^\d{5}$', super(FRZipCodeField, self).__init__(r'^\d{5}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class FRPhoneNumberField(Field): class FRPhoneNumberField(Field):
""" """

View File

@ -120,9 +120,9 @@ class HROIBField(RegexField):
'invalid': _('Enter a valid 11 digit OIB'), 'invalid': _('Enter a valid 11 digit OIB'),
} }
def __init__(self, *args, **kwargs): def __init__(self, min_length=11, max_length=11, *args, **kwargs):
super(HROIBField, self).__init__(r'^\d{11}$', super(HROIBField, self).__init__(r'^\d{11}$',
min_length=11, max_length=11, *args, **kwargs) min_length, max_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
super(HROIBField, self).clean(value) super(HROIBField, self).clean(value)

View File

@ -15,9 +15,9 @@ class INZipCodeField(RegexField):
'invalid': gettext(u'Enter a zip code in the format XXXXXXX.'), 'invalid': gettext(u'Enter a zip code in the format XXXXXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(INZipCodeField, self).__init__(r'^\d{6}$', super(INZipCodeField, self).__init__(r'^\d{6}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class INStateField(Field): class INStateField(Field):
""" """

View File

@ -19,9 +19,9 @@ class ISIdNumberField(RegexField):
'checksum': _(u'The Icelandic identification number is not valid.'), 'checksum': _(u'The Icelandic identification number is not valid.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=11, min_length=10, *args, **kwargs):
kwargs['min_length'],kwargs['max_length'] = 10,11 super(ISIdNumberField, self).__init__(r'^\d{6}(-| )?\d{4}$',
super(ISIdNumberField, self).__init__(r'^\d{6}(-| )?\d{4}$', *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
value = super(ISIdNumberField, self).clean(value) value = super(ISIdNumberField, self).clean(value)
@ -61,9 +61,9 @@ class ISPhoneNumberField(RegexField):
Icelandic phone number. Seven digits with an optional hyphen or space after Icelandic phone number. Seven digits with an optional hyphen or space after
the first three digits. the first three digits.
""" """
def __init__(self, *args, **kwargs): def __init__(self, max_length=8, min_length=7, *args, **kwargs):
kwargs['min_length'], kwargs['max_length'] = 7,8 super(ISPhoneNumberField, self).__init__(r'^\d{3}(-| )?\d{4}$',
super(ISPhoneNumberField, self).__init__(r'^\d{3}(-| )?\d{4}$', *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
value = super(ISPhoneNumberField, self).clean(value) value = super(ISPhoneNumberField, self).clean(value)

View File

@ -14,9 +14,9 @@ class ITZipCodeField(RegexField):
default_error_messages = { default_error_messages = {
'invalid': _('Enter a valid zip code.'), 'invalid': _('Enter a valid zip code.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(ITZipCodeField, self).__init__(r'^\d{5}$', super(ITZipCodeField, self).__init__(r'^\d{5}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class ITRegionSelect(Select): class ITRegionSelect(Select):
""" """
@ -44,9 +44,9 @@ class ITSocialSecurityNumberField(RegexField):
'invalid': _(u'Enter a valid Social Security number.'), 'invalid': _(u'Enter a valid Social Security number.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(ITSocialSecurityNumberField, self).__init__(r'^\w{3}\s*\w{3}\s*\w{5}\s*\w{5}$', super(ITSocialSecurityNumberField, self).__init__(r'^\w{3}\s*\w{3}\s*\w{5}\s*\w{5}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
value = super(ITSocialSecurityNumberField, self).clean(value) value = super(ITSocialSecurityNumberField, self).clean(value)

View File

@ -16,9 +16,9 @@ class JPPostalCodeField(RegexField):
'invalid': _('Enter a postal code in the format XXXXXXX or XXX-XXXX.'), 'invalid': _('Enter a postal code in the format XXXXXXX or XXX-XXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$', super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
""" """

View File

@ -13,9 +13,9 @@ class NOZipCodeField(RegexField):
'invalid': _('Enter a zip code in the format XXXX.'), 'invalid': _('Enter a zip code in the format XXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(NOZipCodeField, self).__init__(r'^\d{4}$', super(NOZipCodeField, self).__init__(r'^\d{4}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class NOMunicipalitySelect(Select): class NOMunicipalitySelect(Select):
""" """

View File

@ -25,8 +25,8 @@ class PEDNIField(CharField):
'max_digits': _("This field requires 8 digits."), 'max_digits': _("This field requires 8 digits."),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=8, min_length=8, *args, **kwargs):
super(PEDNIField, self).__init__(max_length=8, min_length=8, *args, super(PEDNIField, self).__init__(max_length, min_length, *args,
**kwargs) **kwargs)
def clean(self, value): def clean(self, value):
@ -53,8 +53,8 @@ class PERUCField(RegexField):
'max_digits': _("This field requires 11 digits."), 'max_digits': _("This field requires 11 digits."),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=11, min_length=11, *args, **kwargs):
super(PERUCField, self).__init__(max_length=11, min_length=11, *args, super(PERUCField, self).__init__(max_length, min_length, *args,
**kwargs) **kwargs)
def clean(self, value): def clean(self, value):

View File

@ -40,9 +40,9 @@ class PLPESELField(RegexField):
'checksum': _(u'Wrong checksum for the National Identification Number.'), 'checksum': _(u'Wrong checksum for the National Identification Number.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(PLPESELField, self).__init__(r'^\d{11}$', super(PLPESELField, self).__init__(r'^\d{11}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
super(PLPESELField, self).clean(value) super(PLPESELField, self).clean(value)
@ -77,9 +77,9 @@ class PLNationalIDCardNumberField(RegexField):
'checksum': _(u'Wrong checksum for the National ID Card Number.'), 'checksum': _(u'Wrong checksum for the National ID Card Number.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(PLNationalIDCardNumberField, self).__init__(r'^[A-Za-z]{3}\d{6}$', super(PLNationalIDCardNumberField, self).__init__(r'^[A-Za-z]{3}\d{6}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self,value): def clean(self,value):
super(PLNationalIDCardNumberField, self).clean(value) super(PLNationalIDCardNumberField, self).clean(value)
@ -129,9 +129,9 @@ class PLNIPField(RegexField):
'checksum': _(u'Wrong checksum for the Tax Number (NIP).'), 'checksum': _(u'Wrong checksum for the Tax Number (NIP).'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(PLNIPField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$', super(PLNIPField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self,value): def clean(self,value):
super(PLNIPField, self).clean(value) super(PLNIPField, self).clean(value)
@ -169,9 +169,9 @@ class PLREGONField(RegexField):
'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'), 'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(PLREGONField, self).__init__(r'^\d{9,14}$', super(PLREGONField, self).__init__(r'^\d{9,14}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self,value): def clean(self,value):
super(PLREGONField, self).clean(value) super(PLREGONField, self).clean(value)
@ -209,7 +209,7 @@ class PLPostalCodeField(RegexField):
'invalid': _(u'Enter a postal code in the format XX-XXX.'), 'invalid': _(u'Enter a postal code in the format XX-XXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(PLPostalCodeField, self).__init__(r'^\d{2}-\d{3}$', super(PLPostalCodeField, self).__init__(r'^\d{2}-\d{3}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)

View File

@ -17,9 +17,9 @@ class PTZipCodeField(RegexField):
'invalid': _('Enter a zip code in the format XXXX-XXX.'), 'invalid': _('Enter a zip code in the format XXXX-XXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(PTZipCodeField, self).__init__(r'^(\d{4}-\d{3}|\d{7})$', super(PTZipCodeField, self).__init__(r'^(\d{4}-\d{3}|\d{7})$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self,value): def clean(self,value):
cleaned = super(PTZipCodeField, self).clean(value) cleaned = super(PTZipCodeField, self).clean(value)

View File

@ -19,9 +19,9 @@ class ROCIFField(RegexField):
'invalid': _("Enter a valid CIF."), 'invalid': _("Enter a valid CIF."),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=10, min_length=2, *args, **kwargs):
super(ROCIFField, self).__init__(r'^(RO)?[0-9]{2,10}', max_length=10, super(ROCIFField, self).__init__(r'^(RO)?[0-9]{2,10}', max_length,
min_length=2, *args, **kwargs) min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
""" """
@ -56,9 +56,9 @@ class ROCNPField(RegexField):
'invalid': _("Enter a valid CNP."), 'invalid': _("Enter a valid CNP."),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=13, min_length=13, *args, **kwargs):
super(ROCNPField, self).__init__(r'^[1-9][0-9]{12}', max_length=13, super(ROCNPField, self).__init__(r'^[1-9][0-9]{12}', max_length,
min_length=13, *args, **kwargs) min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
""" """
@ -143,9 +143,9 @@ class ROIBANField(RegexField):
'invalid': _('Enter a valid IBAN in ROXX-XXXX-XXXX-XXXX-XXXX-XXXX format'), 'invalid': _('Enter a valid IBAN in ROXX-XXXX-XXXX-XXXX-XXXX-XXXX format'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=40, min_length=24, *args, **kwargs):
super(ROIBANField, self).__init__(r'^[0-9A-Za-z\-\s]{24,40}$', super(ROIBANField, self).__init__(r'^[0-9A-Za-z\-\s]{24,40}$',
max_length=40, min_length=24, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
""" """
@ -175,9 +175,9 @@ class ROPhoneNumberField(RegexField):
'invalid': _('Phone numbers must be in XXXX-XXXXXX format.'), 'invalid': _('Phone numbers must be in XXXX-XXXXXX format.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=20, min_length=10, *args, **kwargs):
super(ROPhoneNumberField, self).__init__(r'^[0-9\-\(\)\s]{10,20}$', super(ROPhoneNumberField, self).__init__(r'^[0-9\-\(\)\s]{10,20}$',
max_length=20, min_length=10, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
""" """
@ -200,7 +200,7 @@ class ROPostalCodeField(RegexField):
'invalid': _('Enter a valid postal code in the format XXXXXX'), 'invalid': _('Enter a valid postal code in the format XXXXXX'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=6, min_length=6, *args, **kwargs):
super(ROPostalCodeField, self).__init__(r'^[0-9][0-8][0-9]{4}$', super(ROPostalCodeField, self).__init__(r'^[0-9][0-8][0-9]{4}$',
max_length=6, min_length=6, *args, **kwargs) max_length, min_length, *args, **kwargs)

View File

@ -37,9 +37,9 @@ class RUPostalCodeField(RegexField):
default_error_messages = { default_error_messages = {
'invalid': _(u'Enter a postal code in the format XXXXXX.'), 'invalid': _(u'Enter a postal code in the format XXXXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(RUPostalCodeField, self).__init__(r'^\d{6}$', super(RUPostalCodeField, self).__init__(r'^\d{6}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class RUPassportNumberField(RegexField): class RUPassportNumberField(RegexField):
@ -50,9 +50,9 @@ class RUPassportNumberField(RegexField):
default_error_messages = { default_error_messages = {
'invalid': _(u'Enter a passport number in the format XXXX XXXXXX.'), 'invalid': _(u'Enter a passport number in the format XXXX XXXXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(RUPassportNumberField, self).__init__(r'^\d{4} \d{6}$', super(RUPassportNumberField, self).__init__(r'^\d{4} \d{6}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class RUAlienPassportNumberField(RegexField): class RUAlienPassportNumberField(RegexField):
@ -63,6 +63,6 @@ class RUAlienPassportNumberField(RegexField):
default_error_messages = { default_error_messages = {
'invalid': _(u'Enter a passport number in the format XX XXXXXXX.'), 'invalid': _(u'Enter a passport number in the format XX XXXXXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(RUAlienPassportNumberField, self).__init__(r'^\d{2} \d{7}$', super(RUAlienPassportNumberField, self).__init__(r'^\d{2} \d{7}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)

View File

@ -30,9 +30,9 @@ class SKPostalCodeField(RegexField):
'invalid': _(u'Enter a postal code in the format XXXXX or XXX XX.'), 'invalid': _(u'Enter a postal code in the format XXXXX or XXX XX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(SKPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$', super(SKPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
""" """

View File

@ -16,9 +16,9 @@ class TRPostalCodeField(RegexField):
'invalid': _(u'Enter a postal code in the format XXXXX.'), 'invalid': _(u'Enter a postal code in the format XXXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=5, min_length=5, *args, **kwargs):
super(TRPostalCodeField, self).__init__(r'^\d{5}$', super(TRPostalCodeField, self).__init__(r'^\d{5}$',
max_length=5, min_length=5, *args, **kwargs) max_length, min_length, *args, **kwargs)
def clean(self, value): def clean(self, value):
value = super(TRPostalCodeField, self).clean(value) value = super(TRPostalCodeField, self).clean(value)

View File

@ -17,9 +17,9 @@ class USZipCodeField(RegexField):
'invalid': _('Enter a zip code in the format XXXXX or XXXXX-XXXX.'), 'invalid': _('Enter a zip code in the format XXXXX or XXXXX-XXXX.'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(USZipCodeField, self).__init__(r'^\d{5}(?:-\d{4})?$', super(USZipCodeField, self).__init__(r'^\d{5}(?:-\d{4})?$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)
class USPhoneNumberField(CharField): class USPhoneNumberField(CharField):
default_error_messages = { default_error_messages = {

View File

@ -4,7 +4,7 @@ South Africa-specific Form helpers
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms import ValidationError from django.forms import ValidationError
from django.forms.fields import Field, RegexField from django.forms.fields import CharField, RegexField
from django.utils.checksums import luhn from django.utils.checksums import luhn
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
import re import re
@ -12,7 +12,7 @@ from datetime import date
id_re = re.compile(r'^(?P<yy>\d\d)(?P<mm>\d\d)(?P<dd>\d\d)(?P<mid>\d{4})(?P<end>\d{3})') id_re = re.compile(r'^(?P<yy>\d\d)(?P<mm>\d\d)(?P<dd>\d\d)(?P<mid>\d{4})(?P<end>\d{3})')
class ZAIDField(Field): class ZAIDField(CharField):
"""A form field for South African ID numbers -- the checksum is validated """A form field for South African ID numbers -- the checksum is validated
using the Luhn checksum, and uses a simlistic (read: not entirely accurate) using the Luhn checksum, and uses a simlistic (read: not entirely accurate)
check for the birthdate check for the birthdate
@ -55,6 +55,6 @@ class ZAPostCodeField(RegexField):
'invalid': _(u'Enter a valid South African postal code'), 'invalid': _(u'Enter a valid South African postal code'),
} }
def __init__(self, *args, **kwargs): def __init__(self, max_length=None, min_length=None, *args, **kwargs):
super(ZAPostCodeField, self).__init__(r'^\d{4}$', super(ZAPostCodeField, self).__init__(r'^\d{4}$',
max_length=None, min_length=None, *args, **kwargs) max_length, min_length, *args, **kwargs)

View File

@ -1,5 +1,6 @@
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.core.validators import EMPTY_VALUES from django.core.validators import EMPTY_VALUES
from django.forms.fields import CharField
from django.test.utils import get_warnings_state, restore_warnings_state from django.test.utils import get_warnings_state, restore_warnings_state
from django.utils.unittest import TestCase from django.utils.unittest import TestCase
@ -13,8 +14,8 @@ class LocalFlavorTestCase(TestCase):
def restore_warnings_state(self): def restore_warnings_state(self):
restore_warnings_state(self._warnings_state) restore_warnings_state(self._warnings_state)
def assertFieldOutput(self, fieldclass, valid, invalid, field_args=[], def assertFieldOutput(self, fieldclass, valid, invalid, field_args=None,
field_kwargs={}, empty_value=u''): field_kwargs=None, empty_value=u''):
""" """
Asserts that a field behaves correctly with various inputs. Asserts that a field behaves correctly with various inputs.
@ -29,6 +30,10 @@ class LocalFlavorTestCase(TestCase):
empty_value: the expected clean output for inputs in EMPTY_VALUES empty_value: the expected clean output for inputs in EMPTY_VALUES
""" """
if field_args is None:
field_args = []
if field_kwargs is None:
field_kwargs = {}
required = fieldclass(*field_args, **field_kwargs) required = fieldclass(*field_args, **field_kwargs)
optional = fieldclass(*field_args, **dict(field_kwargs, required=False)) optional = fieldclass(*field_args, **dict(field_kwargs, required=False))
# test valid inputs # test valid inputs
@ -49,3 +54,7 @@ class LocalFlavorTestCase(TestCase):
self.assertRaisesRegexp(ValidationError, error_required, self.assertRaisesRegexp(ValidationError, error_required,
required.clean, e) required.clean, e)
self.assertEqual(optional.clean(e), empty_value) self.assertEqual(optional.clean(e), empty_value)
# test that max_length and min_length are always accepted
if issubclass(fieldclass, CharField):
field_kwargs.update({'min_length':2, 'max_length':20})
self.assertTrue(isinstance(fieldclass(*field_args, **field_kwargs), fieldclass))