From 4a7727e46ebe1bd5ca5db92e62459eec1137fa04 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Wed, 16 May 2007 23:10:31 +0000 Subject: [PATCH] unicode: Unicode audit pass through localflavor. Also fixed a few common stylistic errors. git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5271 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/localflavor/au/forms.py | 10 +++---- django/contrib/localflavor/br/forms.py | 6 ++--- django/contrib/localflavor/cl/forms.py | 11 ++++---- django/contrib/localflavor/de/forms.py | 10 +++---- django/contrib/localflavor/fi/forms.py | 10 +++---- django/contrib/localflavor/fr/forms.py | 6 ++--- django/contrib/localflavor/is_/forms.py | 5 ++-- django/contrib/localflavor/it/forms.py | 8 +++--- django/contrib/localflavor/it/it_province.py | 2 +- django/contrib/localflavor/it/util.py | 26 +++++++++++-------- django/contrib/localflavor/jp/forms.py | 4 +-- django/contrib/localflavor/no/forms.py | 8 +++--- .../localflavor/no/no_municipalities.py | 10 +++---- django/contrib/localflavor/us/forms.py | 18 ++++++------- 14 files changed, 70 insertions(+), 64 deletions(-) diff --git a/django/contrib/localflavor/au/forms.py b/django/contrib/localflavor/au/forms.py index 02250e24ed..33e883511b 100644 --- a/django/contrib/localflavor/au/forms.py +++ b/django/contrib/localflavor/au/forms.py @@ -15,14 +15,14 @@ class AUPostCodeField(RegexField): def __init__(self, *args, **kwargs): super(AUPostCodeField, self).__init__(r'^\d{4}$', max_length=None, min_length=None, - error_message=ugettext(u'Enter a 4 digit post code.'), - *args, **kwargs) + error_message=ugettext('Enter a 4 digit post code.'), + *args, **kwargs) class AUPhoneNumberField(Field): """Australian phone number field.""" def clean(self, value): - """Validate a phone number. Strips parentheses, whitespace and - hyphens. + """ + Validate a phone number. Strips parentheses, whitespace and hyphens. """ super(AUPhoneNumberField, self).clean(value) if value in EMPTY_VALUES: @@ -39,5 +39,5 @@ class AUStateSelect(Select): choices. """ def __init__(self, attrs=None): - from au_states import STATE_CHOICES # relative import + from au_states import STATE_CHOICES super(AUStateSelect, self).__init__(attrs, choices=STATE_CHOICES) diff --git a/django/contrib/localflavor/br/forms.py b/django/contrib/localflavor/br/forms.py index bef6711734..c7082c0f9e 100644 --- a/django/contrib/localflavor/br/forms.py +++ b/django/contrib/localflavor/br/forms.py @@ -16,7 +16,7 @@ class BRZipCodeField(RegexField): super(BRZipCodeField, self).__init__(r'^\d{5}-\d{3}$', max_length=None, min_length=None, error_message=ugettext('Enter a zip code in the format XXXXX-XXX.'), - *args, **kwargs) + *args, **kwargs) class BRPhoneNumberField(Field): def clean(self, value): @@ -27,7 +27,7 @@ class BRPhoneNumberField(Field): m = phone_digits_re.search(value) if m: return u'%s-%s-%s' % (m.group(1), m.group(2), m.group(3)) - raise ValidationError(ugettext(u'Phone numbers must be in XX-XXXX-XXXX format.')) + raise ValidationError(ugettext('Phone numbers must be in XX-XXXX-XXXX format.')) class BRStateSelect(Select): """ @@ -35,7 +35,7 @@ class BRStateSelect(Select): as its choices. """ def __init__(self, attrs=None): - from br_states import STATE_CHOICES # relative import + from br_states import STATE_CHOICES super(BRStateSelect, self).__init__(attrs, choices=STATE_CHOICES) diff --git a/django/contrib/localflavor/cl/forms.py b/django/contrib/localflavor/cl/forms.py index 96eb91cf42..a737bcb5a7 100644 --- a/django/contrib/localflavor/cl/forms.py +++ b/django/contrib/localflavor/cl/forms.py @@ -5,6 +5,7 @@ Chile specific form helpers. from django.newforms import ValidationError from django.newforms.fields import RegexField, EMPTY_VALUES from django.utils.translation import ugettext +from django.utils.encoding import smart_unicode class CLRutField(RegexField): """ @@ -19,11 +20,11 @@ class CLRutField(RegexField): del kwargs['strict'] super(CLRutField, self).__init__(r'^(\d{1,2}\.)?\d{3}\.\d{3}-[\dkK]$', error_message=ugettext('Enter valid a Chilean RUT. The format is XX.XXX.XXX-X.'), - *args, **kwargs) + *args, **kwargs) else: # In non-strict mode, accept RUTs that validate but do not exist in # the real world. - super(CLRutField, self).__init__(r'^[\d\.]{1,11}-?[\dkK]$', error_message=ugettext(u'Enter valid a Chilean RUT'), *args, **kwargs) + super(CLRutField, self).__init__(r'^[\d\.]{1,11}-?[\dkK]$', error_message=ugettext('Enter valid a Chilean RUT'), *args, **kwargs) def clean(self, value): """ @@ -49,14 +50,14 @@ class CLRutField(RegexField): multi += 1 if multi == 8: multi = 2 - return '0123456789K0'[11 - suma % 11] + return u'0123456789K0'[11 - suma % 11] def _canonify(self, rut): """ Turns the RUT into one normalized format. Returns a (rut, verifier) tuple. """ - rut = str(rut).replace(' ', '').replace('.', '').replace('-', '') + rut = smart_unicode(rut).replace(' ', '').replace('.', '').replace('-', '') return rut[:-1], rut[-1] def _format(self, code, verifier=None): @@ -74,5 +75,5 @@ class CLRutField(RegexField): else: new_dot = pos - 3 code = code[:new_dot] + '.' + code[new_dot:] - return '%s-%s' % (code, verifier) + return u'%s-%s' % (code, verifier) diff --git a/django/contrib/localflavor/de/forms.py b/django/contrib/localflavor/de/forms.py index 34e4f176db..1a0b8587a6 100644 --- a/django/contrib/localflavor/de/forms.py +++ b/django/contrib/localflavor/de/forms.py @@ -13,15 +13,15 @@ class DEZipCodeField(RegexField): def __init__(self, *args, **kwargs): super(DEZipCodeField, self).__init__(r'^\d{5}$', max_length=None, min_length=None, - error_message=ugettext(u'Enter a zip code in the format XXXXX.'), - *args, **kwargs) + error_message=ugettext('Enter a zip code in the format XXXXX.'), + *args, **kwargs) class DEStateSelect(Select): """ A Select widget that uses a list of DE states as its choices. """ def __init__(self, attrs=None): - from de_states import STATE_CHOICES # relative import + from de_states import STATE_CHOICES super(DEStateSelect, self).__init__(attrs, choices=STATE_CHOICES) class DEIdentityCardNumberField(Field): @@ -57,7 +57,7 @@ class DEIdentityCardNumberField(Field): def clean(self, value): super(DEIdentityCardNumberField, self).clean(value) - error_msg = ugettext(u'Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.') + error_msg = ugettext('Enter a valid German identity card number in XXXXXXXXXXX-XXXXXXX-XXXXXXX-X format.') if value in EMPTY_VALUES: return u'' match = re.match(id_re, value) @@ -71,7 +71,7 @@ class DEIdentityCardNumberField(Field): if residence == '0000000000' or birthday == '0000000' or validity == '0000000': raise ValidationError(error_msg) - all_digits = "%s%s%s%s" % (residence, birthday, validity, checksum) + all_digits = u"%s%s%s%s" % (residence, birthday, validity, checksum) if not self.has_valid_checksum(residence) or not self.has_valid_checksum(birthday) or \ not self.has_valid_checksum(validity) or not self.has_valid_checksum(all_digits): raise ValidationError(error_msg) diff --git a/django/contrib/localflavor/fi/forms.py b/django/contrib/localflavor/fi/forms.py index 27e658362a..82e024f7b1 100644 --- a/django/contrib/localflavor/fi/forms.py +++ b/django/contrib/localflavor/fi/forms.py @@ -11,15 +11,15 @@ class FIZipCodeField(RegexField): def __init__(self, *args, **kwargs): super(FIZipCodeField, self).__init__(r'^\d{5}$', max_length=None, min_length=None, - error_message=ugettext(u'Enter a zip code in the format XXXXX.'), - *args, **kwargs) + error_message=ugettext('Enter a zip code in the format XXXXX.'), + *args, **kwargs) class FIMunicipalitySelect(Select): """ A Select widget that uses a list of Finnish municipalities as its choices. """ def __init__(self, attrs=None): - from fi_municipalities import MUNICIPALITY_CHOICES # relative import + from fi_municipalities import MUNICIPALITY_CHOICES super(FIMunicipalitySelect, self).__init__(attrs, choices=MUNICIPALITY_CHOICES) class FISocialSecurityNumber(Field): @@ -37,9 +37,9 @@ class FISocialSecurityNumber(Field): (?P(\d{3})) (?P[%s])$""" % checkmarks, value, re.VERBOSE | re.IGNORECASE) if not result: - raise ValidationError(ugettext(u'Enter a valid Finnish social security number.')) + raise ValidationError(ugettext('Enter a valid Finnish social security number.')) gd = result.groupdict() checksum = int(gd['date'] + gd['serial']) if checkmarks[checksum % len(checkmarks)] == gd['checksum'].upper(): return u'%s' % value.upper() - raise ValidationError(ugettext(u'Enter a valid Finnish social security number.')) + raise ValidationError(ugettext('Enter a valid Finnish social security number.')) diff --git a/django/contrib/localflavor/fr/forms.py b/django/contrib/localflavor/fr/forms.py index 5b7be616a3..cc0a5db259 100644 --- a/django/contrib/localflavor/fr/forms.py +++ b/django/contrib/localflavor/fr/forms.py @@ -14,8 +14,8 @@ class FRZipCodeField(RegexField): def __init__(self, *args, **kwargs): super(FRZipCodeField, self).__init__(r'^\d{5}$', max_length=None, min_length=None, - error_message=ugettext(u'Enter a zip code in the format XXXXX.'), - *args, **kwargs) + error_message=ugettext('Enter a zip code in the format XXXXX.'), + *args, **kwargs) class FRPhoneNumberField(Field): """ @@ -39,6 +39,6 @@ class FRDepartmentSelect(Select): A Select widget that uses a list of FR departments as its choices. """ def __init__(self, attrs=None): - from fr_department import DEPARTMENT_ASCII_CHOICES # relative import + from fr_department import DEPARTMENT_ASCII_CHOICES super(FRDepartmentSelect, self).__init__(attrs, choices=DEPARTMENT_ASCII_CHOICES) diff --git a/django/contrib/localflavor/is_/forms.py b/django/contrib/localflavor/is_/forms.py index b4c477f057..ef8799e29c 100644 --- a/django/contrib/localflavor/is_/forms.py +++ b/django/contrib/localflavor/is_/forms.py @@ -6,6 +6,7 @@ from django.newforms import ValidationError from django.newforms.fields import RegexField, EMPTY_VALUES from django.newforms.widgets import Select from django.utils.translation import ugettext +from django.utils.encoding import smart_unicode class ISIdNumberField(RegexField): """ @@ -13,7 +14,7 @@ class ISIdNumberField(RegexField): of Iceland has. """ def __init__(self, *args, **kwargs): - error_msg = ugettext(u'Enter a valid Icelandic identification number. The format is XXXXXX-XXXX.') + error_msg = ugettext('Enter a valid Icelandic identification number. The format is XXXXXX-XXXX.') kwargs['min_length'],kwargs['max_length'] = 10,11 super(ISIdNumberField, self).__init__(r'^\d{6}(-| )?\d{4}$', error_message=error_msg, *args, **kwargs) @@ -48,7 +49,7 @@ class ISIdNumberField(RegexField): Takes in the value in canonical form and returns it in the common display format. """ - return value[:6]+'-'+value[6:] + return smart_unicode(value[:6]+'-'+value[6:]) class ISPhoneNumberField(RegexField): """ diff --git a/django/contrib/localflavor/it/forms.py b/django/contrib/localflavor/it/forms.py index ae98b6f414..0b6eecec45 100644 --- a/django/contrib/localflavor/it/forms.py +++ b/django/contrib/localflavor/it/forms.py @@ -13,15 +13,15 @@ class ITZipCodeField(RegexField): def __init__(self, *args, **kwargs): super(ITZipCodeField, self).__init__(r'^\d{5}$', max_length=None, min_length=None, - error_message=ugettext(u'Enter a valid zip code.'), - *args, **kwargs) + error_message=ugettext('Enter a valid zip code.'), + *args, **kwargs) class ITRegionSelect(Select): """ A Select widget that uses a list of IT regions as its choices. """ def __init__(self, attrs=None): - from it_region import REGION_CHOICES # relative import + from it_region import REGION_CHOICES super(ITRegionSelect, self).__init__(attrs, choices=REGION_CHOICES) class ITProvinceSelect(Select): @@ -29,7 +29,7 @@ class ITProvinceSelect(Select): A Select widget that uses a list of IT regions as its choices. """ def __init__(self, attrs=None): - from it_province import PROVINCE_CHOICES # relative import + from it_province import PROVINCE_CHOICES super(ITProvinceSelect, self).__init__(attrs, choices=PROVINCE_CHOICES) class ITSocialSecurityNumberField(RegexField): diff --git a/django/contrib/localflavor/it/it_province.py b/django/contrib/localflavor/it/it_province.py index 1867191b31..669ecd7f95 100644 --- a/django/contrib/localflavor/it/it_province.py +++ b/django/contrib/localflavor/it/it_province.py @@ -33,7 +33,7 @@ PROVINCE_CHOICES = ( ('KR', 'Crotone'), ('CN', 'Cuneo'), ('EN', 'Enna'), -# ('FM', 'Fermo'), # active starting from 2009 +# ('FM', 'Fermo'), # active starting from 2009 ('FE', 'Ferrara'), ('FI', 'Firenze'), ('FG', 'Foggia'), diff --git a/django/contrib/localflavor/it/util.py b/django/contrib/localflavor/it/util.py index 49b607f160..c162ff7eff 100644 --- a/django/contrib/localflavor/it/util.py +++ b/django/contrib/localflavor/it/util.py @@ -1,23 +1,27 @@ +from django.utils.encoding import smart_str, smart_unicode + def ssn_check_digit(value): "Calculate Italian social security number check digit." ssn_even_chars = { - '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, - 'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, 'I': 8, 'J': 9, - 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, 'Q': 16, 'R': 17, 'S': 18, - 'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23, 'Y': 24, 'Z': 25 + '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, + '9': 9, 'A': 0, 'B': 1, 'C': 2, 'D': 3, 'E': 4, 'F': 5, 'G': 6, 'H': 7, + 'I': 8, 'J': 9, 'K': 10, 'L': 11, 'M': 12, 'N': 13, 'O': 14, 'P': 15, + 'Q': 16, 'R': 17, 'S': 18, 'T': 19, 'U': 20, 'V': 21, 'W': 22, 'X': 23, + 'Y': 24, 'Z': 25 } ssn_odd_chars = { - '0': 1, '1': 0, '2': 5, '3': 7, '4': 9, '5': 13, '6': 15, '7': 17, '8': 19, '9': 21, - 'A': 1, 'B': 0, 'C': 5, 'D': 7, 'E': 9, 'F': 13, 'G': 15, 'H': 17, 'I': 19, 'J': 21, - 'K': 2, 'L': 4, 'M': 18, 'N': 20, 'O': 11, 'P': 3, 'Q': 6, 'R': 8, 'S': 12, - 'T': 14, 'U': 16, 'V': 10, 'W': 22, 'X': 25, 'Y': 24, 'Z': 23 + '0': 1, '1': 0, '2': 5, '3': 7, '4': 9, '5': 13, '6': 15, '7': 17, '8': + 19, '9': 21, 'A': 1, 'B': 0, 'C': 5, 'D': 7, 'E': 9, 'F': 13, 'G': 15, + 'H': 17, 'I': 19, 'J': 21, 'K': 2, 'L': 4, 'M': 18, 'N': 20, 'O': 11, + 'P': 3, 'Q': 6, 'R': 8, 'S': 12, 'T': 14, 'U': 16, 'V': 10, 'W': 22, + 'X': 25, 'Y': 24, 'Z': 23 } # Chars from 'A' to 'Z' ssn_check_digits = [chr(x) for x in range(65, 91)] ssn = value.upper() total = 0 - for i in range(0,15): + for i in range(0, 15): try: if i % 2 == 0: total += ssn_odd_chars[ssn[i]] @@ -30,11 +34,11 @@ def ssn_check_digit(value): def vat_number_check_digit(vat_number): "Calculate Italian VAT number check digit." - normalized_vat_number = str(vat_number).zfill(10) + normalized_vat_number = smart_str(vat_number).zfill(10) total = 0 for i in range(0, 10, 2): total += int(normalized_vat_number[i]) for i in range(1, 11, 2): quotient , remainder = divmod(int(normalized_vat_number[i]) * 2, 10) total += quotient + remainder - return str((10 - total % 10) % 10) + return smart_unicode((10 - total % 10) % 10) diff --git a/django/contrib/localflavor/jp/forms.py b/django/contrib/localflavor/jp/forms.py index 9f6244e1c9..682ffb8c31 100644 --- a/django/contrib/localflavor/jp/forms.py +++ b/django/contrib/localflavor/jp/forms.py @@ -18,8 +18,8 @@ class JPPostalCodeField(RegexField): def __init__(self, *args, **kwargs): super(JPPostalCodeField, self).__init__(r'^\d{3}-\d{4}$|^\d{7}$', max_length=None, min_length=None, - error_message=ugettext(u'Enter a postal code in the format XXXXXXX or XXX-XXXX.'), - *args, **kwargs) + error_message=ugettext('Enter a postal code in the format XXXXXXX or XXX-XXXX.'), + *args, **kwargs) def clean(self, value): """ diff --git a/django/contrib/localflavor/no/forms.py b/django/contrib/localflavor/no/forms.py index 8d1867097c..a0d599324f 100644 --- a/django/contrib/localflavor/no/forms.py +++ b/django/contrib/localflavor/no/forms.py @@ -12,8 +12,8 @@ class NOZipCodeField(RegexField): def __init__(self, *args, **kwargs): super(NOZipCodeField, self).__init__(r'^\d{4}$', max_length=None, min_length=None, - error_message=ugettext(u'Enter a zip code in the format XXXX.'), - *args, **kwargs) + error_message=ugettext('Enter a zip code in the format XXXX.'), + *args, **kwargs) class NOMunicipalitySelect(Select): """ @@ -60,7 +60,7 @@ class NOSocialSecurityNumber(Field): self.gender = 'F' else: self.gender = 'M' - + digits = map(int, list(value)) weight_1 = [3, 7, 6, 1, 8, 9, 4, 5, 2, 1, 0] weight_2 = [5, 4, 3, 2, 7, 6, 5, 4, 3, 2, 1] @@ -74,4 +74,4 @@ class NOSocialSecurityNumber(Field): raise ValidationError(msg) return value - + diff --git a/django/contrib/localflavor/no/no_municipalities.py b/django/contrib/localflavor/no/no_municipalities.py index d66fef514c..d6bacda275 100644 --- a/django/contrib/localflavor/no/no_municipalities.py +++ b/django/contrib/localflavor/no/no_municipalities.py @@ -1,4 +1,4 @@ -# -*- coding: iso-8859-1 -*- +# -*- coding: utf-8 -*- """ An alphabetical list of Norwegian municipalities (fylker) fro use as `choices` in a formfield. @@ -15,18 +15,18 @@ MUNICIPALITY_CHOICES = ( ('hedmark', u'Hedmark'), ('hordaland', u'Hordaland'), ('janmayen', u'Jan Mayen'), - ('moreogromsdal', u'Møre og Romsdal'), - ('nordtrondelag', u'Nord-Trøndelag'), + ('moreogromsdal', u'Møre og Romsdal'), + ('nordtrondelag', u'Nord-Trøndelag'), ('nordland', u'Nordland'), ('oppland', u'Oppland'), ('oslo', u'Oslo'), ('rogaland', u'Rogaland'), ('sognogfjordane', u'Sogn og Fjordane'), ('svalbard', u'Svalbard'), - ('sortrondelag', u'Sør-Trøndelag'), + ('sortrondelag', u'Sør-Trøndelag'), ('telemark', u'Telemark'), ('troms', u'Troms'), ('vestagder', u'Vest-Agder'), ('vestfold', u'Vestfold'), - ('ostfold', u'Østfold') + ('ostfold', u'Østfold') ) diff --git a/django/contrib/localflavor/us/forms.py b/django/contrib/localflavor/us/forms.py index 70f7b0cef1..259a7f7058 100644 --- a/django/contrib/localflavor/us/forms.py +++ b/django/contrib/localflavor/us/forms.py @@ -15,8 +15,8 @@ class USZipCodeField(RegexField): def __init__(self, *args, **kwargs): super(USZipCodeField, self).__init__(r'^\d{5}(?:-\d{4})?$', max_length=None, min_length=None, - error_message=ugettext(u'Enter a zip code in the format XXXXX or XXXXX-XXXX.'), - *args, **kwargs) + error_message=ugettext('Enter a zip code in the format XXXXX or XXXXX-XXXX.'), + *args, **kwargs) class USPhoneNumberField(Field): def clean(self, value): @@ -38,17 +38,17 @@ class USSocialSecurityNumberField(Field): * Conforms to the XXX-XX-XXXX format. * No group consists entirely of zeroes. * The leading group is not "666" (block "666" will never be allocated). - * The number is not in the promotional block 987-65-4320 through 987-65-4329, - which are permanently invalid. + * The number is not in the promotional block 987-65-4320 through + 987-65-4329, which are permanently invalid. * The number is not one known to be invalid due to otherwise widespread - promotional use or distribution (e.g., the Woolworth's number or the 1962 - promotional number). + promotional use or distribution (e.g., the Woolworth's number or the + 1962 promotional number). """ def clean(self, value): super(USSocialSecurityNumberField, self).clean(value) if value in EMPTY_VALUES: return u'' - msg = ugettext(u'Enter a valid U.S. Social Security number in XXX-XX-XXXX format.') + msg = ugettext('Enter a valid U.S. Social Security number in XXX-XX-XXXX format.') match = re.match(ssn_re, value) if not match: raise ValidationError(msg) @@ -75,7 +75,7 @@ class USStateField(Field): abbreviation for the given state. """ def clean(self, value): - from us_states import STATES_NORMALIZED # relative import + from us_states import STATES_NORMALIZED super(USStateField, self).clean(value) if value in EMPTY_VALUES: return u'' @@ -95,5 +95,5 @@ class USStateSelect(Select): A Select widget that uses a list of U.S. states/territories as its choices. """ def __init__(self, attrs=None): - from us_states import STATE_CHOICES # relative import + from us_states import STATE_CHOICES super(USStateSelect, self).__init__(attrs, choices=STATE_CHOICES)