2007-03-31 17:05:54 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
BR-specific Form helpers
|
|
|
|
"""
|
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import absolute_import, unicode_literals
|
2011-10-18 08:47:49 +08:00
|
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
from django.contrib.localflavor.br.br_states import STATE_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 Field, RegexField, CharField, Select
|
2007-04-04 14:34:19 +08:00
|
|
|
from django.utils.encoding import smart_unicode
|
2008-06-18 21:10:05 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2011-10-18 08:47:49 +08:00
|
|
|
|
2007-03-31 17:05:54 +08:00
|
|
|
|
|
|
|
phone_digits_re = re.compile(r'^(\d{2})[-\.]?(\d{4})[-\.]?(\d{4})$')
|
|
|
|
|
|
|
|
class BRZipCodeField(RegexField):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _('Enter a zip code in the format XXXXX-XXX.'),
|
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
2007-03-31 17:05:54 +08:00
|
|
|
super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$',
|
2011-05-03 19:52:04 +08:00
|
|
|
max_length, min_length, *args, **kwargs)
|
2007-03-31 17:05:54 +08:00
|
|
|
|
|
|
|
class BRPhoneNumberField(Field):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _('Phone numbers must be in XX-XXXX-XXXX format.'),
|
|
|
|
}
|
|
|
|
|
2007-03-31 17:05:54 +08:00
|
|
|
def clean(self, value):
|
|
|
|
super(BRPhoneNumberField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2007-03-31 17:05:54 +08:00
|
|
|
value = re.sub('(\(|\)|\s+)', '', smart_unicode(value))
|
|
|
|
m = phone_digits_re.search(value)
|
|
|
|
if m:
|
2012-06-08 00:08:47 +08:00
|
|
|
return '%s-%s-%s' % (m.group(1), m.group(2), m.group(3))
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-03-31 17:05:54 +08:00
|
|
|
|
|
|
|
class BRStateSelect(Select):
|
|
|
|
"""
|
2007-04-26 23:05:47 +08:00
|
|
|
A Select widget that uses a list of Brazilian states/territories
|
2007-03-31 17:05:54 +08:00
|
|
|
as its choices.
|
|
|
|
"""
|
|
|
|
def __init__(self, attrs=None):
|
|
|
|
super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES)
|
2007-04-26 21:11:07 +08:00
|
|
|
|
2007-09-14 15:19:38 +08:00
|
|
|
class BRStateChoiceField(Field):
|
|
|
|
"""
|
|
|
|
A choice field that uses a list of Brazilian states as its choices.
|
|
|
|
"""
|
|
|
|
widget = Select
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
2012-06-08 00:08:47 +08:00
|
|
|
'invalid': _('Select a valid brazilian state. That state is not one of the available states.'),
|
2007-12-17 16:05:27 +08:00
|
|
|
}
|
2007-09-14 15:19:38 +08:00
|
|
|
|
|
|
|
def __init__(self, required=True, widget=None, label=None,
|
|
|
|
initial=None, help_text=None):
|
|
|
|
super(BRStateChoiceField, self).__init__(required, widget, label,
|
|
|
|
initial, help_text)
|
|
|
|
self.widget.choices = STATE_CHOICES
|
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
value = super(BRStateChoiceField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
value = ''
|
2007-09-14 15:19:38 +08:00
|
|
|
value = smart_unicode(value)
|
2012-06-08 00:08:47 +08:00
|
|
|
if value == '':
|
2007-09-14 15:19:38 +08:00
|
|
|
return value
|
|
|
|
valid_values = set([smart_unicode(k) for k, v in self.widget.choices])
|
|
|
|
if value not in valid_values:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-09-14 15:19:38 +08:00
|
|
|
return value
|
2007-04-26 21:11:07 +08:00
|
|
|
|
|
|
|
def DV_maker(v):
|
|
|
|
if v >= 2:
|
|
|
|
return 11 - v
|
|
|
|
return 0
|
|
|
|
|
|
|
|
class BRCPFField(CharField):
|
|
|
|
"""
|
|
|
|
This field validate a CPF number or a CPF string. A CPF number is
|
2007-04-26 23:05:47 +08:00
|
|
|
compounded by XXX.XXX.XXX-VD. The two last digits are check digits.
|
2007-04-26 21:11:07 +08:00
|
|
|
|
|
|
|
More information:
|
|
|
|
http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas
|
|
|
|
"""
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _("Invalid CPF number."),
|
|
|
|
'max_digits': _("This field requires at most 11 digits or 14 characters."),
|
|
|
|
'digits_only': _("This field requires only numbers."),
|
|
|
|
}
|
|
|
|
|
2011-05-03 19:52:04 +08:00
|
|
|
def __init__(self, max_length=14, min_length=11, *args, **kwargs):
|
|
|
|
super(BRCPFField, self).__init__(max_length, min_length, *args, **kwargs)
|
2007-04-26 21:11:07 +08:00
|
|
|
|
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Value can be either a string in the format XXX.XXX.XXX-XX or an
|
|
|
|
11-digit number.
|
|
|
|
"""
|
|
|
|
value = super(BRCPFField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2007-04-26 21:11:07 +08:00
|
|
|
orig_value = value[:]
|
|
|
|
if not value.isdigit():
|
|
|
|
value = re.sub("[-\.]", "", value)
|
|
|
|
try:
|
|
|
|
int(value)
|
|
|
|
except ValueError:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['digits_only'])
|
2007-04-26 21:11:07 +08:00
|
|
|
if len(value) != 11:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['max_digits'])
|
2007-04-26 21:11:07 +08:00
|
|
|
orig_dv = value[-2:]
|
|
|
|
|
|
|
|
new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(10, 1, -1))])
|
|
|
|
new_1dv = DV_maker(new_1dv % 11)
|
|
|
|
value = value[:-2] + str(new_1dv) + value[-1]
|
|
|
|
new_2dv = sum([i * int(value[idx]) for idx, i in enumerate(range(11, 1, -1))])
|
|
|
|
new_2dv = DV_maker(new_2dv % 11)
|
|
|
|
value = value[:-1] + str(new_2dv)
|
|
|
|
if value[-2:] != orig_dv:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-26 21:11:07 +08:00
|
|
|
|
|
|
|
return orig_value
|
|
|
|
|
|
|
|
class BRCNPJField(Field):
|
2007-12-17 16:05:27 +08:00
|
|
|
default_error_messages = {
|
|
|
|
'invalid': _("Invalid CNPJ number."),
|
|
|
|
'digits_only': _("This field requires only numbers."),
|
|
|
|
'max_digits': _("This field requires at least 14 digits"),
|
|
|
|
}
|
|
|
|
|
2007-04-26 21:11:07 +08:00
|
|
|
def clean(self, value):
|
|
|
|
"""
|
|
|
|
Value can be either a string in the format XX.XXX.XXX/XXXX-XX or a
|
|
|
|
group of 14 characters.
|
|
|
|
"""
|
|
|
|
value = super(BRCNPJField, self).clean(value)
|
|
|
|
if value in EMPTY_VALUES:
|
2012-06-08 00:08:47 +08:00
|
|
|
return ''
|
2007-04-26 21:11:07 +08:00
|
|
|
orig_value = value[:]
|
|
|
|
if not value.isdigit():
|
|
|
|
value = re.sub("[-/\.]", "", value)
|
|
|
|
try:
|
|
|
|
int(value)
|
|
|
|
except ValueError:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['digits_only'])
|
2007-04-26 21:11:07 +08:00
|
|
|
if len(value) != 14:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['max_digits'])
|
2007-04-26 21:11:07 +08:00
|
|
|
orig_dv = value[-2:]
|
|
|
|
|
|
|
|
new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(5, 1, -1) + range(9, 1, -1))])
|
|
|
|
new_1dv = DV_maker(new_1dv % 11)
|
|
|
|
value = value[:-2] + str(new_1dv) + value[-1]
|
|
|
|
new_2dv = sum([i * int(value[idx]) for idx, i in enumerate(range(6, 1, -1) + range(9, 1, -1))])
|
|
|
|
new_2dv = DV_maker(new_2dv % 11)
|
|
|
|
value = value[:-1] + str(new_2dv)
|
|
|
|
if value[-2:] != orig_dv:
|
2007-12-17 16:05:27 +08:00
|
|
|
raise ValidationError(self.error_messages['invalid'])
|
2007-04-26 21:11:07 +08:00
|
|
|
|
|
|
|
return orig_value
|