Ticket #10736: uy_localflavor.diff

File uy_localflavor.diff, 7.4 KB (added by gonz, 6 years ago)
  • django/contrib/localflavor/uy/util.py

     
     1# -*- coding: utf-8 -*-
     2
     3def get_validation_digit(number):
     4    """ Calculates the validation digit for the given number. """
     5    sum = 0
     6    dvs = [4, 3, 6, 7, 8, 9, 2]
     7    number = str(number)
     8
     9    for i in range(0, len(number)):
     10        sum = (int(number[-1 - i]) * dvs[i] + sum) % 10
     11
     12    return (10-sum) % 10
  • django/contrib/localflavor/uy/uy_departaments.py

     
     1# -*- coding: utf-8 -*-
     2"""A list of Urguayan departaments as `choices` in a formfield."""
     3
     4DEPARTAMENT_CHOICES = (
     5    ('G', u'Artigas'),
     6    ('A', u'Canelones'),
     7    ('E', u'Cerro Largo'),
     8    ('L', u'Colonia'),
     9    ('Q', u'Durazno'),
     10    ('N', u'Flores'),
     11    ('O', u'Florida'),
     12    ('P', u'Lavalleja'),
     13    ('B', u'Maldonado'),
     14    ('S', u'Montevideo'),
     15    ('I', u'Paysandú'),
     16    ('J', u'Río Negro'),
     17    ('F', u'Rivera'),
     18    ('C', u'Rocha'),
     19    ('H', u'Salto'),
     20    ('M', u'San José'),
     21    ('K', u'Soriano'),
     22    ('R', u'Tacuarembó'),
     23    ('D', u'Treinta y Tres'),
     24)
  • django/contrib/localflavor/uy/forms.py

     
     1# -*- coding: utf-8 -*-
     2"""
     3UY-specific form helpers.
     4"""
     5import re
     6
     7from django.forms.fields import Select, RegexField, EMPTY_VALUES
     8from django.forms import ValidationError
     9from django.utils.translation import ugettext_lazy as _
     10from django.contrib.localflavor.uy.util import get_validation_digit
     11
     12
     13class UYDepartamentSelect(Select):
     14    """
     15    A Select widget that uses a list of Uruguayan departaments as its choices.
     16    """
     17    def __init__(self, attrs=None):
     18        from uy_departaments import DEPARTAMENT_CHOICES
     19        super(UYDepartamentSelect, self).__init__(attrs, choices=DEPARTAMENT_CHOICES)
     20
     21
     22class UYCIField(RegexField):
     23    """
     24    A field that validates Uruguayan 'Cedula de identidad' (CI) numbers.
     25    """
     26    default_error_messages = {
     27        'invalid': _("Enter a valid CI number in X.XXX.XXX-X,"
     28                     "XXXXXXX-X or XXXXXXXX format."),
     29        'invalid_validation_digit': _("Enter a valid CI number."),
     30    }
     31
     32    def __init__(self, *args, **kwargs):
     33        super(UYCIField, self).__init__(r'(?P<num>(\d{6,7}|(\d\.)?\d{3}\.\d{3}))-?(?P<val>\d)',
     34                                        *args, **kwargs)
     35
     36    def clean(self, value):
     37        """
     38        Validates format and validation digit.
     39
     40        The official format is [X.]XXX.XXX-X but usually dots and/or slash are
     41        omitted so, when validating, those characters are ignored if found in
     42        the correct place. The three typically used formats are supported:
     43        [X]XXXXXXX, [X]XXXXXX-X and [X.]XXX.XXX-X.
     44        """
     45
     46        value = super(UYCIField, self).clean(value)
     47        if value in EMPTY_VALUES:
     48            return u''
     49        match = self.regex.match(value)
     50        if not match:
     51            raise ValidationError(self.error_messages['invalid'])
     52
     53        number = int(match.group('num').replace('.', ''))
     54        validation_digit = int(match.group('val'))
     55
     56        if not validation_digit == get_validation_digit(number):
     57            raise ValidationError(self.error_messages['invalid_validation_digit'])
     58
     59        return value
  • tests/regressiontests/forms/localflavor/uy.py

     
     1# -*- coding: utf-8 -*-
     2# Tests for the contrib/localflavor/ UY form fields.
     3
     4tests = r"""
     5# UYDepartamentSelect #########################################################
     6
     7>>> from django.contrib.localflavor.uy.forms import UYDepartamentSelect
     8>>> f = UYDepartamentSelect()
     9>>> f.render('departamentos', 'S')
     10u'<select name="departamentos">\n<option value="G">Artigas</option>\n<option value="A">Canelones</option>\n<option value="E">Cerro Largo</option>\n<option value="L">Colonia</option>\n<option value="Q">Durazno</option>\n<option value="N">Flores</option>\n<option value="O">Florida</option>\n<option value="P">Lavalleja</option>\n<option value="B">Maldonado</option>\n<option value="S" selected="selected">Montevideo</option>\n<option value="I">Paysand\xfa</option>\n<option value="J">R\xedo Negro</option>\n<option value="F">Rivera</option>\n<option value="C">Rocha</option>\n<option value="H">Salto</option>\n<option value="M">San Jos\xe9</option>\n<option value="K">Soriano</option>\n<option value="R">Tacuaremb\xf3</option>\n<option value="D">Treinta y Tres</option>\n</select>'
     11
     12# UYCIField ###################################################################
     13
     14>>> from django.contrib.localflavor.uy.util import get_validation_digit
     15>>> get_validation_digit(409805) == 3
     16True
     17>>> get_validation_digit(1005411) == 2
     18True
     19
     20>>> from django.contrib.localflavor.uy.forms import UYCIField
     21>>> f = UYCIField()
     22>>> f.clean('4098053')
     23u'4098053'
     24>>> f.clean('409805-3')
     25u'409805-3'
     26>>> f.clean('409.805-3')
     27u'409.805-3'
     28>>> f.clean('10054112')
     29u'10054112'
     30>>> f.clean('1005411-2')
     31u'1005411-2'
     32>>> f.clean('1.005.411-2')
     33u'1.005.411-2'
     34>>> f.clean('foo')
     35Traceback (most recent call last):
     36...
     37ValidationError: [u'Enter a valid CI number in X.XXX.XXX-X,XXXXXXX-X or XXXXXXXX format.']
     38>>> f.clean('409805-2')
     39Traceback (most recent call last):
     40...
     41ValidationError: [u'Enter a valid CI number.']
     42>>> f.clean('1.005.411-5')
     43Traceback (most recent call last):
     44...
     45ValidationError: [u'Enter a valid CI number.']
     46"""
  • tests/regressiontests/forms/tests.py

     
    2525from localflavor.sk import tests as localflavor_sk_tests
    2626from localflavor.uk import tests as localflavor_uk_tests
    2727from localflavor.us import tests as localflavor_us_tests
     28from localflavor.uy import tests as localflavor_uy_tests
    2829from localflavor.za import tests as localflavor_za_tests
    2930from regressions import tests as regression_tests
    3031from util import tests as util_tests
     
    5960    'localflavor_sk_tests': localflavor_sk_tests,
    6061    'localflavor_uk_tests': localflavor_uk_tests,
    6162    'localflavor_us_tests': localflavor_us_tests,
     63    'localflavor_uy_tests': localflavor_uy_tests,
    6264    'localflavor_za_tests': localflavor_za_tests,
    6365    'regression_tests': regression_tests,
    6466    'formset_tests': formset_tests,
  • AUTHORS

     
    456456    Mykola Zamkovoi <nickzam@gmail.com>
    457457    Jarek Zgoda <jarek.zgoda@gmail.com>
    458458    Cheng Zhang
     459    Gonzalo Saavedra <gonzalosaavedra@gmail.com>
    459460
    460461A big THANK YOU goes to:
    461462
Back to Top