2007-09-15 20:20:35 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
PE-specific Form helpers.
|
|
|
|
"""
|
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
from django.contrib.localflavor.pe.pe_region import REGION_CHOICES
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.core.validators import EMPTY_VALUES
|
2008-07-19 09:22:26 +08:00
|
|
|
from django.forms import ValidationError
|
2010-01-05 11:56:19 +08:00
|
|
|
from django.forms.fields import RegexField, CharField, Select
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2007-09-15 20:20:35 +08:00
|
|
|
|
2011-10-18 08:47:49 +08:00
|
|
|
|
2008-08-08 23:34:02 +08:00
|
|
|
class PERegionSelect(Select):
|
2007-09-15 20:20:35 +08:00
|
|
|
"""
|
2008-08-08 23:34:02 +08:00
|
|
|
A Select widget that uses a list of Peruvian Regions as its choices.
|
2007-09-15 20:20:35 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
2008-08-08 23:34:02 +08:00
|
|
|
super(PERegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
|
2007-09-15 20:20:35 +08:00
|
|
|
|
|
|
|
class PEDNIField(CharField):
|
|
|
|
"""
|
|
|
|
A field that validates `Documento Nacional de IdentidadŽ (DNI) numbers.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _("This field requires only numbers."),
|
|
|
|
'max_digits': _("This field requires 8 digits."),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=8, min_length=8, *args, **kwargs):
|
|
|
|
super(PEDNIField, self).__init__(max_length, min_length, *args,
|
2007-09-15 20:20:35 +08:00
|
|
|
**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():
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-09-15 20:20:35 +08:00
|
|
|
if len(value) != 8:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['max_digits'])
|
2007-09-15 20:20:35 +08:00
|
|
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
class PERUCField(RegexField):
|
|
|
|
"""
|
|
|
|
This field validates a RUC (Registro Unico de Contribuyentes). A RUC is of
|
|
|
|
the form XXXXXXXXXXX.
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2008-06-18 21:10:05 +08:00
|
|
|
'invalid': _("This field requires only numbers."),
|
|
|
|
'max_digits': _("This field requires 11 digits."),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=11, min_length=11, *args, **kwargs):
|
|
|
|
super(PERUCField, self).__init__(max_length, min_length, *args,
|
2007-09-15 20:20:35 +08:00
|
|
|
**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():
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-09-15 20:20:35 +08:00
|
|
|
if len(value) != 11:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['max_digits'])
|
2007-09-15 20:20:35 +08:00
|
|
|
return value
|
|
|
|
|