From b2f92dfcc511d5bb03e9bd81e073ee7a4e37e7d0 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sat, 15 Sep 2007 12:20:35 +0000 Subject: [PATCH] Fixed #5462 -- Added Peruvian localflavor. Thanks, xbito. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6283 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/pe/__init__.py | 0 django/contrib/localflavor/pe/forms.py | 61 +++++++++++++++++++ .../contrib/localflavor/pe/pe_department.py | 35 +++++++++++ 3 files changed, 96 insertions(+) create mode 100644 django/contrib/localflavor/pe/__init__.py create mode 100644 django/contrib/localflavor/pe/forms.py create mode 100644 django/contrib/localflavor/pe/pe_department.py diff --git a/django/contrib/localflavor/pe/__init__.py b/django/contrib/localflavor/pe/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/localflavor/pe/forms.py b/django/contrib/localflavor/pe/forms.py new file mode 100644 index 0000000000..b1ae215417 --- /dev/null +++ b/django/contrib/localflavor/pe/forms.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- +""" +PE-specific Form helpers. +""" + +from django.newforms import ValidationError +from django.newforms.fields import RegexField, CharField, Select, EMPTY_VALUES +from django.utils.translation import ugettext + +class PEDepartmentSelect(Select): + """ + A Select widget that uses a list of Peruvian Departments as its choices. + """ + def __init__(self, attrs=None): + from pe_department import DEPARTMENT_CHOICES + super(PEDepartmentSelect, self).__init__(attrs, choices=DEPARTMENT_CHOICES) + +class PEDNIField(CharField): + """ + A field that validates `Documento Nacional de IdentidadŽ (DNI) numbers. + """ + def __init__(self, *args, **kwargs): + super(PEDNIField, self).__init__(max_length=8, min_length=8, *args, + **kwargs) + + def clean(self, value): + """ + Value must be a string in the XXXXXXXX formats. + """ + value = super(PEDNIField, self).clean(value) + if value in EMPTY_VALUES: + return u'' + if not value.isdigit(): + raise ValidationError(ugettext("This field requires only numbers.")) + if len(value) != 8: + raise ValidationError(ugettext("This field requires 8 digits.")) + + return value + +class PERUCField(RegexField): + """ + This field validates a RUC (Registro Unico de Contribuyentes). A RUC is of + the form XXXXXXXXXXX. + """ + def __init__(self, *args, **kwargs): + super(PERUCField, self).__init__(max_length=11, min_length=11, *args, + **kwargs) + + def clean(self, value): + """ + Value must be an 11-digit number. + """ + value = super(PERUCField, self).clean(value) + if value in EMPTY_VALUES: + return u'' + if not value.isdigit(): + raise ValidationError(ugettext("This field requires only numbers.")) + if len(value) != 11: + raise ValidationError(ugettext("This field requires 11 digits.")) + return value + diff --git a/django/contrib/localflavor/pe/pe_department.py b/django/contrib/localflavor/pe/pe_department.py new file mode 100644 index 0000000000..d7fa65f9cb --- /dev/null +++ b/django/contrib/localflavor/pe/pe_department.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +""" +A list of Peru departaments as `choices` in a +formfield. + +This exists in this standalone file so that it's only imported into memory +when explicitly needed. +""" + +DEPARTMENT_CHOICES = ( + ('AMA', u'Amazonas'), + ('ANC', u'Ancash'), + ('APU', u'Apurímac'), + ('ARE', u'Arequipa'), + ('AYA', u'Ayacucho'), + ('CAJ', u'Cajamarca'), + ('CUS', u'Cusco'), + ('HUV', u'Huancavelica'), + ('HUC', u'Huánuco'), + ('ICA', u'Ica'), + ('JUN', u'Junín'), + ('LAL', u'La Libertad'), + ('LAM', u'Lambayeque'), + ('LIM', u'Lima'), + ('LOR', u'Loreto'), + ('MDD', u'Madre de Dios'), + ('MOQ', u'Moquegua'), + ('PAS', u'Pasco'), + ('PIU', u'Piura'), + ('PUN', u'Puno'), + ('SAM', u'San Martín'), + ('TAC', u'Tacna'), + ('TUM', u'Tumbes'), + ('UCA', u'Ucayali'), +)