utils.converters - Converter Components

Converters for zope.schema-based datatypes.

class waeup.kofa.utils.converters.DefaultFieldConverter(context)[source]

Bases: grokcore.component.components.Adapter

__doc__ = None
__implemented__ = <implementedBy waeup.kofa.utils.converters.DefaultFieldConverter>
__module__ = 'waeup.kofa.utils.converters'
__provides__

Special descriptor for class __provides__

The descriptor caches the implementedBy info, so that we can get declarations for objects without instance-specific interfaces a bit quicker.

For example:

>>> from zope.interface import Interface
>>> class IFooFactory(Interface):
...     pass
>>> class IFoo(Interface):
...     pass
>>> class C(object):
...     implements(IFoo)
...     classProvides(IFooFactory)
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> [i.getName() for i in C().__provides__]
['IFoo']
request_data(name, value, schema_field, prefix='', mode='create')[source]
class waeup.kofa.utils.converters.DefaultObjectConverter(iface)[source]

Bases: grokcore.component.components.Adapter

Turn string values into real values.

A converter can convert string values for objects that implement a certain interface into real values based on the given interface.

__doc__ = 'Turn string values into real values.\n\n A converter can convert string values for objects that implement a\n certain interface into real values based on the given interface.\n '
__implemented__ = <implementedBy waeup.kofa.utils.converters.DefaultObjectConverter>
__init__(iface)[source]
__module__ = 'waeup.kofa.utils.converters'
__provides__

Special descriptor for class __provides__

The descriptor caches the implementedBy info, so that we can get declarations for objects without instance-specific interfaces a bit quicker.

For example:

>>> from zope.interface import Interface
>>> class IFooFactory(Interface):
...     pass
>>> class IFoo(Interface):
...     pass
>>> class C(object):
...     implements(IFoo)
...     classProvides(IFooFactory)
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> [i.getName() for i in C().__provides__]
['IFoo']
fromStringDict(data_dict, context, form_fields=None, mode='create')[source]

Convert values in data_dict.

Converts data in data_dict into real values based on context and form_fields.

data_dict is a mapping (dict) from field names to values represented as strings.

The fields (keys) to convert can be given in optional form_fields. If given, form_fields should be an instance of zope.formlib.form.Fields. Suitable instances are for example created by grok.AutoFields.

If no form_fields are given, a default is computed from the associated interface.

The context can be an existing object (implementing the associated interface) or a factory name. If it is a string, we try to create an object using zope.component.createObject().

Returns a tuple (<FIELD_ERRORS>, <INVARIANT_ERRORS>, <DATA_DICT>) where

<FIELD_ERRORS>
is a list of tuples (<FIELD_NAME>, <ERROR>) for each error that happened when validating the input data in data_dict
<INVARIANT_ERRORS>
is a list of invariant errors concerning several fields
<DATA_DICT>
is a dict with the values from input dict converted.

If mode is 'create' or 'update' then some additional filtering applies:

  • values set to DELETION_MARKER are set to missing_value (or default value if field is required) and
  • values set to IGNORE_MARKER are ignored and thus not part of the returned <DATA_DICT>.

If errors happen, i.e. the error lists are not empty, always an empty <DATA_DICT> is returned.

If <DATA_DICT> is non-empty, there were no errors.

class waeup.kofa.utils.converters.ExtendedCheckBoxWidget(context, request)[source]

Bases: zope.formlib.boolwidgets.CheckBoxWidget

A checkbox widget that supports more input values as True/False markers.

The default bool widget expects the string ‘on’ as only valid True value in HTML forms for bool fields.

This widget also accepts ‘1’, ‘true’ and ‘yes’ for that. Also all uppercase/lowecase combinations of these strings are accepted.

The widget still renders True to 'on' when a form is generated.

__doc__ = "A checkbox widget that supports more input values as True/False\n markers.\n\n The default bool widget expects the string 'on' as only valid\n ``True`` value in HTML forms for bool fields.\n\n This widget also accepts '1', 'true' and 'yes' for that. Also all\n uppercase/lowecase combinations of these strings are accepted.\n\n The widget still renders ``True`` to ``'on'`` when a form is\n generated.\n "
__module__ = 'waeup.kofa.utils.converters'
_getFormInput()[source]

Returns the form input used by _toFieldValue.

Return values:

'on' checkbox is checked '' checkbox is not checked None form input was not provided
_toFieldValue(input)[source]

Convert from HTML presentation to Python bool.

true_markers = ['1', 'true', 'on', 'yes']
class waeup.kofa.utils.converters.ListFieldConverter(context)[source]

Bases: grokcore.component.components.Adapter

__doc__ = None
__implemented__ = <implementedBy waeup.kofa.utils.converters.ListFieldConverter>
__module__ = 'waeup.kofa.utils.converters'
__provides__

Special descriptor for class __provides__

The descriptor caches the implementedBy info, so that we can get declarations for objects without instance-specific interfaces a bit quicker.

For example:

>>> from zope.interface import Interface
>>> class IFooFactory(Interface):
...     pass
>>> class IFoo(Interface):
...     pass
>>> class C(object):
...     implements(IFoo)
...     classProvides(IFooFactory)
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> [i.getName() for i in C().__provides__]
['IFoo']
request_data(name, value, schema_field, prefix='', mode='create')[source]
class waeup.kofa.utils.converters.PhoneNumberFieldConverter(context)[source]

Bases: grokcore.component.components.Adapter

Convert strings into dict as expected from forms feeding PhoneWidget.

If you want strings without extra-checks imported, you can use schema.TextLine in your interface instead of PhoneNumber.

__doc__ = 'Convert strings into dict as expected from forms feeding PhoneWidget.\n\n If you want strings without extra-checks imported, you can use\n schema.TextLine in your interface instead of PhoneNumber.\n '
__implemented__ = <implementedBy waeup.kofa.utils.converters.PhoneNumberFieldConverter>
__module__ = 'waeup.kofa.utils.converters'
__provides__

Special descriptor for class __provides__

The descriptor caches the implementedBy info, so that we can get declarations for objects without instance-specific interfaces a bit quicker.

For example:

>>> from zope.interface import Interface
>>> class IFooFactory(Interface):
...     pass
>>> class IFoo(Interface):
...     pass
>>> class C(object):
...     implements(IFoo)
...     classProvides(IFooFactory)
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> [i.getName() for i in C().__provides__]
['IFoo']
request_data(name, value, schema_field, prefix='', mode='create')[source]
class waeup.kofa.utils.converters.ResultEntryConverter(context)[source]

Bases: grokcore.component.components.Adapter

__doc__ = None
__implemented__ = <implementedBy waeup.kofa.utils.converters.ResultEntryConverter>
__module__ = 'waeup.kofa.utils.converters'
__provides__

Special descriptor for class __provides__

The descriptor caches the implementedBy info, so that we can get declarations for objects without instance-specific interfaces a bit quicker.

For example:

>>> from zope.interface import Interface
>>> class IFooFactory(Interface):
...     pass
>>> class IFoo(Interface):
...     pass
>>> class C(object):
...     implements(IFoo)
...     classProvides(IFooFactory)
>>> [i.getName() for i in C.__provides__]
['IFooFactory']
>>> [i.getName() for i in C().__provides__]
['IFoo']
request_data(name, value, schema_field, prefix='', mode='create')[source]

Turn CSV values into ResultEntry-compatible form data.

Expects as value a _string_ like (u'mysubject', u'mygrade') and turns it into some dict like:

{
  'form.grade.subject': u'9234896395...',
  'form.grade.grade': u'7e67e9e777..'
  }

where the values are tokens from appropriate sources.

Such dicts can be transformed into real ResultEntry objects by input widgets used in converters.

waeup.kofa.utils.converters.getWidgetsData(widgets, form_prefix, data)[source]

Get data and validation errors from widgets for data.

Updates the dict in data with values from the widgets in widgets.

Returns a list of tuples (<WIDGET_NAME>, <ERROR>) where <WIDGET_NAME> is a widget name (normally the same as the associated field name) and <ERROR> is the exception that happened for that widget/field.

This is merely a copy from the same-named function in zope.formlib.form. The only difference is that we also store the fieldname for which a validation error happened in the returned error list (what the original does not do).