Ticket #8527: ca-form-2.diff

File ca-form-2.diff, 2.7 KB (added by Claude Paroz, 14 years ago)

Similar patch for current trunk (+unittest)

  • django/contrib/localflavor/ca/forms.py

    diff --git a/django/contrib/localflavor/ca/forms.py b/django/contrib/localflavor/ca/forms.py
    index 788c9d0..ac393de 100644
    a b Canada-specific Form helpers  
    44
    55from django.core.validators import EMPTY_VALUES
    66from django.forms import ValidationError
    7 from django.forms.fields import Field, RegexField, Select
     7from django.forms.fields import Field, CharField, Select
    88from django.utils.encoding import smart_unicode
    99from django.utils.translation import ugettext_lazy as _
    1010import re
    import re  
    1212phone_digits_re = re.compile(r'^(?:1-?)?(\d{3})[-\.]?(\d{3})[-\.]?(\d{4})$')
    1313sin_re = re.compile(r"^(\d{3})-(\d{3})-(\d{3})$")
    1414
    15 class CAPostalCodeField(RegexField):
     15class CAPostalCodeField(CharField):
    1616    """
    1717    Canadian postal code field.
    1818
    class CAPostalCodeField(RegexField):  
    2525        'invalid': _(u'Enter a postal code in the format XXX XXX.'),
    2626    }
    2727
    28     def __init__(self, max_length=None, min_length=None, *args, **kwargs):
    29         super(CAPostalCodeField, self).__init__(r'^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ] \d[ABCEGHJKLMNPRSTVWXYZ]\d$',
    30             max_length, min_length, *args, **kwargs)
     28    postcode_regex = re.compile(r'^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]) *(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$')
     29
     30    def clean(self, value):
     31        value = super(CAPostalCodeField, self).clean(value)
     32        if value in EMPTY_VALUES:
     33            return u''
     34        postcode = value.upper().strip()
     35        m = self.postcode_regex.match(postcode)
     36        if not m:
     37            raise ValidationError(self.default_error_messages['invalid'])
     38        return "%s %s" % (m.group(1), m.group(2))
    3139
    3240class CAPhoneNumberField(Field):
    3341    """Canadian phone number field."""
  • tests/regressiontests/forms/localflavor/ca.py

    diff --git a/tests/regressiontests/forms/localflavor/ca.py b/tests/regressiontests/forms/localflavor/ca.py
    index bd31a5c..8a17ed2 100644
    a b class CALocalFlavorTests(LocalFlavorTestCase):  
    4545            'T2S 2W7': 'T2S 2W7',
    4646            'T2S 2Z7': 'T2S 2Z7',
    4747            'T2Z 2H7': 'T2Z 2H7',
    48 
     48            'T2S2H7' : 'T2S 2H7',
     49            't2s 2h7': 'T2S 2H7',
     50            't2s2h7' : 'T2S 2H7',
     51            't2s            2H7': 'T2S 2H7',
     52            '  t2s    2H7  ': 'T2S 2H7',
    4953        }
    5054        invalid = {
    51             'T2S2H7' : error_format,
    5255            'T2S 2H' : error_format,
    5356            '2T6 H8I': error_format,
    5457            'T2S2H' : error_format,
     58            't2s h8i': error_format,
    5559            90210 : error_format,
    5660            'W2S 2H3': error_format,
    5761            'Z2S 2H3': error_format,
Back to Top