Django

Code

Changeset 9130

Show
Ignore:
Timestamp:
10/05/08 01:00:55 (3 months ago)
Author:
mtredinnick
Message:

[1.0.X] Fixed #9259 -- Fixed a validation error for Spanish identity card numbers.
Patch from Marc Garcia.

Backport of r9129 from trunk.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/releases/1.0.X/django/contrib/localflavor/es/forms.py

    r8405 r9130  
    7777        self.cif_types = 'ABCDEFGHKLMNPQS' 
    7878        self.nie_types = 'XT' 
    79         super(ESIdentityCardNumberField, self).__init__(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types + self.cif_types.lower() + self.nie_types.lower(), self.nif_control + self.nif_control.lower()), 
    80                 max_length=None, min_length=None, 
     79        id_card_re = re.compile(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types, self.nif_control + self.cif_control), re.IGNORECASE) 
     80        super(ESIdentityCardNumberField, self).__init__(id_card_re, max_length=None, min_length=None, 
    8181                error_message=self.default_error_messages['invalid%s' % (self.only_nif and '_only_nif' or '')], 
    8282                *args, **kwargs) 
     
    8989 
    9090        value = value.upper().replace(' ', '').replace('-', '') 
    91         m = re.match(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types, self.nif_control), value) 
     91        m = re.match(r'^([%s]?)[ -]?(\d+)[ -]?([%s]?)$' % (self.cif_types + self.nie_types, self.nif_control + self.cif_control), value) 
    9292        letter1, number, letter2 = m.groups() 
    9393 
  • django/branches/releases/1.0.X/tests/regressiontests/forms/localflavor/es.py

    r8405 r9130  
    136136>>> from django.contrib.localflavor.es.forms import ESIdentityCardNumberField 
    137137>>> f = ESIdentityCardNumberField() 
     138>>> f.clean('78699688J') 
     139'78699688J' 
     140>>> f.clean('78699688-J') 
     141'78699688J' 
     142>>> f.clean('78699688 J') 
     143'78699688J' 
     144>>> f.clean('78699688 j') 
     145'78699688J' 
     146>>> f.clean('78699688T') 
     147Traceback (most recent call last): 
     148... 
     149ValidationError: [u'Invalid checksum for NIF.'] 
     150>>> f.clean('X0901797J') 
     151'X0901797J' 
     152>>> f.clean('X-6124387-Q') 
     153'X6124387Q' 
     154>>> f.clean('X 0012953 G') 
     155'X0012953G' 
     156>>> f.clean('x-3287690-r') 
     157'X3287690R' 
     158>>> f.clean('t-03287690r') 
     159'T03287690R' 
     160>>> f.clean('P2907500I') 
     161'P2907500I' 
     162>>> f.clean('X-03287690') 
     163Traceback (most recent call last): 
     164... 
     165ValidationError: [u'Please enter a valid NIF, NIE, or CIF.'] 
     166>>> f.clean('X-03287690-T') 
     167Traceback (most recent call last): 
     168... 
     169ValidationError: [u'Invalid checksum for NIE.'] 
     170>>> f.clean('B38790911') 
     171'B38790911' 
     172>>> f.clean('B31234560') 
     173'B31234560' 
     174>>> f.clean('B-3879091A') 
     175'B3879091A' 
     176>>> f.clean('B 38790917') 
     177Traceback (most recent call last): 
     178... 
     179ValidationError: [u'Invalid checksum for CIF.'] 
     180>>> f.clean('B 38790911') 
     181'B38790911' 
     182>>> f.clean('P-3900800-H') 
     183'P3900800H' 
     184>>> f.clean('P 39008008') 
     185'P39008008' 
     186>>> f.clean('C-28795565') 
     187'C28795565' 
     188>>> f.clean('C 2879556E') 
     189'C2879556E' 
     190>>> f.clean('C28795567') 
     191Traceback (most recent call last): 
     192... 
     193ValidationError: [u'Invalid checksum for CIF.'] 
     194>>> f.clean('I38790911') 
     195Traceback (most recent call last): 
     196... 
     197ValidationError: [u'Please enter a valid NIF, NIE, or CIF.'] 
     198>>> f.clean('78699688-2') 
     199Traceback (most recent call last): 
     200... 
     201ValidationError: [u'Please enter a valid NIF, NIE, or CIF.'] 
     202>>> f.clean('') 
     203Traceback (most recent call last): 
     204... 
     205ValidationError: [u'This field is required.'] 
     206 
     207>>> f = ESIdentityCardNumberField(required=False) 
    138208>>> f.clean('78699688J') 
    139209'78699688J' 
     
    168238>>> f.clean('B38790911') 
    169239'B38790911' 
    170 >>> f.clean('B31234560') 
    171 'B31234560' 
    172240>>> f.clean('B-3879091A') 
    173241'B3879091A' 
     
    199267ValidationError: [u'Please enter a valid NIF, NIE, or CIF.'] 
    200268>>> f.clean('') 
    201 Traceback (most recent call last): 
    202 ... 
    203 ValidationError: [u'This field is required.'] 
    204  
    205 >>> f = ESIdentityCardNumberField(required=False) 
    206 >>> f.clean('78699688J') 
    207 '78699688J' 
    208 >>> f.clean('78699688-J') 
    209 '78699688J' 
    210 >>> f.clean('78699688 J') 
    211 '78699688J' 
    212 >>> f.clean('78699688 j') 
    213 '78699688J' 
    214 >>> f.clean('78699688T') 
    215 Traceback (most recent call last): 
    216 ... 
    217 ValidationError: [u'Invalid checksum for NIF.'] 
    218 >>> f.clean('X0901797J') 
    219 'X0901797J' 
    220 >>> f.clean('X-6124387-Q') 
    221 'X6124387Q' 
    222 >>> f.clean('X 0012953 G') 
    223 'X0012953G' 
    224 >>> f.clean('x-3287690-r') 
    225 'X3287690R' 
    226 >>> f.clean('t-03287690r') 
    227 'T03287690R' 
    228 >>> f.clean('X-03287690') 
    229 Traceback (most recent call last): 
    230 ... 
    231 ValidationError: [u'Please enter a valid NIF, NIE, or CIF.'] 
    232 >>> f.clean('X-03287690-T') 
    233 Traceback (most recent call last): 
    234 ... 
    235 ValidationError: [u'Invalid checksum for NIE.'] 
    236 >>> f.clean('B38790911') 
    237 'B38790911' 
    238 >>> f.clean('B-3879091A') 
    239 'B3879091A' 
    240 >>> f.clean('B 38790917') 
    241 Traceback (most recent call last): 
    242 ... 
    243 ValidationError: [u'Invalid checksum for CIF.'] 
    244 >>> f.clean('B 38790911') 
    245 'B38790911' 
    246 >>> f.clean('P-3900800-H') 
    247 'P3900800H' 
    248 >>> f.clean('P 39008008') 
    249 'P39008008' 
    250 >>> f.clean('C-28795565') 
    251 'C28795565' 
    252 >>> f.clean('C 2879556E') 
    253 'C2879556E' 
    254 >>> f.clean('C28795567') 
    255 Traceback (most recent call last): 
    256 ... 
    257 ValidationError: [u'Invalid checksum for CIF.'] 
    258 >>> f.clean('I38790911') 
    259 Traceback (most recent call last): 
    260 ... 
    261 ValidationError: [u'Please enter a valid NIF, NIE, or CIF.'] 
    262 >>> f.clean('78699688-2') 
    263 Traceback (most recent call last): 
    264 ... 
    265 ValidationError: [u'Please enter a valid NIF, NIE, or CIF.'] 
    266 >>> f.clean('') 
    267269u'' 
    268270