app/django/contrib/localflavor/pl/forms.py
changeset 323 ff1a9aa48cfd
parent 54 03e267d67478
equal deleted inserted replaced
322:6641e941ef1e 323:ff1a9aa48cfd
     2 Polish-specific form helpers
     2 Polish-specific form helpers
     3 """
     3 """
     4 
     4 
     5 import re
     5 import re
     6 
     6 
     7 from django.newforms import ValidationError
     7 from django.forms import ValidationError
     8 from django.newforms.fields import Select, RegexField
     8 from django.forms.fields import Select, RegexField
     9 from django.utils.translation import ugettext as _
     9 from django.utils.translation import ugettext_lazy as _
    10 
    10 
    11 class PLVoivodeshipSelect(Select):
    11 class PLProvinceSelect(Select):
    12     """
    12     """
    13     A select widget with list of Polish voivodeships (administrative provinces)
    13     A select widget with list of Polish administrative provinces as choices.
    14     as choices.
       
    15     """
    14     """
    16     def __init__(self, attrs=None):
    15     def __init__(self, attrs=None):
    17         from pl_voivodeships import VOIVODESHIP_CHOICES
    16         from pl_voivodeships import VOIVODESHIP_CHOICES
    18         super(PLVoivodeshipSelect, self).__init__(attrs, choices=VOIVODESHIP_CHOICES)
    17         super(PLProvinceSelect, self).__init__(attrs, choices=VOIVODESHIP_CHOICES)
    19 
    18 
    20 class PLAdministrativeUnitSelect(Select):
    19 class PLCountySelect(Select):
    21     """
    20     """
    22     A select widget with list of Polish administrative units as choices.
    21     A select widget with list of Polish administrative units as choices.
    23     """
    22     """
    24     def __init__(self, attrs=None):
    23     def __init__(self, attrs=None):
    25         from pl_administrativeunits import ADMINISTRATIVE_UNIT_CHOICES
    24         from pl_administrativeunits import ADMINISTRATIVE_UNIT_CHOICES
    26         super(PLAdministrativeUnitSelect, self).__init__(attrs, choices=ADMINISTRATIVE_UNIT_CHOICES)
    25         super(PLCountySelect, self).__init__(attrs, choices=ADMINISTRATIVE_UNIT_CHOICES)
    27 
    26 
    28 class PLNationalIdentificationNumberField(RegexField):
    27 class PLPESELField(RegexField):
    29     """
    28     """
    30     A form field that validates as Polish Identification Number (PESEL).
    29     A form field that validates as Polish Identification Number (PESEL).
    31 
    30 
    32     Checks the following rules:
    31     Checks the following rules:
    33         * the length consist of 11 digits
    32         * the length consist of 11 digits
    39         'invalid': _(u'National Identification Number consists of 11 digits.'),
    38         'invalid': _(u'National Identification Number consists of 11 digits.'),
    40         'checksum': _(u'Wrong checksum for the National Identification Number.'),
    39         'checksum': _(u'Wrong checksum for the National Identification Number.'),
    41     }
    40     }
    42 
    41 
    43     def __init__(self, *args, **kwargs):
    42     def __init__(self, *args, **kwargs):
    44         super(PLNationalIdentificationNumberField, self).__init__(r'^\d{11}$',
    43         super(PLPESELField, self).__init__(r'^\d{11}$',
    45             max_length=None, min_length=None, *args, **kwargs)
    44             max_length=None, min_length=None, *args, **kwargs)
    46 
    45 
    47     def clean(self,value):
    46     def clean(self,value):
    48         super(PLNationalIdentificationNumberField, self).clean(value)
    47         super(PLPESELField, self).clean(value)
    49         if not self.has_valid_checksum(value):
    48         if not self.has_valid_checksum(value):
    50             raise ValidationError(self.error_messages['checksum'])
    49             raise ValidationError(self.error_messages['checksum'])
    51         return u'%s' % value
    50         return u'%s' % value
    52 
    51 
    53     def has_valid_checksum(self, number):
    52     def has_valid_checksum(self, number):
    58         result = 0
    57         result = 0
    59         for i in range(len(number)):
    58         for i in range(len(number)):
    60             result += int(number[i]) * multiple_table[i]
    59             result += int(number[i]) * multiple_table[i]
    61         return result % 10 == 0
    60         return result % 10 == 0
    62 
    61 
    63 class PLTaxNumberField(RegexField):
    62 class PLNIPField(RegexField):
    64     """
    63     """
    65     A form field that validates as Polish Tax Number (NIP).
    64     A form field that validates as Polish Tax Number (NIP).
    66     Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY.
    65     Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY.
    67 
    66 
    68     Checksum algorithm based on documentation at
    67     Checksum algorithm based on documentation at
    72         'invalid': _(u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'),
    71         'invalid': _(u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'),
    73         'checksum': _(u'Wrong checksum for the Tax Number (NIP).'),
    72         'checksum': _(u'Wrong checksum for the Tax Number (NIP).'),
    74     }
    73     }
    75 
    74 
    76     def __init__(self, *args, **kwargs):
    75     def __init__(self, *args, **kwargs):
    77         super(PLTaxNumberField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$',
    76         super(PLNIPField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$',
    78             max_length=None, min_length=None, *args, **kwargs)
    77             max_length=None, min_length=None, *args, **kwargs)
    79 
    78 
    80     def clean(self,value):
    79     def clean(self,value):
    81         super(PLTaxNumberField, self).clean(value)
    80         super(PLNIPField, self).clean(value)
    82         value = re.sub("[-]", "", value)
    81         value = re.sub("[-]", "", value)
    83         if not self.has_valid_checksum(value):
    82         if not self.has_valid_checksum(value):
    84             raise ValidationError(self.error_messages['checksum'])
    83             raise ValidationError(self.error_messages['checksum'])
    85         return u'%s' % value
    84         return u'%s' % value
    86 
    85 
    97         if result == int(number[-1]):
    96         if result == int(number[-1]):
    98             return True
    97             return True
    99         else:
    98         else:
   100             return False
    99             return False
   101 
   100 
   102 class PLNationalBusinessRegisterField(RegexField):
   101 class PLREGONField(RegexField):
   103     """
   102     """
   104     A form field that validated as Polish National Official Business Register Number (REGON)
   103     A form field that validated as Polish National Official Business Register
   105     Valid forms are: 7 or 9 digits number
   104     Number (REGON). Valid numbers contain 7 or 9 digits.
   106 
   105 
   107     More on the field: http://www.stat.gov.pl/bip/regon_ENG_HTML.htm
   106     More on the field: http://www.stat.gov.pl/bip/regon_ENG_HTML.htm
   108 
   107 
   109     The checksum algorithm is documented at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html
   108     The checksum algorithm is documented at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html
   110     """
   109     """
   112         'invalid': _(u'National Business Register Number (REGON) consists of 7 or 9 digits.'),
   111         'invalid': _(u'National Business Register Number (REGON) consists of 7 or 9 digits.'),
   113         'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'),
   112         'checksum': _(u'Wrong checksum for the National Business Register Number (REGON).'),
   114     }
   113     }
   115 
   114 
   116     def __init__(self, *args, **kwargs):
   115     def __init__(self, *args, **kwargs):
   117         super(PLNationalBusinessRegisterField, self).__init__(r'^\d{7,9}$',
   116         super(PLREGONField, self).__init__(r'^\d{7,9}$',
   118             max_length=None, min_length=None, *args, **kwargs)
   117             max_length=None, min_length=None, *args, **kwargs)
   119 
   118 
   120     def clean(self,value):
   119     def clean(self,value):
   121         super(PLNationalBusinessRegisterField, self).clean(value)
   120         super(PLREGONField, self).clean(value)
   122         if not self.has_valid_checksum(value):
   121         if not self.has_valid_checksum(value):
   123             raise ValidationError(self.error_messages['checksum'])
   122             raise ValidationError(self.error_messages['checksum'])
   124         return u'%s' % value
   123         return u'%s' % value
   125 
   124 
   126     def has_valid_checksum(self, number):
   125     def has_valid_checksum(self, number):