Ticket #8798: ticket8798-r8849.diff
File ticket8798-r8849.diff, 5.0 KB (added by , 16 years ago) |
---|
-
django/contrib/localflavor/de/forms.py
diff --git a/django/contrib/localflavor/de/forms.py b/django/contrib/localflavor/de/forms.py index 7a1b7c0..cdd64f5 100644
a b DE-specific Form helpers 4 4 5 5 from django.forms import ValidationError 6 6 from django.forms.fields import Field, RegexField, Select, EMPTY_VALUES 7 from django.utils.encoding import smart_unicode 7 8 from django.utils.translation import ugettext_lazy as _ 8 9 import re 9 10 10 11 id_re = re.compile(r"^(?P<residence>\d{10})(?P<origin>\w{1,3})[-\ ]?(?P<birthday>\d{7})[-\ ]?(?P<validity>\d{7})[-\ ]?(?P<checksum>\d{1})$") 12 phone_digits_re= re.compile(r"^(((\+\d{2,3})?\s\(([^0]{1}\d{1,6}\)|0\)\d{1,5})|\(\d{1,2}(\s?\d{1,2}){1,2}\))\s(\d{1,2}(\s?\d{1,2}){1,2})((-(\d{1,4}))?))|(\+\d{2,3}\s?\d{1,5}|0\d{1,6})\s?(\d\s)?\d+(-?\d{1,5})?$") 11 13 12 14 class DEZipCodeField(RegexField): 13 15 default_error_messages = { … … class DEZipCodeField(RegexField): 17 19 super(DEZipCodeField, self).__init__(r'^\d{5}$', 18 20 max_length=None, min_length=None, *args, **kwargs) 19 21 22 class DEPhoneNumberField(RegexField): 23 """ 24 A form field that validates telephone numbers following several standards 25 used in Austria and Germany (national and international): 26 27 * DIN 5008 (new) 28 * DIN 5008 (old) 29 * E.123 30 * Informal convention 31 """ 32 default_error_messages = { 33 'invalid': _('Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.'), 34 } 35 def __init__(self, *args, **kwargs): 36 super(DEPhoneNumberField, self).__init__(phone_digits_re, *args, **kwargs) 37 38 def clean(self, value): 39 value = super(DEPhoneNumberField, self).clean(value) 40 if value in EMPTY_VALUES: 41 return u'' 42 return value 43 20 44 class DEStateSelect(Select): 21 45 """ 22 46 A Select widget that uses a list of DE states as its choices. -
docs/ref/contrib/localflavor.txt
diff --git a/docs/ref/contrib/localflavor.txt b/docs/ref/contrib/localflavor.txt index 014ef71..9d50d31 100644
a b Germany (``de``) 286 286 287 287 A ``Select`` widget that uses a list of German states as its choices. 288 288 289 .. class:: de.forms.DEPhoneNumberField 290 291 A form field that validates input as a telephone number following 292 `several standards`_ used in Austria and Germany (national and 293 international notation): 294 295 * DIN 5008 (new) 296 * DIN 5008 (old) 297 * E.123 298 * Informal 299 300 .. _several standards: http://de.wikipedia.org/wiki/Rufnummer#Schreibweisen 301 289 302 Holland (``nl``) 290 303 ================ 291 304 -
tests/regressiontests/forms/localflavor/de.py
diff --git a/tests/regressiontests/forms/localflavor/de.py b/tests/regressiontests/forms/localflavor/de.py index e02323f..3d290df 100644
a b Traceback (most recent call last): 13 13 ... 14 14 ValidationError: [u'Enter a zip code in the format XXXXX.'] 15 15 16 # DEPhoneDIN5008Field ############################################################# 17 18 >>> from django.contrib.localflavor.de.forms import DEPhoneNumberField 19 >>> f = DEPhoneNumberField() 20 >>> f.clean('030 12345-67') # new DIN 5008 21 u'030 12345-67' 22 >>> f.clean('030 1234567') # new DIN 5008 23 u'030 1234567' 24 >>> f.clean('0301234567') # lazy DIN 5008 25 u'0301234567' 26 >>> f.clean('(0 30) 1 23 45-67') # old DIN 5008 27 u'(0 30) 1 23 45-67' 28 >>> f.clean('(0 30) 1 23 456') # old DIN 5008 29 u'(0 30) 1 23 456' 30 >>> f.clean('0900 5 123456') # DIN 5008 Premium rate numbers 31 u'0900 5 123456' 32 >>> f.clean('(030) 123 45 67') # E.123 33 u'(030) 123 45 67' 34 >>> f.clean('+49 (30) 1234567') # Microsoft/TAPI 35 u'+49 (30) 1234567' 36 >>> f.clean('+49 (0)30 12345-67') # Informal standard in Germany and Austria 37 u'+49 (0)30 12345-67' 38 >>> f.clean('+49 30 12345-67') # DIN 5008 39 u'+49 30 12345-67' 40 >>> f.clean('+49 30 1234567') # E.123 41 u'+49 30 1234567' 42 >>> f.clean('+49 30 12345-67') # E.123 43 u'+49 30 12345-67' 44 >>> f.clean('+49301234567') # Lazy E.123 45 u'+49301234567' 46 >>> f.clean('+49 30 12345--67') 47 Traceback (most recent call last): 48 ... 49 ValidationError: [u'Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.'] 50 >>> f.clean('+49 (030) 1234567') 51 Traceback (most recent call last): 52 ... 53 ValidationError: [u'Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.'] 54 >>> f.clean('(030) (123) 45 67') 55 Traceback (most recent call last): 56 ... 57 ValidationError: [u'Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.'] 58 >>> f.clean('abcdefg') 59 Traceback (most recent call last): 60 ValidationError: [u'Enter the phone number in a supported format: DIN 5008, E.123, Microsoft/TAPI.'] 61 >>> f.clean(None) 62 Traceback (most recent call last): 63 ... 64 ValidationError: [u'This field is required.'] 65 >>> f = DEPhoneNumberField(required=False) 66 >>> f.clean(None) 67 u'' 68 >>> f.clean('') 69 u'' 70 71 16 72 # DEStateSelect ############################################################# 17 73 18 74 >>> from django.contrib.localflavor.de.forms import DEStateSelect