summaryrefslogtreecommitdiff
path: root/django/newforms
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2007-05-12 02:36:05 +0000
committerAdrian Holovaty <adrian@holovaty.com>2007-05-12 02:36:05 +0000
commit415e84ad53e0d0d8f7df87784c1893489bdbe0b8 (patch)
tree09541f8319c45ec51fb44154534abc41cb86aee9 /django/newforms
parent4938c8ea6db6f23ebb0883b8a092985344508b25 (diff)
newforms-admin: Merged to [5194]
git-svn-id: http://code.djangoproject.com/svn/django/branches/newforms-admin@5195 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/newforms')
-rw-r--r--django/newforms/fields.py10
-rw-r--r--django/newforms/forms.py11
-rw-r--r--django/newforms/widgets.py25
3 files changed, 32 insertions, 14 deletions
diff --git a/django/newforms/fields.py b/django/newforms/fields.py
index c3b74e93f7..509c099e86 100644
--- a/django/newforms/fields.py
+++ b/django/newforms/fields.py
@@ -332,7 +332,9 @@ class NullBooleanField(BooleanField):
return {True: True, False: False}.get(value, None)
class ChoiceField(Field):
- def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None, help_text=None):
+ widget = Select
+
+ def __init__(self, choices=(), required=True, widget=None, label=None, initial=None, help_text=None):
super(ChoiceField, self).__init__(required, widget, label, initial, help_text)
self.choices = choices
@@ -364,9 +366,7 @@ class ChoiceField(Field):
class MultipleChoiceField(ChoiceField):
hidden_widget = MultipleHiddenInput
-
- def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None, help_text=None):
- super(MultipleChoiceField, self).__init__(choices, required, widget, label, initial, help_text)
+ widget = SelectMultiple
def clean(self, value):
"""
@@ -457,7 +457,7 @@ class MultiValueField(Field):
for i, field in enumerate(self.fields):
try:
field_value = value[i]
- except KeyError:
+ except IndexError:
field_value = None
if self.required and field_value in EMPTY_VALUES:
raise ValidationError(gettext(u'This field is required.'))
diff --git a/django/newforms/forms.py b/django/newforms/forms.py
index 2d7a65d7fe..42a06fd00d 100644
--- a/django/newforms/forms.py
+++ b/django/newforms/forms.py
@@ -122,7 +122,14 @@ class BaseForm(StrAndUnicode):
else:
if errors_on_separate_row and bf_errors:
output.append(error_row % bf_errors)
- label = bf.label and bf.label_tag(escape(bf.label + ':')) or ''
+ if bf.label:
+ label = escape(bf.label)
+ # Only add a colon if the label does not end in punctuation.
+ if label[-1] not in ':?.!':
+ label += ':'
+ label = bf.label_tag(label) or ''
+ else:
+ label = ''
if field.help_text:
help_text = help_text_html % field.help_text
else:
@@ -262,7 +269,7 @@ class BoundField(StrAndUnicode):
def as_widget(self, widget, attrs=None):
attrs = attrs or {}
auto_id = self.auto_id
- if auto_id and not attrs.has_key('id') and not widget.attrs.has_key('id'):
+ if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
attrs['id'] = auto_id
if not self.form.is_bound:
data = self.form.initial.get(self.name, self.field.initial)
diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py
index f701faa35d..dd71ebc455 100644
--- a/django/newforms/widgets.py
+++ b/django/newforms/widgets.py
@@ -230,7 +230,7 @@ class RadioInput(StrAndUnicode):
return self.value == self.choice_value
def tag(self):
- if self.attrs.has_key('id'):
+ if 'id' in self.attrs:
self.attrs['id'] = '%s_%s' % (self.attrs['id'], self.index)
final_attrs = dict(self.attrs, type='radio', name=self.name, value=self.choice_value)
if self.is_checked():
@@ -260,8 +260,8 @@ class RadioSelect(Select):
"Returns a RadioFieldRenderer instance rather than a Unicode string."
if value is None: value = ''
str_value = smart_unicode(value) # Normalize to string.
- attrs = attrs or {}
- return RadioFieldRenderer(name, str_value, attrs, list(chain(self.choices, choices)))
+ final_attrs = self.build_attrs(attrs)
+ return RadioFieldRenderer(name, str_value, final_attrs, list(chain(self.choices, choices)))
def id_for_label(self, id_):
# RadioSelect is represented by multiple <input type="radio"> fields,
@@ -276,7 +276,7 @@ class RadioSelect(Select):
class CheckboxSelectMultiple(SelectMultiple):
def render(self, name, value, attrs=None, choices=()):
if value is None: value = []
- has_id = attrs and attrs.has_key('id')
+ has_id = attrs and 'id' in attrs
final_attrs = self.build_attrs(attrs, name=name)
output = [u'<ul>']
str_values = set([smart_unicode(v) for v in value]) # Normalize to strings.
@@ -327,16 +327,27 @@ class MultiWidget(Widget):
if not isinstance(value, list):
value = self.decompress(value)
output = []
+ final_attrs = self.build_attrs(attrs)
+ id_ = final_attrs.get('id', None)
for i, widget in enumerate(self.widgets):
try:
widget_value = value[i]
- except KeyError:
+ except IndexError:
widget_value = None
- output.append(widget.render(name + '_%s' % i, widget_value, attrs))
+ if id_:
+ final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))
+ output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
return self.format_output(output)
+ def id_for_label(self, id_):
+ # See the comment for RadioSelect.id_for_label()
+ if id_:
+ id_ += '_0'
+ return id_
+ id_for_label = classmethod(id_for_label)
+
def value_from_datadict(self, data, name):
- return [data.get(name + '_%s' % i) for i in range(len(self.widgets))]
+ return [widget.value_from_datadict(data, name + '_%s' % i) for i, widget in enumerate(self.widgets)]
def format_output(self, rendered_widgets):
return u''.join(rendered_widgets)