| 1 |
# -*- coding: utf-8 -*- |
|---|
| 2 |
""" |
|---|
| 3 |
AR-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.encoding import smart_unicode |
|---|
| 9 |
from django.utils.translation import ugettext_lazy as _ |
|---|
| 10 |
|
|---|
| 11 |
class ARProvinceSelect(Select): |
|---|
| 12 |
""" |
|---|
| 13 |
A Select widget that uses a list of Argentinean provinces/autonomous cities |
|---|
| 14 |
as its choices. |
|---|
| 15 |
""" |
|---|
| 16 |
def __init__(self, attrs=None): |
|---|
| 17 |
from ar_provinces import PROVINCE_CHOICES |
|---|
| 18 |
super(ARProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES) |
|---|
| 19 |
|
|---|
| 20 |
class ARPostalCodeField(RegexField): |
|---|
| 21 |
""" |
|---|
| 22 |
A field that accepts a 'classic' NNNN Postal Code or a CPA. |
|---|
| 23 |
|
|---|
| 24 |
See http://www.correoargentino.com.ar/consulta_cpa/home.php |
|---|
| 25 |
""" |
|---|
| 26 |
default_error_messages = { |
|---|
| 27 |
'invalid': _("Enter a postal code in the format NNNN or ANNNNAAA."), |
|---|
| 28 |
} |
|---|
| 29 |
|
|---|
| 30 |
def __init__(self, *args, **kwargs): |
|---|
| 31 |
super(ARPostalCodeField, self).__init__(r'^\d{4}$|^[A-HJ-NP-Za-hj-np-z]\d{4}\D{3}$', |
|---|
| 32 |
min_length=4, max_length=8, *args, **kwargs) |
|---|
| 33 |
|
|---|
| 34 |
def clean(self, value): |
|---|
| 35 |
value = super(ARPostalCodeField, self).clean(value) |
|---|
| 36 |
if value in EMPTY_VALUES: |
|---|
| 37 |
return u'' |
|---|
| 38 |
if len(value) not in (4, 8): |
|---|
| 39 |
raise ValidationError(self.error_messages['invalid']) |
|---|
| 40 |
if len(value) == 8: |
|---|
| 41 |
return u'%s%s%s' % (value[0].upper(), value[1:5], value[5:].upper()) |
|---|
| 42 |
return value |
|---|
| 43 |
|
|---|
| 44 |
class ARDNIField(CharField): |
|---|
| 45 |
""" |
|---|
| 46 |
A field that validates 'Documento Nacional de Identidad' (DNI) numbers. |
|---|
| 47 |
""" |
|---|
| 48 |
default_error_messages = { |
|---|
| 49 |
'invalid': _("This field requires only numbers."), |
|---|
| 50 |
'max_digits': _("This field requires 7 or 8 digits."), |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
def __init__(self, *args, **kwargs): |
|---|
| 54 |
super(ARDNIField, self).__init__(max_length=10, min_length=7, *args, |
|---|
| 55 |
**kwargs) |
|---|
| 56 |
|
|---|
| 57 |
def clean(self, value): |
|---|
| 58 |
""" |
|---|
| 59 |
Value can be a string either in the [X]X.XXX.XXX or [X]XXXXXXX formats. |
|---|
| 60 |
""" |
|---|
| 61 |
value = super(ARDNIField, self).clean(value) |
|---|
| 62 |
if value in EMPTY_VALUES: |
|---|
| 63 |
return u'' |
|---|
| 64 |
if not value.isdigit(): |
|---|
| 65 |
value = value.replace('.', '') |
|---|
| 66 |
if not value.isdigit(): |
|---|
| 67 |
raise ValidationError(self.error_messages['invalid']) |
|---|
| 68 |
if len(value) not in (7, 8): |
|---|
| 69 |
raise ValidationError(self.error_messages['max_digits']) |
|---|
| 70 |
|
|---|
| 71 |
return value |
|---|
| 72 |
|
|---|
| 73 |
class ARCUITField(RegexField): |
|---|
| 74 |
""" |
|---|
| 75 |
This field validates a CUIT (Código Único de Identificación Tributaria). A |
|---|
| 76 |
CUIT is of the form XX-XXXXXXXX-V. The last digit is a check digit. |
|---|
| 77 |
""" |
|---|
| 78 |
default_error_messages = { |
|---|
| 79 |
'invalid': _('Enter a valid CUIT in XX-XXXXXXXX-X or XXXXXXXXXXXX format.'), |
|---|
| 80 |
'checksum': _("Invalid CUIT."), |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
def __init__(self, *args, **kwargs): |
|---|
| 84 |
super(ARCUITField, self).__init__(r'^\d{2}-?\d{8}-?\d$', |
|---|
| 85 |
*args, **kwargs) |
|---|
| 86 |
|
|---|
| 87 |
def clean(self, value): |
|---|
| 88 |
""" |
|---|
| 89 |
Value can be either a string in the format XX-XXXXXXXX-X or an |
|---|
| 90 |
11-digit number. |
|---|
| 91 |
""" |
|---|
| 92 |
value = super(ARCUITField, self).clean(value) |
|---|
| 93 |
if value in EMPTY_VALUES: |
|---|
| 94 |
return u'' |
|---|
| 95 |
value, cd = self._canon(value) |
|---|
| 96 |
if self._calc_cd(value) != cd: |
|---|
| 97 |
raise ValidationError(self.error_messages['checksum']) |
|---|
| 98 |
return self._format(value, cd) |
|---|
| 99 |
|
|---|
| 100 |
def _canon(self, cuit): |
|---|
| 101 |
cuit = cuit.replace('-', '') |
|---|
| 102 |
return cuit[:-1], cuit[-1] |
|---|
| 103 |
|
|---|
| 104 |
def _calc_cd(self, cuit): |
|---|
| 105 |
mults = (5, 4, 3, 2, 7, 6, 5, 4, 3, 2) |
|---|
| 106 |
tmp = sum([m * int(cuit[idx]) for idx, m in enumerate(mults)]) |
|---|
| 107 |
return str(11 - tmp % 11) |
|---|
| 108 |
|
|---|
| 109 |
def _format(self, cuit, check_digit=None): |
|---|
| 110 |
if check_digit == None: |
|---|
| 111 |
check_digit = cuit[-1] |
|---|
| 112 |
cuit = cuit[:-1] |
|---|
| 113 |
return u'%s-%s-%s' % (cuit[:2], cuit[2:], check_digit) |
|---|