Changeset 6952
- Timestamp:
- 12/18/07 22:43:47 (9 months ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/localflavor/uk/forms.py
r6926 r6952 3 3 """ 4 4 5 from django.newforms.fields import RegexField, Select 5 import re 6 7 from django.newforms.fields import CharField, Select 8 from django.newforms import ValidationError 6 9 from django.utils.translation import ugettext 7 10 8 class UKPostcodeField( RegexField):11 class UKPostcodeField(CharField): 9 12 """ 10 13 A form field that validates its input is a UK postcode. … … 12 15 The regular expression used is sourced from the schema for British Standard 13 16 BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd 17 18 The value is uppercased and a space added in the correct place, if required. 14 19 """ 15 20 default_error_messages = { 16 'invalid': ugettext(u'Enter a postcode. A space is required between the two postcode parts.'),21 'invalid': ugettext(u'Enter a valid postcode.'), 17 22 } 23 outcode_pattern = '[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW])' 24 incode_pattern = '[0-9][ABD-HJLNP-UW-Z]{2}' 25 postcode_regex = re.compile(r'^(GIR 0AA|%s %s)$' % (outcode_pattern, incode_pattern)) 26 space_regex = re.compile(r' *(%s)$' % incode_pattern) 18 27 19 def __init__(self, *args, **kwargs): 20 super(UKPostcodeField, self).__init__(r'^(GIR 0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW]) [0-9][ABD-HJLNP-UW-Z]{2})$', 21 max_length=None, min_length=None, *args, **kwargs) 28 def clean(self, value): 29 value = super(UKPostcodeField, self).clean(value) 30 if value == u'': 31 return value 32 postcode = value.upper().strip() 33 # Put a single space before the incode (second part). 34 postcode = self.space_regex.sub(r' \1', postcode) 35 if not self.postcode_regex.search(postcode): 36 raise ValidationError(self.default_error_messages['invalid']) 37 return postcode 22 38 23 39 class UKCountySelect(Select): django/trunk/tests/regressiontests/forms/localflavor/uk.py
r6379 r6952 13 13 u'GIR 0AA' 14 14 >>> f.clean('BT324PX') 15 Traceback (most recent call last): 16 ... 17 ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.'] 15 u'BT32 4PX' 18 16 >>> f.clean('1NV 4L1D') 19 17 Traceback (most recent call last): 20 18 ... 21 ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.'] 19 ValidationError: [u'Enter a valid postcode.'] 20 >>> f.clean('1NV4L1D') 21 Traceback (most recent call last): 22 ... 23 ValidationError: [u'Enter a valid postcode.'] 22 24 >>> f.clean(None) 23 25 Traceback (most recent call last): … … 28 30 ... 29 31 ValidationError: [u'This field is required.'] 30 32 >>> f.clean(' so11aa ') 33 u'SO1 1AA' 34 >>> f.clean(' so1 1aa ') 35 u'SO1 1AA' 36 >>> f.clean('G2 3wt') 37 u'G2 3WT' 38 >>> f.clean('EC1A 1BB') 39 u'EC1A 1BB' 40 >>> f.clean('Ec1a1BB') 41 u'EC1A 1BB' 42 >>> f.clean(' b0gUS') 43 Traceback (most recent call last): 44 ... 45 ValidationError: [u'Enter a valid postcode.'] 31 46 >>> f = UKPostcodeField(required=False) 32 47 >>> f.clean('BT32 4PX') … … 37 52 Traceback (most recent call last): 38 53 ... 39 ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']54 ValidationError: [u'Enter a valid postcode.'] 40 55 >>> f.clean('BT324PX') 41 Traceback (most recent call last): 42 ... 43 ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.'] 56 u'BT32 4PX' 44 57 >>> f.clean(None) 45 58 u''
