Django

Code

root/django/trunk/django/contrib/localflavor/pe/forms.py

Revision 8230, 2.2 kB (checked in by mtredinnick, 2 months ago)

Fixed #7575 -- Peru now has regions, not departments (changed at some pointi n
the last year or so). Thanks andresj and programmerq.

This is backwards incompatible if you're using the Peruvian localflavor.

  • Property svn:eol-style set to native
Line 
1 # -*- coding: utf-8 -*-
2 """
3 PE-specific Form helpers.
4 """
5
6 from django.forms import ValidationError
7 from django.forms.fields import RegexField, CharField, Select, EMPTY_VALUES
8 from django.utils.translation import ugettext_lazy as _
9
10 class PERegionSelect(Select):
11     """
12     A Select widget that uses a list of Peruvian Regions as its choices.
13     """
14     def __init__(self, attrs=None):
15         from pe_region import REGION_CHOICES
16         super(PERegionSelect, self).__init__(attrs, choices=REGION_CHOICES)
17
18 class PEDNIField(CharField):
19     """
20     A field that validates `Documento Nacional de IdentidadŽ (DNI) numbers.
21     """
22     default_error_messages = {
23         'invalid': _("This field requires only numbers."),
24         'max_digits': _("This field requires 8 digits."),
25     }
26
27     def __init__(self, *args, **kwargs):
28         super(PEDNIField, self).__init__(max_length=8, min_length=8, *args,
29                 **kwargs)
30
31     def clean(self, value):
32         """
33         Value must be a string in the XXXXXXXX formats.
34         """
35         value = super(PEDNIField, self).clean(value)
36         if value in EMPTY_VALUES:
37             return u''
38         if not value.isdigit():
39             raise ValidationError(self.error_messages['invalid'])
40         if len(value) != 8:
41             raise ValidationError(self.error_messages['max_digits'])
42
43         return value
44
45 class PERUCField(RegexField):
46     """
47     This field validates a RUC (Registro Unico de Contribuyentes). A RUC is of
48     the form XXXXXXXXXXX.
49     """
50     default_error_messages = {
51         'invalid': _("This field requires only numbers."),
52         'max_digits': _("This field requires 11 digits."),
53     }
54
55     def __init__(self, *args, **kwargs):
56         super(PERUCField, self).__init__(max_length=11, min_length=11, *args,
57             **kwargs)
58
59     def clean(self, value):
60         """
61         Value must be an 11-digit number.
62         """
63         value = super(PERUCField, self).clean(value)
64         if value in EMPTY_VALUES:
65             return u''
66         if not value.isdigit():
67             raise ValidationError(self.error_messages['invalid'])
68         if len(value) != 11:
69             raise ValidationError(self.error_messages['max_digits'])
70         return value
Note: See TracBrowser for help on using the browser.