Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return name
@reify
def first_heading(self):
context_title = getattr(self.request.context, "title", None)
type_title = self.item_type or self.add.type_info.title
if context_title:
return _(
"Add ${type} to ${title}.",
mapping=dict(type=translate(type_title), title=context_title),
)
else:
return _("Add ${type}.", mapping=dict(type=translate(type_title)))
class CommaSeparatedListWidget(deform.widget.Widget):
def __init__(self, template=None, **kw):
super(CommaSeparatedListWidget, self).__init__(**kw)
self.template = template
def serialize(self, field, cstruct, readonly=False):
if cstruct in (colander.null, None):
cstruct = []
return field.renderer(self.template, field=field, cstruct=cstruct)
# noinspection PyMethodOverriding
@staticmethod
def deserialize(field, pstruct):
if pstruct is colander.null:
return colander.null
return [item.strip() for item in pstruct.split(",") if item]
# control value. I'm no genius, but IMO that's not very smart. But
# then again you also don't inject the thousands separator into the
# value attached to the control when editing an existing value.
# Because it's obvious we should have *both* the server and the
# client doing this bullshit on both serialization and
# deserialization. I understand completely, you're just a client
# library, IT'S NO PROBLEM. LET ME HELP YOU.
if self.options:
thousands = dict(self.options).get("thousands", ",")
pstruct = pstruct.replace(thousands, "")
if not pstruct:
return null
return pstruct
class AutocompleteInputWidget(Widget):
"""
Renders an ``<input type="text">`` widget which provides
autocompletion via a list of values using bootstrap's typeahead plugin
https://github.com/twitter/typeahead.js/
**Attributes/Arguments**
template
The template name used to render the widget. Default:
``typeahead_textinput``.
readonly_template
The template name used to render the widget in read-only mode.
Default: ``readonly/typeahead_textinput``.
strip
null_value
The value used to replace the ``colander.null`` value when it
is passed to the ``serialize`` or ``deserialize`` method.
Default: the empty string.
inline
If true, choices will be rendered on a single line.
Otherwise choices will be rendered one per line.
Default: false.
"""
template = "radio_choice"
readonly_template = "readonly/radio_choice"
class CheckboxChoiceWidget(Widget):
"""
Renders a sequence of ``<input type="check">`` buttons based on a
predefined set of values.
**Attributes/Arguments**
values
A sequence of two-tuples (the first value must be of type
string, unicode or integer, the second value must be string or
unicode) indicating allowable, displayed values, e.g. ``(
('true', 'True'), ('false', 'False') )``. The first element
in the tuple is the value that should be returned when the
form is posted. The second is the display value.
template
The template name used to render the widget. Default:
import colander
from deform.widget import MappingWidget, SequenceWidget, Widget
from future.builtins import bytes, str
class IPPDisplayWidget(Widget):
def serialize(self, field, cstruct=None, readonly=False):
return 'DISPLAY {}'.format(field.name.replace('_', '-'))
class IPPNameWidget(Widget):
def serialize(self, field, cstruct=None, readonly=False):
name = field.name
while field.parent is not None:
field = field.parent
value = getattr(field.schema, name)
return '{} "{}"'.format(name.upper(), value)
class IPPFileWidget(Widget):
def serialize(self, field, cstruct=None, readonly=False):
if cstruct is colander.null:
return ''
if not isinstance(cstruct, (str, bytes)):
raise ValueError('Wrong value provided for field {!r}'.format(
field.name))
return 'FILE {}'.format(cstruct)
except Exception as e:
field.unparseable = pstruct
raise Invalid(field.schema, str(e))
return rows
def handle_error(self, field, error):
msgs = []
if error.msg:
field.error = error
else:
for e in error.children:
msgs.append("line %s: %s" % (e.pos + 1, e))
field.error = Invalid(field.schema, "\n".join(msgs))
class TextInputCSVWidget(Widget):
"""
Widget used for a tuple of scalars; allows for editing a single
CSV line within a text input. Used with a schema node which is a
tuple composed entirely of scalar values (integers, strings, etc).
**Attributes/Arguments**
template
The template name used to render the widget. Default:
``textinput``.
readonly_template
The template name used to render the widget in read-only mode.
Default: ``readonly/textinput``.
"""
values = self.get_template_values(field, cstruct, kw)
return field.renderer(template, **values)
def deserialize(self, field, pstruct):
if pstruct is null:
return null
elif not isinstance(pstruct, string_types):
raise Invalid(field.schema, "Pstruct is not a string")
if self.strip:
pstruct = pstruct.strip()
if not pstruct:
return null
return pstruct
class MoneyInputWidget(Widget):
"""
Renders an ``<input type="text">`` widget with Javascript which enforces
a valid currency input. It should be used along with the
``colander.Decimal`` schema type (at least if you care about your money).
This widget depends on the ``jquery-maskMoney`` JQuery plugin.
**Attributes/Arguments**
template
The template name used to render the widget. Default:
``moneyinput``.
readonly_template
The template name used to render the widget in read-only mode.
Default: ``readonly/textinput``.
item_template
The template name used to render each item in the form.
Default: ``mapping_item``.
readonly_item_template
The template name used to render each item in the form.
Default: ``readonly/mapping_item``.
"""
template = "form"
readonly_template = "readonly/form"
class SequenceWidget(Widget):
"""Renders a sequence (0 .. N widgets, each the same as the other)
into a set of fields.
**Attributes/Arguments**
template
The template name used to render the widget. Default:
``sequence``.
readonly_template
The template name used to render the widget in read-only mode.
Default: ``readonly/sequence``.
item_template
The template name used to render each value in the sequence.
Default: ``sequence_item``.
options["formatSubmit"] = "yyyy-mm-dd"
kw.setdefault("options_json", json.dumps(options))
values = self.get_template_values(field, cstruct, kw)
return field.renderer(template, **values)
def deserialize(self, field, pstruct):
if pstruct in ("", null):
return null
try:
validated = self._pstruct_schema.deserialize(pstruct)
except Invalid as exc:
raise Invalid(field.schema, "Invalid pstruct: %s" % exc)
return validated["date_submit"] or validated["date"]
class DateTimeInputWidget(Widget):
"""
Renders a datetime picker widget.
The default rendering is as a pair of inputs (a date and a time) using
pickadate.js (https://github.com/amsul/pickadate.js).
Used for ``colander.DateTime`` schema nodes.
**Attributes/Arguments**
date_options
A dictionary of date options passed to pickadate.
time_options
A dictionary of time options passed to pickadate.
field value. Default: ``Password did not match confirm``.
redisplay
If true, on validation failure involving a field with this widget,
retain and redisplay the provided values in the password inputs. If
false, on validation failure, the fields will be rendered empty.
Default:: ``False``.
"""
template = "checked_password"
readonly_template = "readonly/checked_password"
mismatch_message = _("Password did not match confirm")
redisplay = False
class MappingWidget(Widget):
"""
Renders a mapping into a set of fields.
**Attributes/Arguments**
template
The template name used to render the widget. Default:
``mapping``. See also ``mapping_accordion`` template for hideable
user experience.
readonly_template
The template name used to render the widget in read-only mode.
Default: ``readonly/mapping``.
item_template
The template name used to render each item in the mapping.
options["formatSubmit"] = "HH:i"
kw.setdefault("options_json", json.dumps(options))
values = self.get_template_values(field, cstruct, kw)
return field.renderer(template, **values)
def deserialize(self, field, pstruct):
if pstruct in ("", null):
return null
try:
validated = self._pstruct_schema.deserialize(pstruct)
except Invalid as exc:
raise Invalid(field.schema, text_("Invalid pstruct: %s" % exc))
return validated["time_submit"] or validated["time"]
class DateInputWidget(Widget):
"""
Renders a date picker widget.
The default rendering is as a native HTML5 date input widget,
falling back to pickadate (https://github.com/amsul/pickadate.js.)
Most useful when the schema node is a ``colander.Date`` object.
**Attributes/Arguments**
options
Dictionary of options for configuring the widget (eg: date format)
template
The template name used to render the widget. Default:
``dateinput``.