1
0
mirror of https://github.com/django/django.git synced 2025-10-24 22:26:08 +00:00

Fixed #13032 - Added localize parameter to form fields to be able to selectively enable localization.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12867 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Jannis Leidel
2010-03-27 16:43:27 +00:00
parent ad5afd6ed2
commit ca4c6f65ea
7 changed files with 60 additions and 19 deletions

View File

@@ -71,7 +71,7 @@ class Field(object):
def __init__(self, required=True, widget=None, label=None, initial=None,
help_text=None, error_messages=None, show_hidden_initial=False,
validators=[]):
validators=[], localize=False):
# required -- Boolean that specifies whether the field is required.
# True by default.
# widget -- A Widget class, or instance of a Widget class, that should
@@ -85,9 +85,12 @@ class Field(object):
# initial -- A value to use in this Field's initial display. This value
# is *not* used as a fallback if data isn't given.
# help_text -- An optional string to use as "help text" for this Field.
# error_messages -- An optional dictionary to override the default
# messages that the field will raise.
# show_hidden_initial -- Boolean that specifies if it is needed to render a
# hidden widget with initial value after widget.
# validators -- List of addtional validators to use
# localize -- Boolean that specifies if the field should be localized.
if label is not None:
label = smart_unicode(label)
self.required, self.label, self.initial = required, label, initial
@@ -100,6 +103,9 @@ class Field(object):
if isinstance(widget, type):
widget = widget()
# Trigger the localization machinery if needed.
self.localize = localize
# Hook into self.widget_attrs() for any Field-specific HTML attributes.
extra_attrs = self.widget_attrs(widget)
if extra_attrs:
@@ -119,6 +125,9 @@ class Field(object):
self.validators = self.default_validators + validators
def localize_value(self, value):
return formats.localize_input(value)
def to_python(self, value):
return value
@@ -213,7 +222,8 @@ class IntegerField(Field):
value = super(IntegerField, self).to_python(value)
if value in validators.EMPTY_VALUES:
return None
value = formats.sanitize_separators(value)
if self.localize:
value = formats.sanitize_separators(value)
try:
value = int(str(value))
except (ValueError, TypeError):
@@ -233,7 +243,8 @@ class FloatField(IntegerField):
value = super(IntegerField, self).to_python(value)
if value in validators.EMPTY_VALUES:
return None
value = formats.sanitize_separators(value)
if self.localize:
value = formats.sanitize_separators(value)
try:
value = float(value)
except (ValueError, TypeError):
@@ -268,7 +279,8 @@ class DecimalField(Field):
"""
if value in validators.EMPTY_VALUES:
return None
value = formats.sanitize_separators(value)
if self.localize:
value = formats.sanitize_separators(value)
value = smart_str(value).strip()
try:
value = Decimal(value)

View File

@@ -443,6 +443,8 @@ class BoundField(StrAndUnicode):
name = self.html_name
else:
name = self.html_initial_name
if self.field.localize:
data = self.field.localize_value(data)
return widget.render(name, data, attrs=attrs)
def as_text(self, attrs=None, **kwargs):

View File

@@ -13,7 +13,6 @@ from django.utils.safestring import mark_safe
from django.utils import formats
import time
import datetime
from django.utils.formats import get_format
from util import flatatt
from urlparse import urljoin
@@ -214,7 +213,7 @@ class Input(Widget):
final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
if value != '':
# Only add the 'value' attribute if a value is non-empty.
final_attrs['value'] = force_unicode(formats.localize_input(value))
final_attrs['value'] = force_unicode(value)
return mark_safe(u'<input%s />' % flatatt(final_attrs))
class TextInput(Input):
@@ -319,7 +318,7 @@ class DateInput(Input):
# formatted by HiddenInput using formats.localize_input, which is not
# necessarily the format used for this widget. Attempt to convert it.
try:
input_format = get_format('DATE_INPUT_FORMATS')[0]
input_format = formats.get_format('DATE_INPUT_FORMATS')[0]
initial = datetime.date(*time.strptime(initial, input_format)[:3])
except (TypeError, ValueError):
pass
@@ -350,7 +349,7 @@ class DateTimeInput(Input):
# formatted by HiddenInput using formats.localize_input, which is not
# necessarily the format used for this widget. Attempt to convert it.
try:
input_format = get_format('DATETIME_INPUT_FORMATS')[0]
input_format = formats.get_format('DATETIME_INPUT_FORMATS')[0]
initial = datetime.datetime(*time.strptime(initial, input_format)[:6])
except (TypeError, ValueError):
pass
@@ -381,7 +380,7 @@ class TimeInput(Input):
# formatted by HiddenInput using formats.localize_input, which is not
# necessarily the format used for this widget. Attempt to convert it.
try:
input_format = get_format('TIME_INPUT_FORMATS')[0]
input_format = formats.get_format('TIME_INPUT_FORMATS')[0]
initial = datetime.time(*time.strptime(initial, input_format)[3:6])
except (TypeError, ValueError):
pass
@@ -771,6 +770,8 @@ class SplitHiddenDateTimeWidget(SplitDateTimeWidget):
"""
is_hidden = True
def __init__(self, attrs=None):
widgets = (HiddenInput(attrs=attrs), HiddenInput(attrs=attrs))
super(SplitDateTimeWidget, self).__init__(widgets, attrs)
def __init__(self, attrs=None, date_format=None, time_format=None):
super(SplitHiddenDateTimeWidget, self).__init__(attrs, date_format, time_format)
for widget in self.widgets:
widget.input_type = 'hidden'
widget.is_hidden = True