2015-09-09 21:09:59 +08:00
|
|
|
import datetime
|
2016-12-28 06:00:56 +08:00
|
|
|
import warnings
|
2015-09-09 21:09:59 +08:00
|
|
|
|
|
|
|
from django.forms.utils import flatatt, pretty_name
|
|
|
|
from django.forms.widgets import Textarea, TextInput
|
2016-12-28 06:00:56 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango21Warning
|
2016-11-20 04:54:19 +08:00
|
|
|
from django.utils.encoding import force_text
|
2016-08-03 14:18:48 +08:00
|
|
|
from django.utils.functional import cached_property
|
2015-09-09 21:09:59 +08:00
|
|
|
from django.utils.html import conditional_escape, format_html, html_safe
|
2016-12-28 06:00:56 +08:00
|
|
|
from django.utils.inspect import func_supports_parameter
|
2015-09-09 21:09:59 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-09-09 21:09:59 +08:00
|
|
|
|
|
|
|
__all__ = ('BoundField',)
|
|
|
|
|
|
|
|
|
|
|
|
@html_safe
|
2017-01-19 15:39:46 +08:00
|
|
|
class BoundField:
|
2015-09-09 21:09:59 +08:00
|
|
|
"A Field plus data"
|
|
|
|
def __init__(self, form, field, name):
|
|
|
|
self.form = form
|
|
|
|
self.field = field
|
|
|
|
self.name = name
|
|
|
|
self.html_name = form.add_prefix(name)
|
|
|
|
self.html_initial_name = form.add_initial_prefix(name)
|
|
|
|
self.html_initial_id = form.add_initial_prefix(self.auto_id)
|
|
|
|
if self.field.label is None:
|
|
|
|
self.label = pretty_name(name)
|
|
|
|
else:
|
|
|
|
self.label = self.field.label
|
|
|
|
self.help_text = field.help_text or ''
|
|
|
|
|
|
|
|
def __str__(self):
|
2017-01-25 05:23:56 +08:00
|
|
|
"""Render this field as an HTML widget."""
|
2015-09-09 21:09:59 +08:00
|
|
|
if self.field.show_hidden_initial:
|
|
|
|
return self.as_widget() + self.as_hidden(only_initial=True)
|
|
|
|
return self.as_widget()
|
|
|
|
|
2016-08-03 14:18:48 +08:00
|
|
|
@cached_property
|
|
|
|
def subwidgets(self):
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
2016-08-03 14:18:48 +08:00
|
|
|
Most widgets yield a single subwidget, but others like RadioSelect and
|
|
|
|
CheckboxSelectMultiple produce one subwidget for each choice.
|
2015-09-09 21:09:59 +08:00
|
|
|
|
2016-08-03 14:18:48 +08:00
|
|
|
This property is cached so that only one database query occurs when
|
|
|
|
rendering ModelChoiceFields.
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
|
|
|
id_ = self.field.widget.attrs.get('id') or self.auto_id
|
|
|
|
attrs = {'id': id_} if id_ else {}
|
2016-07-22 08:16:22 +08:00
|
|
|
attrs = self.build_widget_attrs(attrs)
|
2016-12-28 06:00:56 +08:00
|
|
|
return list(
|
|
|
|
BoundWidget(self.field.widget, widget, self.form.renderer)
|
|
|
|
for widget in self.field.widget.subwidgets(self.html_name, self.value(), attrs=attrs)
|
|
|
|
)
|
2016-08-03 14:18:48 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.subwidgets)
|
2015-09-09 21:09:59 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
2016-08-03 14:18:48 +08:00
|
|
|
return len(self.subwidgets)
|
2015-09-09 21:09:59 +08:00
|
|
|
|
|
|
|
def __getitem__(self, idx):
|
|
|
|
# Prevent unnecessary reevaluation when accessing BoundField's attrs
|
|
|
|
# from templates.
|
2016-12-29 23:27:49 +08:00
|
|
|
if not isinstance(idx, (int, slice)):
|
2015-09-09 21:09:59 +08:00
|
|
|
raise TypeError
|
2016-08-03 14:18:48 +08:00
|
|
|
return self.subwidgets[idx]
|
2015-09-09 21:09:59 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def errors(self):
|
|
|
|
"""
|
2017-01-25 05:23:56 +08:00
|
|
|
Return an ErrorList (empty if there are no errors) for this field.
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
|
|
|
return self.form.errors.get(self.name, self.form.error_class())
|
|
|
|
|
|
|
|
def as_widget(self, widget=None, attrs=None, only_initial=False):
|
|
|
|
"""
|
2017-01-25 05:23:56 +08:00
|
|
|
Render the field by rendering the passed widget, adding any HTML
|
|
|
|
attributes passed as attrs. If a widget isn't specified, use the
|
|
|
|
field's default widget.
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
|
|
|
if not widget:
|
|
|
|
widget = self.field.widget
|
|
|
|
|
|
|
|
if self.field.localize:
|
|
|
|
widget.is_localized = True
|
|
|
|
|
|
|
|
attrs = attrs or {}
|
2016-07-22 08:16:22 +08:00
|
|
|
attrs = self.build_widget_attrs(attrs, widget)
|
2015-09-09 21:09:59 +08:00
|
|
|
auto_id = self.auto_id
|
|
|
|
if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
|
|
|
|
if not only_initial:
|
|
|
|
attrs['id'] = auto_id
|
|
|
|
else:
|
|
|
|
attrs['id'] = self.html_initial_id
|
|
|
|
|
|
|
|
if not only_initial:
|
|
|
|
name = self.html_name
|
|
|
|
else:
|
|
|
|
name = self.html_initial_name
|
2016-12-28 06:00:56 +08:00
|
|
|
|
|
|
|
kwargs = {}
|
|
|
|
if func_supports_parameter(widget.render, 'renderer'):
|
|
|
|
kwargs['renderer'] = self.form.renderer
|
|
|
|
else:
|
|
|
|
warnings.warn(
|
|
|
|
'Add the `renderer` argument to the render() method of %s. '
|
|
|
|
'It will be mandatory in Django 2.1.' % widget.__class__,
|
|
|
|
RemovedInDjango21Warning, stacklevel=2,
|
|
|
|
)
|
2017-03-04 22:47:49 +08:00
|
|
|
return widget.render(
|
2016-12-28 06:00:56 +08:00
|
|
|
name=name,
|
|
|
|
value=self.value(),
|
|
|
|
attrs=attrs,
|
|
|
|
**kwargs
|
|
|
|
)
|
2015-09-09 21:09:59 +08:00
|
|
|
|
|
|
|
def as_text(self, attrs=None, **kwargs):
|
|
|
|
"""
|
2017-01-25 05:23:56 +08:00
|
|
|
Return a string of HTML for representing this as an <input type="text">.
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
|
|
|
return self.as_widget(TextInput(), attrs, **kwargs)
|
|
|
|
|
|
|
|
def as_textarea(self, attrs=None, **kwargs):
|
2017-01-25 05:23:56 +08:00
|
|
|
"""Return a string of HTML for representing this as a <textarea>."""
|
2015-09-09 21:09:59 +08:00
|
|
|
return self.as_widget(Textarea(), attrs, **kwargs)
|
|
|
|
|
|
|
|
def as_hidden(self, attrs=None, **kwargs):
|
|
|
|
"""
|
2017-01-25 05:23:56 +08:00
|
|
|
Return a string of HTML for representing this as an <input type="hidden">.
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
|
|
|
return self.as_widget(self.field.hidden_widget(), attrs, **kwargs)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def data(self):
|
|
|
|
"""
|
2017-01-25 05:23:56 +08:00
|
|
|
Return the data for this BoundField, or None if it wasn't given.
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
|
|
|
return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
|
|
|
|
|
|
|
|
def value(self):
|
|
|
|
"""
|
2017-01-25 05:23:56 +08:00
|
|
|
Return the value for this BoundField, using the initial value if
|
2015-09-09 21:09:59 +08:00
|
|
|
the form is not bound or the data otherwise.
|
|
|
|
"""
|
2016-08-19 08:55:47 +08:00
|
|
|
data = self.initial
|
|
|
|
if self.form.is_bound:
|
|
|
|
data = self.field.bound_data(self.data, data)
|
2015-09-09 21:09:59 +08:00
|
|
|
return self.field.prepare_value(data)
|
|
|
|
|
|
|
|
def label_tag(self, contents=None, attrs=None, label_suffix=None):
|
|
|
|
"""
|
2017-01-25 05:23:56 +08:00
|
|
|
Wrap the given contents in a <label>, if the field has an ID attribute.
|
|
|
|
contents should be mark_safe'd to avoid HTML escaping. If contents
|
|
|
|
aren't given, use the field's HTML-escaped label.
|
2015-09-09 21:09:59 +08:00
|
|
|
|
2017-01-25 05:23:56 +08:00
|
|
|
If attrs are given, use them as HTML attributes on the <label> tag.
|
2015-09-09 21:09:59 +08:00
|
|
|
|
2017-01-25 05:23:56 +08:00
|
|
|
label_suffix overrides the form's label_suffix.
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
|
|
|
contents = contents or self.label
|
|
|
|
if label_suffix is None:
|
|
|
|
label_suffix = (self.field.label_suffix if self.field.label_suffix is not None
|
|
|
|
else self.form.label_suffix)
|
|
|
|
# Only add the suffix if the label does not end in punctuation.
|
|
|
|
# Translators: If found as last label character, these punctuation
|
|
|
|
# characters will prevent the default label_suffix to be appended to the label
|
|
|
|
if label_suffix and contents and contents[-1] not in _(':?.!'):
|
|
|
|
contents = format_html('{}{}', contents, label_suffix)
|
|
|
|
widget = self.field.widget
|
|
|
|
id_ = widget.attrs.get('id') or self.auto_id
|
|
|
|
if id_:
|
|
|
|
id_for_label = widget.id_for_label(id_)
|
|
|
|
if id_for_label:
|
|
|
|
attrs = dict(attrs or {}, **{'for': id_for_label})
|
|
|
|
if self.field.required and hasattr(self.form, 'required_css_class'):
|
|
|
|
attrs = attrs or {}
|
|
|
|
if 'class' in attrs:
|
|
|
|
attrs['class'] += ' ' + self.form.required_css_class
|
|
|
|
else:
|
|
|
|
attrs['class'] = self.form.required_css_class
|
|
|
|
attrs = flatatt(attrs) if attrs else ''
|
|
|
|
contents = format_html('<label{}>{}</label>', attrs, contents)
|
|
|
|
else:
|
|
|
|
contents = conditional_escape(contents)
|
|
|
|
return mark_safe(contents)
|
|
|
|
|
|
|
|
def css_classes(self, extra_classes=None):
|
|
|
|
"""
|
2017-01-25 05:23:56 +08:00
|
|
|
Return a string of space-separated CSS classes for this field.
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
|
|
|
if hasattr(extra_classes, 'split'):
|
|
|
|
extra_classes = extra_classes.split()
|
|
|
|
extra_classes = set(extra_classes or [])
|
|
|
|
if self.errors and hasattr(self.form, 'error_css_class'):
|
|
|
|
extra_classes.add(self.form.error_css_class)
|
|
|
|
if self.field.required and hasattr(self.form, 'required_css_class'):
|
|
|
|
extra_classes.add(self.form.required_css_class)
|
|
|
|
return ' '.join(extra_classes)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_hidden(self):
|
2017-01-25 05:23:56 +08:00
|
|
|
"""Return True if this BoundField's widget is hidden."""
|
2015-09-09 21:09:59 +08:00
|
|
|
return self.field.widget.is_hidden
|
|
|
|
|
|
|
|
@property
|
|
|
|
def auto_id(self):
|
|
|
|
"""
|
2017-01-25 05:23:56 +08:00
|
|
|
Calculate and return the ID attribute for this BoundField, if the
|
|
|
|
associated Form has specified auto_id. Return an empty string otherwise.
|
2015-09-09 21:09:59 +08:00
|
|
|
"""
|
|
|
|
auto_id = self.form.auto_id
|
2016-09-03 02:17:15 +08:00
|
|
|
if auto_id and '%s' in force_text(auto_id):
|
|
|
|
return force_text(auto_id) % self.html_name
|
2015-09-09 21:09:59 +08:00
|
|
|
elif auto_id:
|
|
|
|
return self.html_name
|
|
|
|
return ''
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id_for_label(self):
|
|
|
|
"""
|
|
|
|
Wrapper around the field widget's `id_for_label` method.
|
|
|
|
Useful, for example, for focusing on this field regardless of whether
|
|
|
|
it has a single widget or a MultiWidget.
|
|
|
|
"""
|
|
|
|
widget = self.field.widget
|
|
|
|
id_ = widget.attrs.get('id') or self.auto_id
|
|
|
|
return widget.id_for_label(id_)
|
2016-07-22 08:16:22 +08:00
|
|
|
|
2016-08-13 03:43:24 +08:00
|
|
|
@cached_property
|
2016-08-13 01:59:01 +08:00
|
|
|
def initial(self):
|
2016-08-19 08:55:47 +08:00
|
|
|
data = self.form.get_initial_for_field(self.field, self.name)
|
|
|
|
# If this is an auto-generated default date, nix the microseconds for
|
|
|
|
# standardized handling. See #22502.
|
|
|
|
if (isinstance(data, (datetime.datetime, datetime.time)) and
|
|
|
|
not self.field.widget.supports_microseconds):
|
|
|
|
data = data.replace(microsecond=0)
|
2016-08-13 01:59:01 +08:00
|
|
|
return data
|
|
|
|
|
2016-07-22 08:16:22 +08:00
|
|
|
def build_widget_attrs(self, attrs, widget=None):
|
|
|
|
if not widget:
|
|
|
|
widget = self.field.widget
|
|
|
|
attrs = dict(attrs) # Copy attrs to avoid modifying the argument.
|
2016-08-13 01:59:01 +08:00
|
|
|
if widget.use_required_attribute(self.initial) and self.field.required and self.form.use_required_attribute:
|
2016-07-22 08:16:22 +08:00
|
|
|
attrs['required'] = True
|
|
|
|
if self.field.disabled:
|
|
|
|
attrs['disabled'] = True
|
|
|
|
return attrs
|
2016-12-28 06:00:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
@html_safe
|
2017-01-19 15:39:46 +08:00
|
|
|
class BoundWidget:
|
2016-12-28 06:00:56 +08:00
|
|
|
"""
|
|
|
|
A container class used for iterating over widgets. This is useful for
|
|
|
|
widgets that have choices. For example, the following can be used in a
|
|
|
|
template:
|
|
|
|
|
|
|
|
{% for radio in myform.beatles %}
|
|
|
|
<label for="{{ radio.id_for_label }}">
|
|
|
|
{{ radio.choice_label }}
|
|
|
|
<span class="radio">{{ radio.tag }}</span>
|
|
|
|
</label>
|
|
|
|
{% endfor %}
|
|
|
|
"""
|
|
|
|
def __init__(self, parent_widget, data, renderer):
|
|
|
|
self.parent_widget = parent_widget
|
|
|
|
self.data = data
|
|
|
|
self.renderer = renderer
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.tag(wrap_label=True)
|
|
|
|
|
|
|
|
def tag(self, wrap_label=False):
|
|
|
|
context = {'widget': self.data, 'wrap_label': wrap_label}
|
|
|
|
return self.parent_widget._render(self.template_name, context, self.renderer)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def template_name(self):
|
|
|
|
if 'template_name' in self.data:
|
|
|
|
return self.data['template_name']
|
|
|
|
return self.parent_widget.template_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id_for_label(self):
|
|
|
|
return 'id_%s_%s' % (self.data['name'], self.data['index'])
|
|
|
|
|
|
|
|
@property
|
|
|
|
def choice_label(self):
|
|
|
|
return self.data['label']
|