Ticket #5462: pe-localflavor.patch

File pe-localflavor.patch, 3.5 KB (added by Fernando Gutierrez, 17 years ago)
  • django/contrib/localflavor/pe/forms.py

     
     1# -*- coding: utf-8 -*-
     2"""
     3PE-specific Form helpers.
     4"""
     5
     6from django.newforms import ValidationError
     7from django.newforms.fields import RegexField, CharField, Select, EMPTY_VALUES
     8from django.utils.translation import ugettext
     9
     10class PEDepartmentSelect(Select):
     11    """
     12    A Select widget that uses a list of Peruvian Departments
     13    as its choices.
     14    """
     15    def __init__(self, attrs=None):
     16        from pe_department import DEPARTMENT_CHOICES
     17        super(PEDepartmentSelect, self).__init__(attrs, choices=DEPARTMENT_CHOICES)
     18
     19class PEDNIField(CharField):
     20    """
     21    A field that validates `Documento Nacional de IdentidadŽ (DNI) numbers.
     22    """
     23    def __init__(self, *args, **kwargs):
     24        super(PEDNIField, self).__init__(max_length=8, min_length=8, *args,
     25                **kwargs)
     26
     27    def clean(self, value):
     28        """
     29        Value must be a string in the XXXXXXXX formats.
     30        """
     31        value = super(PEDNIField, self).clean(value)
     32        if value in EMPTY_VALUES:
     33            return u''
     34        if not value.isdigit():
     35            raise ValidationError(ugettext("This field requires only numbers."))
     36        if len(value) != 8:
     37            raise ValidationError(ugettext("This field requires 8 digits."))
     38
     39        return value
     40
     41class PERUCField(RegexField):
     42    """
     43    This field validates a RUC (Registro Unico de Contribuyentes). A
     44    RUC is of the form XXXXXXXXXXX.
     45    """
     46    def __init__(self, *args, **kwargs):
     47        super(PERUCField, self).__init__(max_length=11, min_length=11, *args,
     48            **kwargs)
     49
     50    def clean(self, value):
     51        """
     52        Value must be an 11-digit number.
     53        """
     54        value = super(PERUCField, self).clean(value)
     55        if value in EMPTY_VALUES:
     56            return u''
     57        if not value.isdigit():
     58            raise ValidationError(ugettext("This field requires only numbers."))
     59        if len(value) != 11:
     60            raise ValidationError(ugettext("This field requires 11 digits."))
     61        return value
  • django/contrib/localflavor/pe/pe_department.py

     
     1# -*- coding: utf-8 -*-
     2"""
     3A list of Peru departaments as `choices` in a
     4formfield.
     5
     6This exists in this standalone file so that it's only imported into memory
     7when explicitly needed.
     8"""
     9
     10DEPARTMENT_CHOICES = (
     11    ('AMA', u'Amazonas'),
     12    ('ANC', u'Ancash'),
     13    ('APU', u'Apurímac'),
     14    ('ARE', u'Arequipa'),
     15    ('AYA', u'Ayacucho'),
     16    ('CAJ', u'Cajamarca'),
     17    ('CUS', u'Cusco'),
     18    ('HUV', u'Huancavelica'),
     19    ('HUC', u'Huánuco'),
     20    ('ICA', u'Ica'),
     21    ('JUN', u'Junín'),
     22    ('LAL', u'La Libertad'),
     23    ('LAM', u'Lambayeque'),
     24    ('LIM', u'Lima'),
     25    ('LOR', u'Loreto'),
     26    ('MDD', u'Madre de Dios'),
     27    ('MOQ', u'Moquegua'),
     28    ('PAS', u'Pasco'),
     29    ('PIU', u'Piura'),
     30    ('PUN', u'Puno'),
     31    ('SAM', u'San Martín'),
     32    ('TAC', u'Tacna'),
     33    ('TUM', u'Tumbes'),
     34    ('UCA', u'Ucayali'),
     35)
Back to Top