Ticket #8735: django_localflavor_pt.diff
File django_localflavor_pt.diff, 6.8 KB (added by , 16 years ago) |
---|
-
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 """ 2 PT-specific Form helpers 3 """ 4 5 from django.forms import ValidationError 6 from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES 7 from django.utils.encoding import smart_unicode 8 from django.utils.translation import ugettext_lazy as _ 9 import re 10 11 phone_digits_re = re.compile(r'^(\d{9}|(00|\+)\d*)$') 12 13 14 class 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 30 class 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 4 tests = r""" 5 # PTZipCodeField ############################################################# 6 7 PTZipCodeField 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') 11 u'3030-034' 12 >>> f.clean('1003456') 13 u'1003-456' 14 >>> f.clean('2A200') 15 Traceback (most recent call last): 16 ... 17 ValidationError: [u'Enter a zip code in the format XXXX-XXX.'] 18 >>> f.clean('980001') 19 Traceback (most recent call last): 20 ... 21 ValidationError: [u'Enter a zip code in the format XXXX-XXX.'] 22 >>> f.clean(None) 23 Traceback (most recent call last): 24 ... 25 ValidationError: [u'This field is required.'] 26 >>> f.clean('') 27 Traceback (most recent call last): 28 ... 29 ValidationError: [u'This field is required.'] 30 31 >>> f = PTZipCodeField(required=False) 32 >>> f.clean('3030-034') 33 u'3030-034' 34 >>> f.clean('1003456') 35 u'1003-456' 36 >>> f.clean('2A200') 37 Traceback (most recent call last): 38 ... 39 ValidationError: [u'Enter a zip code in the format XXXX-XXX.'] 40 >>> f.clean('980001') 41 Traceback (most recent call last): 42 ... 43 ValidationError: [u'Enter a zip code in the format XXXX-XXX.'] 44 >>> f.clean(None) 45 u'' 46 >>> f.clean('') 47 u'' 48 49 # PTPhoneNumberField ########################################################## 50 51 PTPhoneNumberField validates that the data is a valid Portuguese phone number. 52 It'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') 56 u'917845189' 57 >>> f.clean('91 784 5189') 58 u'917845189' 59 >>> f.clean('91 784 5189') 60 u'917845189' 61 >>> f.clean('+351 91 111') 62 u'+35191111' 63 >>> f.clean('00351873') 64 u'00351873' 65 >>> f.clean('91 784 51 8') 66 Traceback (most recent call last): 67 ... 68 ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.'] 69 >>> f.clean('091 456 987 1') 70 Traceback (most recent call last): 71 ... 72 ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.'] 73 >>> f.clean(None) 74 Traceback (most recent call last): 75 ... 76 ValidationError: [u'This field is required.'] 77 >>> f.clean('') 78 Traceback (most recent call last): 79 ... 80 ValidationError: [u'This field is required.'] 81 82 >>> f = PTPhoneNumberField(required=False) 83 >>> f.clean('917845189') 84 u'917845189' 85 >>> f.clean('91 784 5189') 86 u'917845189' 87 >>> f.clean('91 784 5189') 88 u'917845189' 89 >>> f.clean('+351 91 111') 90 u'+35191111' 91 >>> f.clean('00351873') 92 u'00351873' 93 >>> f.clean('91 784 51 8') 94 Traceback (most recent call last): 95 ... 96 ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.'] 97 >>> f.clean('091 456 987 1') 98 Traceback (most recent call last): 99 ... 100 ValidationError: [u'Phone numbers must have 9 digits, or start by + or 00.'] 101 >>> f.clean(None) 102 u'' 103 >>> f.clean('') 104 u'' 105 106 """ -
tests/regressiontests/forms/tests.py
20 20 from localflavor.jp import tests as localflavor_jp_tests 21 21 from localflavor.nl import tests as localflavor_nl_tests 22 22 from localflavor.pl import tests as localflavor_pl_tests 23 from localflavor.pt import tests as localflavor_pt_tests 23 24 from localflavor.ro import tests as localflavor_ro_tests 24 25 from localflavor.sk import tests as localflavor_sk_tests 25 26 from localflavor.uk import tests as localflavor_uk_tests … … 53 54 'localflavor_jp_tests': localflavor_jp_tests, 54 55 'localflavor_nl_tests': localflavor_nl_tests, 55 56 'localflavor_pl_tests': localflavor_pl_tests, 57 'localflavor_pt_tests': localflavor_pt_tests, 56 58 'localflavor_ro_tests': localflavor_ro_tests, 57 59 'localflavor_sk_tests': localflavor_sk_tests, 58 60 'localflavor_uk_tests': localflavor_uk_tests,