Ticket #8735: django_localflavor_pt.diff

File django_localflavor_pt.diff, 6.8 KB (added by alcides, 16 years ago)

Patch to add Portuguese localflavor, along with tests, based on the French localflavor

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

    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: django/contrib/localflavor/pt/forms.pyc
    ___________________________________________________________________
    Name: svn:mime-type
       + application/octet-stream
    
    Cannot display: file marked as a binary type.
    svn:mime-type = application/octet-stream
    
    Property changes on: django/contrib/localflavor/pt/__init__.pyc
    ___________________________________________________________________
    Name: svn:mime-type
       + application/octet-stream
    
     
     1"""
     2PT-specific Form helpers
     3"""
     4
     5from django.forms import ValidationError
     6from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES
     7from django.utils.encoding import smart_unicode
     8from django.utils.translation import ugettext_lazy as _
     9import re
     10
     11phone_digits_re = re.compile(r'^(\d{9}|(00|\+)\d*)$')
     12
     13
     14class PTZipCodeField(RegexField):
     15    default_error_messages = {
     16        'invalid': _('Enter a zip code in the format XXXX-XXX.'),
     17    }
     18
     19    def __init__(self, *args, **kwargs):
     20        super(PTZipCodeField, self).__init__(r'^(\d{4}-\d{3}|\d{7})$',
     21            max_length=None, min_length=None, *args, **kwargs)
     22
     23    def clean(self,value):
     24        cleaned = super(PTZipCodeField, self).clean(value)
     25        if len(cleaned) == 7:
     26               return u'%s-%s' % (cleaned[:4],cleaned[4:])
     27        else:
     28               return cleaned
     29               
     30class PTPhoneNumberField(Field):
     31    """
     32    Validate local Portuguese phone number (including international ones)
     33        It should have 9 digits (may include spaces) or start by 00 or + (international)
     34    """
     35    default_error_messages = {
     36        'invalid': u'Phone numbers must have 9 digits, or start by + or 00.',
     37    }
     38
     39    def clean(self, value):
     40        super(PTPhoneNumberField, self).clean(value)
     41        if value in EMPTY_VALUES:
     42            return u''
     43        value = re.sub('(\.|\s)', '', smart_unicode(value))
     44        m = phone_digits_re.search(value)
     45        if m:
     46            return u'%s' % value
     47        raise ValidationError(self.error_messages['invalid'])
     48
  • tests/regressiontests/forms/localflavor/pt.py

     
     1# -*- coding: utf-8 -*-
     2# Tests for the contrib/localflavor/ PT form fields.
     3
     4tests = r"""
     5# PTZipCodeField #############################################################
     6
     7PTZipCodeField validates that the data is a valid PT zipcode.
     8>>> from django.contrib.localflavor.pt.forms import PTZipCodeField
     9>>> f = PTZipCodeField()
     10>>> f.clean('3030-034')
     11u'3030-034'
     12>>> f.clean('1003456')
     13u'1003-456'
     14>>> f.clean('2A200')
     15Traceback (most recent call last):
     16...
     17ValidationError: [u'Enter a zip code in the format XXXX-XXX.']
     18>>> f.clean('980001')
     19Traceback (most recent call last):
     20...
     21ValidationError: [u'Enter a zip code in the format XXXX-XXX.']
     22>>> f.clean(None)
     23Traceback (most recent call last):
     24...
     25ValidationError: [u'This field is required.']
     26>>> f.clean('')
     27Traceback (most recent call last):
     28...
     29ValidationError: [u'This field is required.']
     30
     31>>> f = PTZipCodeField(required=False)
     32>>> f.clean('3030-034')
     33u'3030-034'
     34>>> f.clean('1003456')
     35u'1003-456'
     36>>> f.clean('2A200')
     37Traceback (most recent call last):
     38...
     39ValidationError: [u'Enter a zip code in the format XXXX-XXX.']
     40>>> f.clean('980001')
     41Traceback (most recent call last):
     42...
     43ValidationError: [u'Enter a zip code in the format XXXX-XXX.']
     44>>> f.clean(None)
     45u''
     46>>> f.clean('')
     47u''
     48
     49# PTPhoneNumberField ##########################################################
     50
     51PTPhoneNumberField validates that the data is a valid Portuguese phone number.
     52It's normalized to XXXXXXXXX format  or +X(X) for international numbers. Dots are valid too.
     53>>> from django.contrib.localflavor.pt.forms import PTPhoneNumberField
     54>>> f = PTPhoneNumberField()
     55>>> f.clean('917845189')
     56u'917845189'
     57>>> f.clean('91 784 5189')
     58u'917845189'
     59>>> f.clean('91 784 5189')
     60u'917845189'
     61>>> f.clean('+351 91 111')
     62u'+35191111'
     63>>> f.clean('00351873')
     64u'00351873'
     65>>> f.clean('91 784 51 8')
     66Traceback (most recent call last):
     67...
     68ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.']
     69>>> f.clean('091 456 987 1')
     70Traceback (most recent call last):
     71...
     72ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.']
     73>>> f.clean(None)
     74Traceback (most recent call last):
     75...
     76ValidationError: [u'This field is required.']
     77>>> f.clean('')
     78Traceback (most recent call last):
     79...
     80ValidationError: [u'This field is required.']
     81
     82>>> f = PTPhoneNumberField(required=False)
     83>>> f.clean('917845189')
     84u'917845189'
     85>>> f.clean('91 784 5189')
     86u'917845189'
     87>>> f.clean('91 784 5189')
     88u'917845189'
     89>>> f.clean('+351 91 111')
     90u'+35191111'
     91>>> f.clean('00351873')
     92u'00351873'
     93>>> f.clean('91 784 51 8')
     94Traceback (most recent call last):
     95...
     96ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.']
     97>>> f.clean('091 456 987 1')
     98Traceback (most recent call last):
     99...
     100ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.']
     101>>> f.clean(None)
     102u''
     103>>> f.clean('')
     104u''
     105
     106"""
  • tests/regressiontests/forms/tests.py

     
    2020from localflavor.jp import tests as localflavor_jp_tests
    2121from localflavor.nl import tests as localflavor_nl_tests
    2222from localflavor.pl import tests as localflavor_pl_tests
     23from localflavor.pt import tests as localflavor_pt_tests
    2324from localflavor.ro import tests as localflavor_ro_tests
    2425from localflavor.sk import tests as localflavor_sk_tests
    2526from localflavor.uk import tests as localflavor_uk_tests
     
    5354    'localflavor_jp_tests': localflavor_jp_tests,
    5455    'localflavor_nl_tests': localflavor_nl_tests,
    5556    'localflavor_pl_tests': localflavor_pl_tests,
     57    'localflavor_pt_tests': localflavor_pt_tests,
    5658    'localflavor_ro_tests': localflavor_ro_tests,
    5759    'localflavor_sk_tests': localflavor_sk_tests,
    5860    'localflavor_uk_tests': localflavor_uk_tests,
Back to Top