From 5188a18dbed4182a602b540e7ce47eb781b0c704 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Fri, 14 Sep 2007 07:19:38 +0000 Subject: [PATCH] Fixed #4964 -- Added Brazilian state field to localflavors. Thanks, William Alves de Souza. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6176 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/br/forms.py | 42 +++++++++++++++++----- tests/regressiontests/forms/localflavor.py | 14 ++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/django/contrib/localflavor/br/forms.py b/django/contrib/localflavor/br/forms.py index c7082c0f9e..0f3e8e947e 100644 --- a/django/contrib/localflavor/br/forms.py +++ b/django/contrib/localflavor/br/forms.py @@ -6,7 +6,7 @@ BR-specific Form helpers from django.newforms import ValidationError from django.newforms.fields import Field, RegexField, CharField, Select, EMPTY_VALUES from django.utils.encoding import smart_unicode -from django.utils.translation import ugettext +from django.utils.translation import ugettext as _ import re phone_digits_re = re.compile(r'^(\d{2})[-\.]?(\d{4})[-\.]?(\d{4})$') @@ -15,7 +15,7 @@ class BRZipCodeField(RegexField): def __init__(self, *args, **kwargs): super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$', max_length=None, min_length=None, - error_message=ugettext('Enter a zip code in the format XXXXX-XXX.'), + error_message=_('Enter a zip code in the format XXXXX-XXX.'), *args, **kwargs) class BRPhoneNumberField(Field): @@ -27,7 +27,7 @@ class BRPhoneNumberField(Field): m = phone_digits_re.search(value) if m: return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) - raise ValidationError(ugettext('Phone numbers must be in XX-XXXX-XXXX format.')) + raise ValidationError(_('Phone numbers must be in XX-XXXX-XXXX format.')) class BRStateSelect(Select): """ @@ -38,6 +38,32 @@ class BRStateSelect(Select): from br_states import STATE_CHOICES super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES) +class BRStateChoiceField(Field): + """ + A choice field that uses a list of Brazilian states as its choices. + """ + widget = Select + + def __init__(self, required=True, widget=None, label=None, + initial=None, help_text=None): + super(BRStateChoiceField, self).__init__(required, widget, label, + initial, help_text) + from br_states import STATE_CHOICES + self.widget.choices = STATE_CHOICES + + def clean(self, value): + value = super(BRStateChoiceField, self).clean(value) + if value in EMPTY_VALUES: + value = u'' + value = smart_unicode(value) + if value == u'': + return value + valid_values = set([smart_unicode(k) for k, v in self.widget.choices]) + if value not in valid_values: + raise ValidationError(_(u'Select a valid brazilian state.' + u' That state is not one' + u' of the available states.')) + return value def DV_maker(v): if v >= 2: @@ -69,9 +95,9 @@ class BRCPFField(CharField): try: int(value) except ValueError: - raise ValidationError(ugettext("This field requires only numbers.")) + raise ValidationError(_("This field requires only numbers.")) if len(value) != 11: - raise ValidationError(ugettext("This field requires at most 11 digits or 14 characters.")) + raise ValidationError(_("This field requires at most 11 digits or 14 characters.")) orig_dv = value[-2:] new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(10, 1, -1))]) @@ -81,7 +107,7 @@ class BRCPFField(CharField): new_2dv = DV_maker(new_2dv % 11) value = value[:-1] + str(new_2dv) if value[-2:] != orig_dv: - raise ValidationError(ugettext("Invalid CPF number.")) + raise ValidationError(_("Invalid CPF number.")) return orig_value @@ -103,7 +129,7 @@ class BRCNPJField(Field): raise ValidationError("This field requires only numbers.") if len(value) != 14: raise ValidationError( - ugettext("This field requires at least 14 digits")) + _("This field requires at least 14 digits")) orig_dv = value[-2:] new_1dv = sum([i * int(value[idx]) for idx, i in enumerate(range(5, 1, -1) + range(9, 1, -1))]) @@ -113,7 +139,7 @@ class BRCNPJField(Field): new_2dv = DV_maker(new_2dv % 11) value = value[:-1] + str(new_2dv) if value[-2:] != orig_dv: - raise ValidationError(ugettext("Invalid CNPJ number.")) + raise ValidationError(_("Invalid CNPJ number.")) return orig_value diff --git a/tests/regressiontests/forms/localflavor.py b/tests/regressiontests/forms/localflavor.py index 01c6df8e76..1d46adfc0b 100644 --- a/tests/regressiontests/forms/localflavor.py +++ b/tests/regressiontests/forms/localflavor.py @@ -980,6 +980,20 @@ u'41-3562-3464' >>> w.render('states', 'PR') u'' +# BRStateChoiceField ######################################################### +>>> from django.contrib.localflavor.br.forms import BRStateChoiceField +>>> f = BRStateChoiceField() +>>> ', '.join([f.clean(s) for s, _ in f.widget.choices]) +u'AC, AL, AP, AM, BA, CE, DF, ES, GO, MA, MT, MS, MG, PA, PB, PR, PE, PI, RJ, RN, RS, RO, RR, SC, SP, SE, TO' +>>> f.clean('') +Traceback (most recent call last): +... +ValidationError: [u'This field is required.'] +>>> f.clean('pr') +Traceback (most recent call last): +... +ValidationError: [u'Select a valid brazilian state. That state is not one of the available states.'] + # DEZipCodeField ############################################################## >>> from django.contrib.localflavor.de.forms import DEZipCodeField