Ticket #11350: 11350.diff
File 11350.diff, 5.2 KB (added by , 14 years ago) |
---|
-
tests/regressiontests/forms/localflavor/il.py
### Eclipse Workspace Patch 1.0 #P django-trunk
1 # -*- coding: utf-8 -*- 2 # Tests for the contrib/localflavor/ IL Form Fields 3 4 tests = r""" 5 # ILPostalCodeField ######################################################### 6 7 >>> from django.contrib.localflavor.il.forms import ILPostalCodeField 8 >>> f = ILPostalCodeField() 9 >>> f.clean('84545x') 10 Traceback (most recent call last): 11 ... 12 ValidationError: [u'Enter a postal code in the format XXXXX'] 13 >>> f.clean('69973') 14 u'69973' 15 >>> f.clean('699 73') 16 u'69973' 17 >>> f.clean('12345') 18 u'12345' 19 >>> f.clean('123456') 20 Traceback (most recent call last): 21 ... 22 ValidationError: [u'Enter a postal code in the format XXXXX'] 23 >>> f.clean('1234') 24 Traceback (most recent call last): 25 ... 26 ValidationError: [u'Enter a postal code in the format XXXXX'] 27 >>> f.clean('123 4') 28 Traceback (most recent call last): 29 ... 30 ValidationError: [u'Enter a postal code in the format XXXXX'] 31 32 # ILIDNumberField ######################################################## 33 34 >>> from django.contrib.localflavor.il.forms import ILIDNumberField 35 >>> f = ILIDNumberField() 36 >>> f.clean('39337423') 37 u'39337423' 38 >>> f.clean('039337423') 39 u'039337423' 40 >>> f.clean('0091') 41 u'0091' 42 >>> f.clean('123465789') 43 Traceback (most recent call last): 44 ... 45 ValidationError: [u'Enter a valid ID number.'] 46 >>> f.clean('012346578') 47 Traceback (most recent call last): 48 ... 49 ValidationError: [u'Enter a valid ID number.'] 50 >>> f.clean('0001') 51 Traceback (most recent call last): 52 ... 53 ValidationError: [u'Enter a valid ID number.'] 54 """ -
django/contrib/localflavor/il/forms.py
1 """ 2 Israeli-specific form helpers 3 """ 4 5 from django.forms import ValidationError 6 from django.forms.fields import RegexField, Field 7 from django.utils.translation import ugettext_lazy as _ 8 import re 9 10 id_number = re.compile(r'^(?P<number>\d{1,8})(?P<check>\d)$') 11 12 class ILPostalCodeField(RegexField): 13 """ 14 A form field that validates its input as an Israeli postal code. 15 Valid form is XXXXX where X represents integer. 16 """ 17 default_error_messages = { 18 'invalid': _(u'Enter a postal code in the format XXXXX'), 19 } 20 21 def __init__(self, *args, **kwargs): 22 super(ILPostalCodeField, self).__init__(r'^\d{5}$', 23 max_length=None, min_length=None, *args, **kwargs) 24 25 def clean(self, value): 26 """ 27 Validates the input and returns a string that contains only numbers. 28 Returns an empty string for empty values. 29 """ 30 return super(ILPostalCodeField, self).clean(value) 31 32 class ILIDNumberField(Field): 33 """ 34 A form field that validates its input as an Israeli identification number. 35 Valid form is per the Israeli ID specification. 36 """ 37 default_error_messages = { 38 'invalid': _(u'Enter a valid ID number.'), 39 } 40 41 def clean(self, value): 42 super(ILIDNumberField, self).__init__(value) 43 44 if value in EMPTY_VALUES: 45 return u'' 46 47 match = re.match(id_number, value) 48 if not match: 49 raise ValidationError(self.error_messages['invalid']) 50 51 number, check = match.groupdict()['number'].zfill(8), int(match.groupdict()['check']) 52 53 sum = 0 54 weight = 1 55 for digit in number+str(check): 56 sum += (lambda x: x/10 + x%10)(int(digit)*weight) 57 weight ^= 3 58 59 if sum%10 != 0: 60 raise ValidationError(self.error_messages['invalid']) -
docs/ref/contrib/localflavor.txt
52 52 * India_ 53 53 * Indonesia_ 54 54 * Ireland_ 55 * Israel_ 55 56 * Italy_ 56 57 * Japan_ 57 58 * Kuwait_ … … 99 100 .. _India: `India (in\_)`_ 100 101 .. _Indonesia: `Indonesia (id)`_ 101 102 .. _Ireland: `Ireland (ie)`_ 103 .. _Israel: `Israel (il)`_ 102 104 .. _Italy: `Italy (it)`_ 103 105 .. _Japan: `Japan (jp)`_ 104 106 .. _Kuwait: `Kuwait (kw)`_ … … 419 421 420 422 .. _NIK: http://en.wikipedia.org/wiki/Indonesian_identity_card 421 423 424 Israel (``il``) 425 ============== 426 427 .. class:: il.forms.ILPostalCodeField 428 429 A form field that validates its input as an Israeli postal code. 430 Valid form is XXXXX where X represents integer. 431 432 .. class:: il.forms.ILIDNumberField 433 434 A form field that validates its input as an Israeli identification number. 435 Valid form is per the Israeli ID specification. 436 422 437 Italy (``it``) 423 438 ============== 424 439