Ticket #5200: pl-localflavor-0.3.diff

File pl-localflavor-0.3.diff, 4.9 KB (added by Slawek Mikula <slawek.mikula@…>, 17 years ago)

Added NIP checksum calculation, added REGON field, updated regression tests

  • django/contrib/localflavor/pl/forms.py

     
    66from django.newforms.fields import Select, RegexField
    77from django.utils.translation import ugettext as _
    88
     9import re
     10
    911class PLVoivodeshipSelect(Select):
    1012    """
    1113    A select widget with list of Polish voivodeships (administrative provinces)
     
    6466    """
    6567    A form field that validates as Polish Tax Number (NIP).
    6668    Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY.
     69
     70    Checksum algorithm based on documentation at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html
    6771    """
     72
     73    def has_valid_checksum(self, number):
     74        """
     75        Calculates a checksum with the provided algorithm.
     76        """
     77        multiple_table = (6, 5, 7, 2, 3, 4, 5, 6, 7)
     78        result = 0
     79        for i in range(len(number)-1):
     80            result += int(number[i])*multiple_table[i]
     81
     82        result %= 11
     83
     84        if result == int(number[-1]):
     85            return True
     86        else:
     87            return False
     88
    6889    def __init__(self, *args, **kwargs):
    6990        super(PLTaxNumberField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$',
    7091            max_length=None, min_length=None,
    7192            error_message=_(u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'),  *args, **kwargs)
    7293
     94    def clean(self,value):
     95        super(PLTaxNumberField, self).clean(value)
     96        value = re.sub("[-]","",value)
     97        if not self.has_valid_checksum(value):
     98            raise ValidationError(_(u'Wrong checksum for the Tax Number (NIP).'))
     99        return u'%s' % value
    73100
     101class PLNationalBusinessRegisterField(RegexField):
     102    """
     103    A form field that validated as Polish National Official Business Register Number (REGON)
     104    Valid forms are:
     105
     106    More on the field: http://www.stat.gov.pl/bip/regon_ENG_HTML.htm
     107
     108    The checksum algorithm is documented at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html
     109    """
     110   
     111    def has_valid_checksum(self, number):
     112        """
     113        Calculates a checksum with the provided algorithm.
     114        """
     115        multiple_table_7 = (2, 3, 4, 5, 6, 7)
     116        multiple_table_9 = (8, 9, 2, 3, 4, 5, 6, 7)
     117        result = 0
     118       
     119        if len(number) == 7:
     120            multiple_table = multiple_table_7
     121        else:
     122            multiple_table = multiple_table_9
     123
     124        for i in range(len(number)-1):
     125            result += int(number[i])*multiple_table[i]
     126
     127        result %= 11
     128
     129        if result == 10:
     130            result = 0
     131
     132        if result  == int(number[-1]):
     133            return True
     134        else:
     135            return False
     136
     137    def __init__(self, *args, **kwargs):
     138        super(PLNationalBusinessRegisterField, self).__init__(r'^\d{7,9}$',
     139            max_length=None, min_length=None, error_message=_(u'National Business Register Number (REGON) consists of 7 or 9 digits.'),
     140            *args, **kwargs)
     141   
     142    def clean(self,value):
     143        super(PLNationalBusinessRegisterField, self).clean(value)
     144        if not self.has_valid_checksum(value):
     145            raise ValidationError(_(u'Wrong checksum for the National Business Register Number (REGON).'))
     146        return u'%s' % value
     147
    74148class PLPostalCodeField(RegexField):
    75149    """
    76150    A form field that validates as Polish postal code.
  • tests/regressiontests/forms/localflavor.py

     
    14231423Traceback (most recent call last):
    14241424...
    14251425ValidationError: [u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.']
    1426 >>> f.clean('43-34-234-323')
    1427 u'43-34-234-323'
    1428 >>> f.clean('433-344-24-23')
    1429 u'433-344-24-23'
     1426>>> f.clean('64-62-414-124')
     1427u'6462414124'
     1428>>> f.clean('646-241-41-24')
     1429u'6462414124'
     1430>>> f.clean('646-241-41-23')
     1431Traceback (most recent call last):
     1432...
     1433ValidationError: [u'Wrong checksum for the Tax Number (NIP).']
    14301434
    14311435# PLNationalIdentificationNumberField ############################################
    14321436
     
    14461450Traceback (most recent call last):
    14471451...
    14481452ValidationError: [u'National Identification Number consists of 11 digits.']
     1453>>>>>>> .r5947
     1454
     1455
     1456# PLNationalBusinessRegisterField ################################################
     1457
     1458>>> from django.contrib.localflavor.pl.forms import PLNationalBusinessRegisterField
     1459>>> f = PLNationalBusinessRegisterField()
     1460>>> f.clean('590096454')
     1461u'590096454'
     1462>>> f.clean('590096453')
     1463Traceback (most recent call last):
     1464...
     1465ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).']
     1466>>> f.clean('590096')
     1467Traceback (most recent call last):
     1468...
     1469ValidationError: [u'National Business Register Number (REGON) consists of 7 or 9 digits.']
     1470
    14491471"""
Back to Top