1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

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
This commit is contained in:
Malcolm Tredinnick 2007-05-16 23:10:31 +00:00
parent 1ad97d0634
commit 4a7727e46e
14 changed files with 70 additions and 64 deletions

View File

@ -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.'),
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)

View File

@ -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)

View File

@ -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):
"""
@ -23,7 +24,7 @@ class CLRutField(RegexField):
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)

View File

@ -13,7 +13,7 @@ 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.'),
error_message=ugettext('Enter a zip code in the format XXXXX.'),
*args, **kwargs)
class DEStateSelect(Select):
@ -21,7 +21,7 @@ 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)

View File

@ -11,7 +11,7 @@ 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.'),
error_message=ugettext('Enter a zip code in the format XXXXX.'),
*args, **kwargs)
class FIMunicipalitySelect(Select):
@ -19,7 +19,7 @@ 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<serial>(\d{3}))
(?P<checksum>[%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.'))

View File

@ -14,7 +14,7 @@ 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.'),
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)

View File

@ -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):
"""

View File

@ -13,7 +13,7 @@ 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.'),
error_message=ugettext('Enter a valid zip code.'),
*args, **kwargs)
class ITRegionSelect(Select):
@ -21,7 +21,7 @@ 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):

View File

@ -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)

View File

@ -18,7 +18,7 @@ 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.'),
error_message=ugettext('Enter a postal code in the format XXXXXXX or XXX-XXXX.'),
*args, **kwargs)
def clean(self, value):

View File

@ -12,7 +12,7 @@ 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.'),
error_message=ugettext('Enter a zip code in the format XXXX.'),
*args, **kwargs)
class NOMunicipalitySelect(Select):

View File

@ -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')
)

View File

@ -15,7 +15,7 @@ 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.'),
error_message=ugettext('Enter a zip code in the format XXXXX or XXXXX-XXXX.'),
*args, **kwargs)
class USPhoneNumberField(Field):
@ -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)