Ticket #4122: localflavor_cl.diff

File localflavor_cl.diff, 5.3 KB (added by marijn <marijn@…>, 18 years ago)
  • django/contrib/localflavor/cl/forms.py

     
     1"""
     2Chile specific form helpers.
     3"""
     4from django.newforms import ValidationError
     5from django.newforms.fields import RegexField, EMPTY_VALUES
     6from django.utils.translation import gettext
     7
     8class CLRutField(RegexField):
     9    """
     10    Chilean "Rol Unico Tributario" (RUT) field. This is the chilean national identification number.
     11    """
     12    def __init__(self, *args, **kwargs):
     13        if kwargs.get('strict')  == True:
     14            del kwargs['strict']
     15            super(CLRutField, self).__init__(r'^(\d{1,2}\.)?\d{3}\.\d{3}-[\dkK]$',
     16                error_message=gettext(u'Enter valid a Chilean Rut. The format is XX.XXX.XXX-X.'),
     17                *args, **kwargs)
     18        else: # We take it relaxed and format it     for the user,
     19                  # accepting RUTs that are validate, but do not exist in the real world.
     20            super(CLRutField, self).__init__(r'^[\d\.]{1,11}-?[\dkK]$',
     21                error_message=gettext(u'Enter valid a Chilean RUT'),
     22                *args, **kwargs)
     23       
     24    def clean(self, value):
     25        """
     26        Check and clean the chilean rut.
     27        """
     28        super(CLRutField, self).clean(value)
     29        if value in EMPTY_VALUES:
     30            return u''
     31        (rut, verificador) = self._canonify(value)
     32        if self._algorithm(rut) == verificador:
     33            return self._format(rut, verificador)
     34        else:
     35            raise ValidationError(u'The Chilean RUT is not valid.')
     36       
     37   
     38    def _algorithm(self, rut):
     39        """
     40        Takes RUT in pure canonical form, calculates the verifier digit.
     41        """
     42        suma  = 0
     43        multi = 2
     44        for r in rut[::-1]:
     45            suma  += int (r) * multi
     46            multi += 1
     47            if multi == 8:
     48                multi = 2
     49        rest = suma % 11
     50        rest = 11 - rest
     51        if rest == 11: return '0'
     52        elif rest == 10: return 'K'
     53        else: return str(rest)
     54
     55    def _canonify(self, rut):
     56        """
     57        Turns the rut into one normalized format. Returns a (rut, verifier) tuple.
     58        """
     59        rut = str(rut)
     60        rut = rut.replace(' ', '')
     61        rut = rut.replace('.', '')
     62        rut = rut.replace('-', '')
     63        return (rut[:-1], rut[-1])
     64
     65    def _format(self, code, verifier=None):
     66        """
     67        Formats the rut from canonical form to the common string representation.
     68        If verifier=None, then the last digit in 'code' is the verifier.
     69        """
     70        if(verifier == None):
     71            verifier = code[-1]
     72            code = code[:-1]
     73        while len(code) > 3 and (code.find('.') > 3 or code.find('.') == -1):
     74            if code.find('.') == -1:
     75                new_dot = -3
     76            else:
     77                new_dot = code.find('.')-3
     78            code = code[:new_dot] +'.'+code[new_dot:]   
     79        return '%s-%s' % (code, verifier)
     80
  • tests/regressiontests/forms/localflavor.py

     
    10521052<option value="VIC">Victoria</option>
    10531053<option value="WA">Western Australia</option>
    10541054</select>
     1055
     1056
     1057## CLRutField #############################################################
     1058
     1059CLRutField is a Field that checks the validity of the Chilean
     1060personal identification number (RUT). It has two modes relaxed (default) and
     1061strict.
     1062
     1063>>> from django.contrib.localflavor.cl.forms import CLRutField
     1064>>> rut = CLRutField()
     1065
     1066>>> rut.clean('11-6')
     1067'11-6'
     1068>>> rut.clean('116')
     1069'11-6'
     1070
     1071# valid format, bad verifier.
     1072>>> rut.clean('11.111.111-0')
     1073Traceback (most recent call last):
     1074...
     1075ValidationError: [u'The Chilean RUT is not valid.']
     1076>>> rut.clean('111')
     1077Traceback (most recent call last):
     1078...
     1079ValidationError: [u'The Chilean RUT is not valid.']
     1080
     1081>>> rut.clean('767484100')
     1082'76.748.410-0'
     1083>>> rut.clean('78.412.790-7')
     1084'78.412.790-7'
     1085>>> rut.clean('8.334.6043')
     1086'8.334.604-3'
     1087>>> rut.clean('76793310-K')
     1088'76.793.310-K'
     1089
     1090Strict RUT usage (does not allow imposible values)
     1091>>> rut = CLRutField(strict=True)
     1092
     1093>>> rut.clean('11-6')
     1094Traceback (most recent call last):
     1095...
     1096ValidationError: [u'Enter valid a Chilean Rut. The format is XX.XXX.XXX-X.']
     1097
     1098# valid format, bad verifier.
     1099>>> rut.clean('11.111.111-0')
     1100Traceback (most recent call last):
     1101...
     1102ValidationError: [u'The Chilean RUT is not valid.']
     1103
     1104# Correct input, invalid format.
     1105>>> rut.clean('767484100')
     1106Traceback (most recent call last):
     1107...
     1108ValidationError: [u'Enter valid a Chilean Rut. The format is XX.XXX.XXX-X.']
     1109>>> rut.clean('78.412.790-7')
     1110'78.412.790-7'
     1111>>> rut.clean('8.334.6043')
     1112Traceback (most recent call last):
     1113...
     1114ValidationError: [u'Enter valid a Chilean Rut. The format is XX.XXX.XXX-X.']
     1115>>> rut.clean('76793310-K')
     1116Traceback (most recent call last):
     1117...
     1118ValidationError: [u'Enter valid a Chilean Rut. The format is XX.XXX.XXX-X.']
     1119
    10551120"""
Back to Top