Ticket #9066: cz_localflavour.patch
File cz_localflavour.patch, 12.8 KB (added by , 16 years ago) |
---|
-
django/contrib/localflavor/cz/cz_regions.py
1 """ 2 Czech regions, translations get from http://www.crwflags.com/fotw/Flags/cz-re.html 3 """ 4 5 from django.utils.translation import ugettext_lazy as _ 6 7 REGION_CHOICES = ( 8 ('PR', _('Prague')), 9 ('CE', _('Cenral Bohemian Region')), 10 ('SO', _('South Bohemian Region')), 11 ('PI', _('Pilsen Region')), 12 ('CA', _('Carlsbad Region')), 13 ('US', _('Usti Region')), 14 ('LB', _('Liberec Region')), 15 ('HK', _('Hradec Region')), 16 ('PA', _('Pardubice Region')), 17 ('VY', _('Vysocina Region')), 18 ('SM', _('South Moravian Region')), 19 ('OL', _('Olomouc Region')), 20 ('ZL', _('Zlin Region')), 21 ('MS', _('Moravian-Silesian Region')), 22 ) -
django/contrib/localflavor/cz/forms.py
1 """ 2 Czech-specific form helpers 3 """ 4 5 from django.forms import ValidationError 6 from django.forms.fields import Select, RegexField, Field, EMPTY_VALUES 7 from django.utils.translation import ugettext_lazy as _ 8 import re 9 10 birth_number = re.compile(r'^(?P<birth>\d{6})/?(?P<id>\d{3,4})$') 11 ic_number = re.compile(r'^(?P<number>\d{7})(?P<check>\d)$') 12 13 class CZRegionSelect(Select): 14 """ 15 A select widget widget with list of Czech regions as choices. 16 """ 17 def __init__(self, attrs=None): 18 from cz_regions import REGION_CHOICES 19 super(CZRegionSelect, self).__init__(attrs, choices=REGION_CHOICES) 20 21 class CZPostalCodeField(RegexField): 22 """ 23 A form field that validates its input as Czech postal code. 24 Valid form is XXXXX or XXX XX, where X represents integer. 25 """ 26 default_error_messages = { 27 'invalid': _(u'Enter a postal code in the format XXXXX or XXX XX.'), 28 } 29 30 def __init__(self, *args, **kwargs): 31 super(CZPostalCodeField, self).__init__(r'^\d{5}$|^\d{3} \d{2}$', 32 max_length=None, min_length=None, *args, **kwargs) 33 34 def clean(self, value): 35 """ 36 Validates the input and returns a string that contains only numbers. 37 Returns an empty string for empty values. 38 """ 39 v = super(CZPostalCodeField, self).clean(value) 40 return v.replace(' ', '') 41 42 class CZBirthNumberField(Field): 43 """ 44 Czech birth number field. 45 """ 46 default_error_messages = { 47 'invalid_format': _(u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.'), 48 'invalid_gender': _(u'Invalid optional parametr Gender, valid values are \'f\' and \'m\''), 49 'invalid': _(u'Enter a valid birth number.'), 50 } 51 52 def clean(self, value, gender=None): 53 super(CZBirthNumberField, self).__init__(value) 54 55 if value in EMPTY_VALUES: 56 return u'' 57 58 match = re.match(birth_number, value) 59 if not match: 60 raise ValidationError(self.error_messages['invalid_format']) 61 62 birth, id = match.groupdict()['birth'], match.groupdict()['id'] 63 64 # Three digits for verificatin number were used until 1. january 1954 65 if len(id) == 3: 66 return u'%s' % value 67 68 # Birth number is in format YYMMDD. Females have month value raised by 50. 69 # In case that all possible number are already used (for given date), 70 # the month field is raised by 20. 71 if gender is not None: 72 if gender == 'f': 73 female_const = 50 74 elif gender == 'm': 75 female_const = 0 76 else: 77 raise ValidationError(self.error_messages['invalid_gender']) 78 79 month = int(birth[2:4]) - female_const 80 if (not 1 <= month <= 12): 81 if (not 1 <= (month - 20) <= 12): 82 raise ValidationError(self.error_messages['invalid']) 83 84 day = int(birth[4:6]) 85 if not (1 <= day <= 31): 86 raise ValidationError(self.error_messages['invalid']) 87 88 # Fourth digit has been added since 1. January 1954. 89 # It is modulo of dividing birth number and verification number by 11. 90 # If the modulo were 10, the last number was 0 (and therefore, the whole 91 # birth number weren't dividable by 11. These number are no longer used (since 1985) 92 # and condition 'modulo == 10' can be removed in 2085. 93 94 modulo = int(birth + id[:3]) % 11 95 96 if (modulo == int(id[-1])) or (modulo == 10 and id[-1] == '0'): 97 return u'%s' % value 98 else: 99 raise ValidationError(self.error_messages['invalid']) 100 101 class CZICNumberField(Field): 102 """ 103 Czech IC number field. 104 """ 105 default_error_messages = { 106 'invalid': _(u'Enter a valid IC number.'), 107 } 108 109 def clean(self, value): 110 super(CZICNumberField, self).__init__(value) 111 112 if value in EMPTY_VALUES: 113 return u'' 114 115 match = re.match(ic_number, value) 116 if not match: 117 raise ValidationError(self.error_messages['invalid']) 118 119 number, check = match.groupdict()['number'], int(match.groupdict()['check']) 120 121 sum = 0 122 weight = 8 123 for digit in number: 124 sum += int(digit)*weight 125 weight -= 1 126 127 remainder = sum % 11 128 129 # remainder is equal: 130 # 0 or 10: last digit is 1 131 # 1: last digit is 0 132 # in other case, last digin is 11 - remainder 133 134 if (not remainder % 10 and check == 1) or \ 135 (remainder == 1 and check == 0) or \ 136 (check == (11 - remainder)): 137 return u'%s' % value 138 139 raise ValidationError(self.error_messages['invalid']) 140 -
tests/regressiontests/forms/localflavor/cz.py
1 # -*- coding: utf-8 -*- 2 # Tests for the contrib/localflavor/ CZ Form Fields 3 4 tests = r""" 5 # CZPostalCodeField ######################################################### 6 7 >>> from django.contrib.localflavor.cz.forms import CZPostalCodeField 8 >>> f = CZPostalCodeField() 9 >>> f.clean('84545x') 10 Traceback (most recent call last): 11 ... 12 ValidationError: [u'Enter a postal code in the format XXXXX or XXX XX.'] 13 >>> f.clean('91909') 14 u'91909' 15 >>> f.clean('917 01') 16 u'91701' 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 or XXX XX.'] 23 >>> f.clean('1234') 24 Traceback (most recent call last): 25 ... 26 ValidationError: [u'Enter a postal code in the format XXXXX or XXX XX.'] 27 >>> f.clean('123 4') 28 Traceback (most recent call last): 29 ... 30 ValidationError: [u'Enter a postal code in the format XXXXX or XXX XX.'] 31 32 # CZRegionSelect ############################################################ 33 34 >>> from django.contrib.localflavor.cz.forms import CZRegionSelect 35 >>> w = CZRegionSelect() 36 >>> w.render('regions', 'TT') 37 u'<select name="regions">\n<option value="PR">Prague</option>\n<option value="CE">Cenral Bohemian Region</option>\n<option value="SO">South Bohemian Region</option>\n<option value="PI">Pilsen Region</option>\n<option value="CA">Carlsbad Region</option>\n<option value="US">Usti Region</option>\n<option value="LB">Liberec Region</option>\n<option value="HK">Hradec Region</option>\n<option value="PA">Pardubice Region</option>\n<option value="VY">Vysocina Region</option>\n<option value="SM">South Moravian Region</option>\n<option value="OL">Olomouc Region</option>\n<option value="ZL">Zlin Region</option>\n<option value="MS">Moravian-Silesian Region</option>\n</select>' 38 39 # CZBirthNumberField ######################################################## 40 41 >>> from django.contrib.localflavor.cz.forms import CZBirthNumberField 42 >>> f = CZBirthNumberField() 43 >>> f.clean('880523/1237') 44 u'880523/1237' 45 >>> f.clean('8805231237') 46 u'8805231237' 47 >>> f.clean('880523/000') 48 u'880523/000' 49 >>> f.clean('880523000') 50 u'880523000' 51 >>> f.clean('882101/0011') 52 u'882101/0011' 53 >>> f.clean('880523/1237', 'm') 54 u'880523/1237' 55 >>> f.clean('885523/1231', 'f') 56 u'885523/1231' 57 >>> f.clean('123456/12') 58 Traceback (most recent call last): 59 ... 60 ValidationError: [u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.'] 61 >>> f.clean('123456/12345') 62 Traceback (most recent call last): 63 ... 64 ValidationError: [u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.'] 65 >>> f.clean('12345612') 66 Traceback (most recent call last): 67 ... 68 ValidationError: [u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.'] 69 >>> f.clean('12345612345') 70 Traceback (most recent call last): 71 ... 72 ValidationError: [u'Enter a birth number in the format XXXXXX/XXXX or XXXXXXXXXX.'] 73 >>> f.clean('881523/0000', 'm') 74 Traceback (most recent call last): 75 ... 76 ValidationError: [u'Enter a valid birth number.'] 77 >>> f.clean('885223/0000', 'm') 78 Traceback (most recent call last): 79 ... 80 ValidationError: [u'Enter a valid birth number.'] 81 >>> f.clean('881223/0000', 'f') 82 Traceback (most recent call last): 83 ... 84 ValidationError: [u'Enter a valid birth number.'] 85 >>> f.clean('886523/0000', 'f') 86 Traceback (most recent call last): 87 ... 88 ValidationError: [u'Enter a valid birth number.'] 89 >>> f.clean('880523/1239') 90 Traceback (most recent call last): 91 ... 92 ValidationError: [u'Enter a valid birth number.'] 93 >>> f.clean('8805231239') 94 Traceback (most recent call last): 95 ... 96 ValidationError: [u'Enter a valid birth number.'] 97 >>> f.clean('990101/0011') 98 Traceback (most recent call last): 99 ... 100 ValidationError: [u'Enter a valid birth number.'] 101 102 # CZICNumberField ######################################################## 103 104 >>> from django.contrib.localflavor.cz.forms import CZICNumberField 105 >>> f = CZICNumberField() 106 >>> f.clean('12345679') 107 u'12345679' 108 >>> f.clean('12345601') 109 u'12345601' 110 >>> f.clean('12345661') 111 u'12345661' 112 >>> f.clean('12345610') 113 u'12345610' 114 >>> f.clean('1234567') 115 Traceback (most recent call last): 116 ... 117 ValidationError: [u'Enter a valid IC number.'] 118 >>> f.clean('12345660') 119 Traceback (most recent call last): 120 ... 121 ValidationError: [u'Enter a valid IC number.'] 122 >>> f.clean('12345600') 123 Traceback (most recent call last): 124 ... 125 ValidationError: [u'Enter a valid IC number.'] 126 """ -
tests/regressiontests/forms/tests.py
10 10 from localflavor.ca import tests as localflavor_ca_tests 11 11 from localflavor.ch import tests as localflavor_ch_tests 12 12 from localflavor.cl import tests as localflavor_cl_tests 13 from localflavor.cz import tests as localflavor_cz_tests 13 14 from localflavor.de import tests as localflavor_de_tests 14 15 from localflavor.es import tests as localflavor_es_tests 15 16 from localflavor.fi import tests as localflavor_fi_tests … … 43 44 'localflavor_ca_tests': localflavor_ca_tests, 44 45 'localflavor_ch_tests': localflavor_ch_tests, 45 46 'localflavor_cl_tests': localflavor_cl_tests, 47 'localflavor_cz_tests': localflavor_cz_tests, 46 48 'localflavor_de_tests': localflavor_de_tests, 47 49 'localflavor_es_tests': localflavor_es_tests, 48 50 'localflavor_fi_tests': localflavor_fi_tests, -
docs/ref/contrib/localflavor.txt
44 44 * Brazil_ 45 45 * Canada_ 46 46 * Chile_ 47 * Czech_ 47 48 * Finland_ 48 49 * France_ 49 50 * Germany_ … … 83 84 .. _Brazil: `Brazil (br)`_ 84 85 .. _Canada: `Canada (ca)`_ 85 86 .. _Chile: `Chile (cl)`_ 87 .. _Czech: `Czech (cz)`_ 86 88 .. _Finland: `Finland (fi)`_ 87 89 .. _France: `France (fr)`_ 88 90 .. _Germany: `Germany (de)`_ … … 231 233 A ``Select`` widget that uses a list of Chilean regions (Regiones) as its 232 234 choices. 233 235 236 Czech (``cz``) 237 ============== 238 239 .. class:: cz.forms.CZPostalCodeField 240 241 A form field that validates input as a Czech postal code. Valid formats 242 are XXXXX or XXX XX, where X is a digit. 243 244 .. class:: cz.forms.CZBirthNumberField 245 246 A form field that validates input as a Czech Birth Number. 247 A valid number must be in format XXXXXX/XXXX (slash is optional). 248 249 .. class:: cz.forms.CZICNumberField 250 251 A form field that validates input as a Czech IC number field. 252 253 .. class:: cz.forms.CZRegionSelect 254 255 A ``Select`` widget that uses a list of Czech regions as its choices. 256 234 257 Finland (``fi``) 235 258 ================ 236 259