mirror of
https://github.com/django/django.git
synced 2025-10-27 07:36:08 +00:00
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes. Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702 git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
@@ -6,7 +6,7 @@ import datetime
|
||||
import re
|
||||
import time
|
||||
|
||||
from django.utils.translation import gettext
|
||||
from django.utils.translation import ugettext
|
||||
from django.utils.encoding import smart_unicode
|
||||
|
||||
from util import ErrorList, ValidationError
|
||||
@@ -84,7 +84,7 @@ class Field(object):
|
||||
Raises ValidationError for any errors.
|
||||
"""
|
||||
if self.required and value in EMPTY_VALUES:
|
||||
raise ValidationError(gettext(u'This field is required.'))
|
||||
raise ValidationError(ugettext(u'This field is required.'))
|
||||
return value
|
||||
|
||||
def widget_attrs(self, widget):
|
||||
@@ -107,9 +107,9 @@ class CharField(Field):
|
||||
return u''
|
||||
value = smart_unicode(value)
|
||||
if self.max_length is not None and len(value) > self.max_length:
|
||||
raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length)
|
||||
raise ValidationError(ugettext(u'Ensure this value has at most %d characters.') % self.max_length)
|
||||
if self.min_length is not None and len(value) < self.min_length:
|
||||
raise ValidationError(gettext(u'Ensure this value has at least %d characters.') % self.min_length)
|
||||
raise ValidationError(ugettext(u'Ensure this value has at least %d characters.') % self.min_length)
|
||||
return value
|
||||
|
||||
def widget_attrs(self, widget):
|
||||
@@ -132,11 +132,11 @@ class IntegerField(Field):
|
||||
try:
|
||||
value = int(value)
|
||||
except (ValueError, TypeError):
|
||||
raise ValidationError(gettext(u'Enter a whole number.'))
|
||||
raise ValidationError(ugettext(u'Enter a whole number.'))
|
||||
if self.max_value is not None and value > self.max_value:
|
||||
raise ValidationError(gettext(u'Ensure this value is less than or equal to %s.') % self.max_value)
|
||||
raise ValidationError(ugettext(u'Ensure this value is less than or equal to %s.') % self.max_value)
|
||||
if self.min_value is not None and value < self.min_value:
|
||||
raise ValidationError(gettext(u'Ensure this value is greater than or equal to %s.') % self.min_value)
|
||||
raise ValidationError(ugettext(u'Ensure this value is greater than or equal to %s.') % self.min_value)
|
||||
return value
|
||||
|
||||
class FloatField(Field):
|
||||
@@ -155,11 +155,11 @@ class FloatField(Field):
|
||||
try:
|
||||
value = float(value)
|
||||
except (ValueError, TypeError):
|
||||
raise ValidationError(gettext('Enter a number.'))
|
||||
raise ValidationError(ugettext('Enter a number.'))
|
||||
if self.max_value is not None and value > self.max_value:
|
||||
raise ValidationError(gettext('Ensure this value is less than or equal to %s.') % self.max_value)
|
||||
raise ValidationError(ugettext('Ensure this value is less than or equal to %s.') % self.max_value)
|
||||
if self.min_value is not None and value < self.min_value:
|
||||
raise ValidationError(gettext('Ensure this value is greater than or equal to %s.') % self.min_value)
|
||||
raise ValidationError(ugettext('Ensure this value is greater than or equal to %s.') % self.min_value)
|
||||
return value
|
||||
|
||||
decimal_re = re.compile(r'^-?(?P<digits>\d+)(\.(?P<decimals>\d+))?$')
|
||||
@@ -183,21 +183,21 @@ class DecimalField(Field):
|
||||
value = value.strip()
|
||||
match = decimal_re.search(value)
|
||||
if not match:
|
||||
raise ValidationError(gettext('Enter a number.'))
|
||||
raise ValidationError(ugettext('Enter a number.'))
|
||||
else:
|
||||
value = Decimal(value)
|
||||
digits = len(match.group('digits') or '')
|
||||
decimals = len(match.group('decimals') or '')
|
||||
if self.max_value is not None and value > self.max_value:
|
||||
raise ValidationError(gettext('Ensure this value is less than or equal to %s.') % self.max_value)
|
||||
raise ValidationError(ugettext('Ensure this value is less than or equal to %s.') % self.max_value)
|
||||
if self.min_value is not None and value < self.min_value:
|
||||
raise ValidationError(gettext('Ensure this value is greater than or equal to %s.') % self.min_value)
|
||||
raise ValidationError(ugettext('Ensure this value is greater than or equal to %s.') % self.min_value)
|
||||
if self.max_digits is not None and (digits + decimals) > self.max_digits:
|
||||
raise ValidationError(gettext('Ensure that there are no more than %s digits in total.') % self.max_digits)
|
||||
raise ValidationError(ugettext('Ensure that there are no more than %s digits in total.') % self.max_digits)
|
||||
if self.decimal_places is not None and decimals > self.decimal_places:
|
||||
raise ValidationError(gettext('Ensure that there are no more than %s decimal places.') % self.decimal_places)
|
||||
raise ValidationError(ugettext('Ensure that there are no more than %s decimal places.') % self.decimal_places)
|
||||
if self.max_digits is not None and self.decimal_places is not None and digits > (self.max_digits - self.decimal_places):
|
||||
raise ValidationError(gettext('Ensure that there are no more than %s digits before the decimal point.') % (self.max_digits - self.decimal_places))
|
||||
raise ValidationError(ugettext('Ensure that there are no more than %s digits before the decimal point.') % (self.max_digits - self.decimal_places))
|
||||
return value
|
||||
|
||||
DEFAULT_DATE_INPUT_FORMATS = (
|
||||
@@ -230,7 +230,7 @@ class DateField(Field):
|
||||
return datetime.date(*time.strptime(value, format)[:3])
|
||||
except ValueError:
|
||||
continue
|
||||
raise ValidationError(gettext(u'Enter a valid date.'))
|
||||
raise ValidationError(ugettext(u'Enter a valid date.'))
|
||||
|
||||
DEFAULT_TIME_INPUT_FORMATS = (
|
||||
'%H:%M:%S', # '14:30:59'
|
||||
@@ -257,7 +257,7 @@ class TimeField(Field):
|
||||
return datetime.time(*time.strptime(value, format)[3:6])
|
||||
except ValueError:
|
||||
continue
|
||||
raise ValidationError(gettext(u'Enter a valid time.'))
|
||||
raise ValidationError(ugettext(u'Enter a valid time.'))
|
||||
|
||||
DEFAULT_DATETIME_INPUT_FORMATS = (
|
||||
'%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59'
|
||||
@@ -293,7 +293,7 @@ class DateTimeField(Field):
|
||||
return datetime.datetime(*time.strptime(value, format)[:6])
|
||||
except ValueError:
|
||||
continue
|
||||
raise ValidationError(gettext(u'Enter a valid date/time.'))
|
||||
raise ValidationError(ugettext(u'Enter a valid date/time.'))
|
||||
|
||||
class RegexField(Field):
|
||||
def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
|
||||
@@ -307,7 +307,7 @@ class RegexField(Field):
|
||||
regex = re.compile(regex)
|
||||
self.regex = regex
|
||||
self.max_length, self.min_length = max_length, min_length
|
||||
self.error_message = error_message or gettext(u'Enter a valid value.')
|
||||
self.error_message = error_message or ugettext(u'Enter a valid value.')
|
||||
|
||||
def clean(self, value):
|
||||
"""
|
||||
@@ -321,9 +321,9 @@ class RegexField(Field):
|
||||
if value == u'':
|
||||
return value
|
||||
if self.max_length is not None and len(value) > self.max_length:
|
||||
raise ValidationError(gettext(u'Ensure this value has at most %d characters.') % self.max_length)
|
||||
raise ValidationError(ugettext(u'Ensure this value has at most %d characters.') % self.max_length)
|
||||
if self.min_length is not None and len(value) < self.min_length:
|
||||
raise ValidationError(gettext(u'Ensure this value has at least %d characters.') % self.min_length)
|
||||
raise ValidationError(ugettext(u'Ensure this value has at least %d characters.') % self.min_length)
|
||||
if not self.regex.search(value):
|
||||
raise ValidationError(self.error_message)
|
||||
return value
|
||||
@@ -336,7 +336,7 @@ email_re = re.compile(
|
||||
class EmailField(RegexField):
|
||||
def __init__(self, max_length=None, min_length=None, *args, **kwargs):
|
||||
RegexField.__init__(self, email_re, max_length, min_length,
|
||||
gettext(u'Enter a valid e-mail address.'), *args, **kwargs)
|
||||
ugettext(u'Enter a valid e-mail address.'), *args, **kwargs)
|
||||
|
||||
url_re = re.compile(
|
||||
r'^https?://' # http:// or https://
|
||||
@@ -354,7 +354,7 @@ except ImportError:
|
||||
class URLField(RegexField):
|
||||
def __init__(self, max_length=None, min_length=None, verify_exists=False,
|
||||
validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs):
|
||||
super(URLField, self).__init__(url_re, max_length, min_length, gettext(u'Enter a valid URL.'), *args, **kwargs)
|
||||
super(URLField, self).__init__(url_re, max_length, min_length, ugettext(u'Enter a valid URL.'), *args, **kwargs)
|
||||
self.verify_exists = verify_exists
|
||||
self.user_agent = validator_user_agent
|
||||
|
||||
@@ -376,9 +376,9 @@ class URLField(RegexField):
|
||||
req = urllib2.Request(value, None, headers)
|
||||
u = urllib2.urlopen(req)
|
||||
except ValueError:
|
||||
raise ValidationError(gettext(u'Enter a valid URL.'))
|
||||
raise ValidationError(ugettext(u'Enter a valid URL.'))
|
||||
except: # urllib2.URLError, httplib.InvalidURL, etc.
|
||||
raise ValidationError(gettext(u'This URL appears to be a broken link.'))
|
||||
raise ValidationError(ugettext(u'This URL appears to be a broken link.'))
|
||||
return value
|
||||
|
||||
class BooleanField(Field):
|
||||
@@ -427,9 +427,9 @@ class ChoiceField(Field):
|
||||
value = smart_unicode(value)
|
||||
if value == u'':
|
||||
return value
|
||||
valid_values = set([str(k) for k, v in self.choices])
|
||||
valid_values = set([smart_unicode(k) for k, v in self.choices])
|
||||
if value not in valid_values:
|
||||
raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
|
||||
raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
|
||||
return value
|
||||
|
||||
class MultipleChoiceField(ChoiceField):
|
||||
@@ -441,11 +441,11 @@ class MultipleChoiceField(ChoiceField):
|
||||
Validates that the input is a list or tuple.
|
||||
"""
|
||||
if self.required and not value:
|
||||
raise ValidationError(gettext(u'This field is required.'))
|
||||
raise ValidationError(ugettext(u'This field is required.'))
|
||||
elif not self.required and not value:
|
||||
return []
|
||||
if not isinstance(value, (list, tuple)):
|
||||
raise ValidationError(gettext(u'Enter a list of values.'))
|
||||
raise ValidationError(ugettext(u'Enter a list of values.'))
|
||||
new_value = []
|
||||
for val in value:
|
||||
val = smart_unicode(val)
|
||||
@@ -454,7 +454,7 @@ class MultipleChoiceField(ChoiceField):
|
||||
valid_values = set([smart_unicode(k) for k, v in self.choices])
|
||||
for val in new_value:
|
||||
if val not in valid_values:
|
||||
raise ValidationError(gettext(u'Select a valid choice. %s is not one of the available choices.') % val)
|
||||
raise ValidationError(ugettext(u'Select a valid choice. %s is not one of the available choices.') % val)
|
||||
return new_value
|
||||
|
||||
class ComboField(Field):
|
||||
@@ -520,18 +520,18 @@ class MultiValueField(Field):
|
||||
if not value or isinstance(value, (list, tuple)):
|
||||
if not value or not [v for v in value if v not in EMPTY_VALUES]:
|
||||
if self.required:
|
||||
raise ValidationError(gettext(u'This field is required.'))
|
||||
raise ValidationError(ugettext(u'This field is required.'))
|
||||
else:
|
||||
return self.compress([])
|
||||
else:
|
||||
raise ValidationError(gettext(u'Enter a list of values.'))
|
||||
raise ValidationError(ugettext(u'Enter a list of values.'))
|
||||
for i, field in enumerate(self.fields):
|
||||
try:
|
||||
field_value = value[i]
|
||||
except IndexError:
|
||||
field_value = None
|
||||
if self.required and field_value in EMPTY_VALUES:
|
||||
raise ValidationError(gettext(u'This field is required.'))
|
||||
raise ValidationError(ugettext(u'This field is required.'))
|
||||
try:
|
||||
clean_data.append(field.clean(field_value))
|
||||
except ValidationError, e:
|
||||
@@ -564,8 +564,8 @@ class SplitDateTimeField(MultiValueField):
|
||||
# Raise a validation error if time or date is empty
|
||||
# (possible if SplitDateTimeField has required=False).
|
||||
if data_list[0] in EMPTY_VALUES:
|
||||
raise ValidationError(gettext(u'Enter a valid date.'))
|
||||
raise ValidationError(ugettext(u'Enter a valid date.'))
|
||||
if data_list[1] in EMPTY_VALUES:
|
||||
raise ValidationError(gettext(u'Enter a valid time.'))
|
||||
raise ValidationError(ugettext(u'Enter a valid time.'))
|
||||
return datetime.datetime.combine(*data_list)
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user