Django

Code

Changeset 6556

Show
Ignore:
Timestamp:
10/20/07 04:24:19 (1 year ago)
Author:
mtredinnick
Message:

Fixed #5200 -- Added some more functionality to the Polish localflavor. Thanks,
Slawek Mikula.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/localflavor/pl/forms.py

    r5940 r6556  
    22Polish-specific form helpers 
    33""" 
     4 
     5import re 
    46 
    57from django.newforms import ValidationError 
     
    3537    """ 
    3638 
    37     def has_valid_checksum(self, number): 
    38         """ 
    39         Calculates a checksum with the provided algorithm. 
    40         """ 
    41         multiple_table = (1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1) 
    42         result = 0 
    43         for i in range(len(number)): 
    44             result += int(number[i])*multiple_table[i] 
    45  
    46         if result % 10 == 0: 
    47             return True 
    48         else: 
    49             return False 
    50  
    5139    def __init__(self, *args, **kwargs): 
    5240        super(PLNationalIdentificationNumberField, self).__init__(r'^\d{11}$', 
     
    6048        return u'%s' % value 
    6149 
     50    def has_valid_checksum(self, number): 
     51        """ 
     52        Calculates a checksum with the provided algorithm. 
     53        """ 
     54        multiple_table = (1, 3, 7, 9, 1, 3, 7, 9, 1, 3, 1) 
     55        result = 0 
     56        for i in range(len(number)): 
     57            result += int(number[i]) * multiple_table[i] 
     58        return result % 10 == 0 
    6259 
    6360class PLTaxNumberField(RegexField): 
     
    6562    A form field that validates as Polish Tax Number (NIP). 
    6663    Valid forms are: XXX-XXX-YY-YY or XX-XX-YYY-YYY. 
     64 
     65    Checksum algorithm based on documentation at 
     66    http://wipos.p.lodz.pl/zylla/ut/nip-rego.html 
    6767    """ 
     68 
    6869    def __init__(self, *args, **kwargs): 
    6970        super(PLTaxNumberField, self).__init__(r'^\d{3}-\d{3}-\d{2}-\d{2}$|^\d{2}-\d{2}-\d{3}-\d{3}$', 
     
    7172            error_message=_(u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'),  *args, **kwargs) 
    7273 
     74    def clean(self,value): 
     75        super(PLTaxNumberField, self).clean(value) 
     76        value = re.sub("[-]", "", value) 
     77        if not self.has_valid_checksum(value): 
     78            raise ValidationError(_(u'Wrong checksum for the Tax Number (NIP).')) 
     79        return u'%s' % value 
     80 
     81    def has_valid_checksum(self, number): 
     82        """ 
     83        Calculates a checksum with the provided algorithm. 
     84        """ 
     85        multiple_table = (6, 5, 7, 2, 3, 4, 5, 6, 7) 
     86        result = 0 
     87        for i in range(len(number)-1): 
     88            result += int(number[i]) * multiple_table[i] 
     89 
     90        result %= 11 
     91        if result == int(number[-1]): 
     92            return True 
     93        else: 
     94            return False 
     95 
     96class PLNationalBusinessRegisterField(RegexField): 
     97    """ 
     98    A form field that validated as Polish National Official Business Register Number (REGON) 
     99    Valid forms are: 7 or 9 digits number 
     100 
     101    More on the field: http://www.stat.gov.pl/bip/regon_ENG_HTML.htm 
     102 
     103    The checksum algorithm is documented at http://wipos.p.lodz.pl/zylla/ut/nip-rego.html 
     104    """ 
     105    def __init__(self, *args, **kwargs): 
     106        super(PLNationalBusinessRegisterField, self).__init__(r'^\d{7,9}$', 
     107            max_length=None, min_length=None, error_message=_(u'National Business Register Number (REGON) consists of 7 or 9 digits.'), 
     108            *args, **kwargs) 
     109 
     110    def clean(self,value): 
     111        super(PLNationalBusinessRegisterField, self).clean(value) 
     112        if not self.has_valid_checksum(value): 
     113            raise ValidationError(_(u'Wrong checksum for the National Business Register Number (REGON).')) 
     114        return u'%s' % value 
     115 
     116    def has_valid_checksum(self, number): 
     117        """ 
     118        Calculates a checksum with the provided algorithm. 
     119        """ 
     120        multiple_table_7 = (2, 3, 4, 5, 6, 7) 
     121        multiple_table_9 = (8, 9, 2, 3, 4, 5, 6, 7) 
     122        result = 0 
     123 
     124        if len(number) == 7: 
     125            multiple_table = multiple_table_7 
     126        else: 
     127            multiple_table = multiple_table_9 
     128 
     129        for i in range(len(number)-1): 
     130            result += int(number[i]) * multiple_table[i] 
     131 
     132        result %= 11 
     133        if result == 10: 
     134            result = 0 
     135        if result  == int(number[-1]): 
     136            return True 
     137        else: 
     138            return False 
    73139 
    74140class PLPostalCodeField(RegexField): 
  • django/trunk/tests/regressiontests/forms/localflavor/pl.py

    r6379 r6556  
    3636... 
    3737ValidationError: [u'Enter a tax number field (NIP) in the format XXX-XXX-XX-XX or XX-XX-XXX-XXX.'] 
    38 >>> f.clean('43-34-234-323') 
    39 u'43-34-234-323' 
    40 >>> f.clean('433-344-24-23') 
    41 u'433-344-24-23' 
    42  
     38>>> f.clean('64-62-414-124') 
     39u'6462414124' 
     40>>> f.clean('646-241-41-24') 
     41u'6462414124' 
     42>>> f.clean('646-241-41-23') 
     43Traceback (most recent call last): 
     44... 
     45ValidationError: [u'Wrong checksum for the Tax Number (NIP).'] 
     46  
    4347# PLNationalIdentificationNumberField ############################################ 
    4448 
     
    5963... 
    6064ValidationError: [u'National Identification Number consists of 11 digits.'] 
     65 
     66# PLNationalBusinessRegisterField ################################################ 
     67 
     68>>> from django.contrib.localflavor.pl.forms import PLNationalBusinessRegisterField 
     69>>> f = PLNationalBusinessRegisterField() 
     70>>> f.clean('590096454') 
     71u'590096454' 
     72>>> f.clean('590096453') 
     73Traceback (most recent call last): 
     74... 
     75ValidationError: [u'Wrong checksum for the National Business Register Number (REGON).'] 
     76>>> f.clean('590096') 
     77Traceback (most recent call last): 
     78... 
     79ValidationError: [u'National Business Register Number (REGON) consists of 7 or 9 digits.'] 
     80 
    6181"""