Ticket #5670: ukpostcodefield.diff
File ukpostcodefield.diff, 3.7 KB (added by , 17 years ago) |
---|
-
django/contrib/localflavor/uk/forms.py
1 1 """ 2 2 UK-specific Form helpers 3 3 """ 4 import re 4 5 5 from django.newforms.fields import RegexField 6 from django.newforms.fields import CharField 7 from django.newforms import ValidationError 6 8 from django.utils.translation import ugettext 7 9 8 class UKPostcodeField( RegexField):10 class UKPostcodeField(CharField): 9 11 """ 10 12 A form field that validates its input is a UK postcode. 13 14 Uppercases the value and adds space in correct place if required. 11 15 12 16 The regular expression used is sourced from the schema for British Standard 13 17 BS7666 address types: http://www.govtalk.gov.uk/gdsc/schemas/bs7666-v2-0.xsd 14 18 """ 15 def __init__(self, *args, **kwargs): 16 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})$', 17 max_length=None, min_length=None, 18 error_message=ugettext(u'Enter a postcode. A space is required between the two postcode parts.'), 19 *args, **kwargs) 19 OUTCODE_PATTERN = '[A-PR-UWYZ]([0-9]{1,2}|([A-HIK-Y][0-9](|[0-9]|[ABEHMNPRVWXY]))|[0-9][A-HJKSTUW])' 20 INCODE_PATTERN = '[0-9][ABD-HJLNP-UW-Z]{2}' 21 POSTCODE_REGEX = re.compile(r'^(GIR 0AA|%s %s)$' % (OUTCODE_PATTERN, INCODE_PATTERN)) 22 SPACE_REGEX = re.compile(r' *(%s)$' % INCODE_PATTERN) 23 24 def clean(self, value): 25 value = super(UKPostcodeField, self).clean(value) 26 if value == u'': 27 return value 28 postcode = value.upper().strip() 29 # Put a single space before the incode (second part). 30 postcode = self.SPACE_REGEX.sub(r' \1', postcode) 31 if not self.POSTCODE_REGEX.search(postcode): 32 raise ValidationError(ugettext(u'Enter a valid postcode.')) 33 return postcode -
tests/regressiontests/forms/localflavor/uk.py
12 12 >>> f.clean('GIR 0AA') 13 13 u'GIR 0AA' 14 14 >>> f.clean('BT324PX') 15 u'BT32 4PX' 16 >>> f.clean('1NV 4L1D') 15 17 Traceback (most recent call last): 16 18 ... 17 ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']18 >>> f.clean('1NV 19 ValidationError: [u'Enter a valid postcode.'] 20 >>> f.clean('1NV4L1D') 19 21 Traceback (most recent call last): 20 22 ... 21 ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']23 ValidationError: [u'Enter a valid postcode.'] 22 24 >>> f.clean(None) 23 25 Traceback (most recent call last): 24 26 ... … … 27 29 Traceback (most recent call last): 28 30 ... 29 31 ValidationError: [u'This field is required.'] 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 ValidationError: [u'Enter a valid postcode.'] 30 44 31 45 >>> f = UKPostcodeField(required=False) 32 46 >>> f.clean('BT32 4PX') … … 36 50 >>> f.clean('1NV 4L1D') 37 51 Traceback (most recent call last): 38 52 ... 39 ValidationError: [u'Enter a postcode. A space is required between the two postcode parts.']53 ValidationError: [u'Enter a valid postcode.'] 40 54 >>> 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.'] 55 u'BT32 4PX' 44 56 >>> f.clean(None) 45 57 u'' 46 58 >>> f.clean('')