[7196] | 1 | ## $Id: converters.py 17787 2024-05-15 06:42:58Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[4805] | 18 | """Converters for zope.schema-based datatypes. |
---|
| 19 | """ |
---|
| 20 | import grok |
---|
[6263] | 21 | from zope.component import createObject |
---|
[6258] | 22 | from zope.formlib import form |
---|
[6278] | 23 | from zope.formlib.boolwidgets import CheckBoxWidget |
---|
[6260] | 24 | from zope.formlib.form import ( |
---|
[6276] | 25 | _widgetKey, WidgetInputError, ValidationError, InputErrors, expandPrefix) |
---|
[12415] | 26 | from zope.formlib.interfaces import IInputWidget, ConversionError |
---|
[6276] | 27 | from zope.interface import Interface |
---|
[6258] | 28 | from zope.publisher.browser import TestRequest |
---|
[7932] | 29 | from zope.schema.interfaces import IList |
---|
| 30 | from waeup.kofa.interfaces import ( |
---|
[17764] | 31 | IObjectConverter, IResultEntryField, IRefereeEntryField, |
---|
| 32 | IFieldConverter, SubjectSource, |
---|
[8214] | 33 | GradeSource, DELETION_MARKER, IGNORE_MARKER) |
---|
[8175] | 34 | from waeup.kofa.schema.interfaces import IPhoneNumber |
---|
[7932] | 35 | from waeup.kofa.schoolgrades import ResultEntry |
---|
[17764] | 36 | from waeup.kofa.refereeentries import RefereeEntry |
---|
[6258] | 37 | |
---|
[6278] | 38 | class ExtendedCheckBoxWidget(CheckBoxWidget): |
---|
| 39 | """A checkbox widget that supports more input values as True/False |
---|
[7597] | 40 | markers. |
---|
[6278] | 41 | |
---|
| 42 | The default bool widget expects the string 'on' as only valid |
---|
| 43 | ``True`` value in HTML forms for bool fields. |
---|
| 44 | |
---|
| 45 | This widget also accepts '1', 'true' and 'yes' for that. Also all |
---|
| 46 | uppercase/lowecase combinations of these strings are accepted. |
---|
| 47 | |
---|
| 48 | The widget still renders ``True`` to ``'on'`` when a form is |
---|
| 49 | generated. |
---|
| 50 | """ |
---|
| 51 | true_markers = ['1', 'true', 'on', 'yes'] |
---|
| 52 | |
---|
| 53 | def _toFieldValue(self, input): |
---|
| 54 | """Convert from HTML presentation to Python bool.""" |
---|
| 55 | if not isinstance(input, basestring): |
---|
| 56 | return False |
---|
| 57 | return input.lower() in self.true_markers |
---|
| 58 | |
---|
| 59 | def _getFormInput(self): |
---|
| 60 | """Returns the form input used by `_toFieldValue`. |
---|
| 61 | |
---|
| 62 | Return values: |
---|
| 63 | |
---|
| 64 | ``'on'`` checkbox is checked |
---|
| 65 | ``''`` checkbox is not checked |
---|
| 66 | ``None`` form input was not provided |
---|
| 67 | |
---|
| 68 | """ |
---|
| 69 | value = self.request.get(self.name) |
---|
| 70 | if isinstance(value, basestring): |
---|
| 71 | value = value.lower() |
---|
| 72 | if value in self.true_markers: |
---|
| 73 | return 'on' |
---|
| 74 | elif self.name + '.used' in self.request: |
---|
| 75 | return '' |
---|
| 76 | else: |
---|
| 77 | return None |
---|
| 78 | |
---|
[6260] | 79 | def getWidgetsData(widgets, form_prefix, data): |
---|
[6273] | 80 | """Get data and validation errors from `widgets` for `data`. |
---|
| 81 | |
---|
| 82 | Updates the dict in `data` with values from the widgets in |
---|
| 83 | `widgets`. |
---|
| 84 | |
---|
| 85 | Returns a list of tuples ``(<WIDGET_NAME>, <ERROR>)`` where |
---|
| 86 | ``<WIDGET_NAME>`` is a widget name (normally the same as the |
---|
| 87 | associated field name) and ``<ERROR>`` is the exception that |
---|
| 88 | happened for that widget/field. |
---|
| 89 | |
---|
| 90 | This is merely a copy from the same-named function in |
---|
| 91 | :mod:`zope.formlib.form`. The only difference is that we also |
---|
| 92 | store the fieldname for which a validation error happened in the |
---|
| 93 | returned error list (what the original does not do). |
---|
| 94 | """ |
---|
[6260] | 95 | errors = [] |
---|
| 96 | form_prefix = expandPrefix(form_prefix) |
---|
| 97 | |
---|
| 98 | for input, widget in widgets.__iter_input_and_widget__(): |
---|
| 99 | if input and IInputWidget.providedBy(widget): |
---|
| 100 | name = _widgetKey(widget, form_prefix) |
---|
| 101 | |
---|
| 102 | if not widget.hasInput(): |
---|
| 103 | continue |
---|
| 104 | try: |
---|
| 105 | data[name] = widget.getInputValue() |
---|
| 106 | except ValidationError, error: |
---|
[12415] | 107 | error = ConversionError(u'Validation failed') |
---|
[6260] | 108 | errors.append((name, error)) |
---|
[12415] | 109 | except WidgetInputError, error: |
---|
| 110 | error = ConversionError(u'Invalid input') |
---|
[6260] | 111 | errors.append((name, error)) |
---|
[12415] | 112 | except ConversionError, error: |
---|
| 113 | errors.append((name, error)) |
---|
[6260] | 114 | return errors |
---|
| 115 | |
---|
[7932] | 116 | class DefaultFieldConverter(grok.Adapter): |
---|
| 117 | grok.context(Interface) |
---|
[8214] | 118 | grok.implements(IFieldConverter) |
---|
[6278] | 119 | |
---|
[8214] | 120 | def request_data(self, name, value, schema_field, prefix='', |
---|
| 121 | mode='create'): |
---|
[9932] | 122 | if prefix == 'form.sex' and isinstance(value, basestring): |
---|
| 123 | value = value.lower() |
---|
[7932] | 124 | return {prefix: value} |
---|
| 125 | |
---|
| 126 | class ListFieldConverter(grok.Adapter): |
---|
| 127 | grok.context(IList) |
---|
[8214] | 128 | grok.implements(IFieldConverter) |
---|
[7932] | 129 | |
---|
[8214] | 130 | def request_data(self, name, value, schema_field, prefix='', |
---|
| 131 | mode='create'): |
---|
[7932] | 132 | value_type = schema_field.value_type |
---|
| 133 | try: |
---|
| 134 | items = eval(value) |
---|
| 135 | except: |
---|
| 136 | return {prefix: value} |
---|
| 137 | result = {'%s.count' % prefix: len(items)} |
---|
| 138 | for num, item in enumerate(items): |
---|
| 139 | sub_converter = IFieldConverter(value_type) |
---|
| 140 | result.update(sub_converter.request_data( |
---|
| 141 | unicode(num), unicode(item), |
---|
| 142 | value_type, "%s.%s." % (prefix, num))) |
---|
| 143 | return result |
---|
| 144 | |
---|
[8175] | 145 | class PhoneNumberFieldConverter(grok.Adapter): |
---|
| 146 | """Convert strings into dict as expected from forms feeding PhoneWidget. |
---|
| 147 | |
---|
| 148 | If you want strings without extra-checks imported, you can use |
---|
| 149 | schema.TextLine in your interface instead of PhoneNumber. |
---|
| 150 | """ |
---|
| 151 | grok.context(IPhoneNumber) |
---|
[8214] | 152 | grok.implements(IFieldConverter) |
---|
[8175] | 153 | |
---|
[8214] | 154 | def request_data(self, name, value, schema_field, prefix='', |
---|
| 155 | mode='create'): |
---|
[8175] | 156 | parts = value.split('-', 2) |
---|
| 157 | country = '' |
---|
| 158 | area = '' |
---|
| 159 | ext = '' |
---|
| 160 | if len(parts) == 3: |
---|
| 161 | country = parts[0] |
---|
| 162 | area = parts[1] |
---|
| 163 | ext = parts[2] |
---|
| 164 | elif len(parts) == 2: |
---|
| 165 | country = parts[0] |
---|
| 166 | ext = parts[1] |
---|
| 167 | else: |
---|
| 168 | ext = value |
---|
| 169 | result = { |
---|
| 170 | u'%s.country' % prefix: country, |
---|
| 171 | u'%s.area' % prefix: area, |
---|
| 172 | u'%s.ext' % prefix: ext} |
---|
| 173 | return result |
---|
| 174 | |
---|
[7932] | 175 | class ResultEntryConverter(grok.Adapter): |
---|
| 176 | grok.context(IResultEntryField) |
---|
[8214] | 177 | grok.implements(IFieldConverter) |
---|
[7932] | 178 | |
---|
[8214] | 179 | def request_data(self, name, value, schema_field, prefix='', |
---|
| 180 | mode='create'): |
---|
[7932] | 181 | """Turn CSV values into ResultEntry-compatible form data. |
---|
| 182 | |
---|
| 183 | Expects as `value` a _string_ like ``(u'mysubject', |
---|
| 184 | u'mygrade')`` and turns it into some dict like:: |
---|
| 185 | |
---|
| 186 | { |
---|
| 187 | 'form.grade.subject': u'9234896395...', |
---|
| 188 | 'form.grade.grade': u'7e67e9e777..' |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | where the values are tokens from appropriate sources. |
---|
| 192 | |
---|
| 193 | Such dicts can be transformed into real ResultEntry objects by |
---|
| 194 | input widgets used in converters. |
---|
| 195 | """ |
---|
| 196 | try: |
---|
| 197 | entry = ResultEntry.from_string(value) |
---|
| 198 | subj, grade = entry.subject, entry.grade |
---|
| 199 | except: |
---|
| 200 | return {prefix: value} |
---|
| 201 | # web forms send tokens instead of real values |
---|
| 202 | s_token = SubjectSource().factory.getToken(subj) |
---|
| 203 | g_token = GradeSource().factory.getToken(grade) |
---|
| 204 | result = { |
---|
| 205 | "%ssubject" % (prefix): s_token, |
---|
| 206 | "%sgrade" % (prefix): g_token, |
---|
| 207 | } |
---|
| 208 | return result |
---|
| 209 | |
---|
[17764] | 210 | class RefereeEntryConverter(grok.Adapter): |
---|
| 211 | grok.context(IRefereeEntryField) |
---|
| 212 | grok.implements(IFieldConverter) |
---|
| 213 | |
---|
| 214 | def request_data(self, name, value, schema_field, prefix='', |
---|
| 215 | mode='create'): |
---|
| 216 | """Turn CSV values into RefereeEntry-compatible form data. |
---|
| 217 | See ResultEntryConverter. |
---|
| 218 | """ |
---|
| 219 | try: |
---|
| 220 | entry = RefereeEntry.from_string(value) |
---|
| 221 | name, email, email_sent = entry.name, entry.email, entry.email_sent |
---|
| 222 | except: |
---|
| 223 | return {prefix: value} |
---|
| 224 | result = { |
---|
| 225 | "%sname" % (prefix): name, |
---|
| 226 | "%semail" % (prefix): email, |
---|
| 227 | } |
---|
| 228 | return result |
---|
| 229 | |
---|
[6273] | 230 | class DefaultObjectConverter(grok.Adapter): |
---|
| 231 | """Turn string values into real values. |
---|
[6260] | 232 | |
---|
[6273] | 233 | A converter can convert string values for objects that implement a |
---|
| 234 | certain interface into real values based on the given interface. |
---|
| 235 | """ |
---|
[6258] | 236 | |
---|
[6273] | 237 | grok.context(Interface) |
---|
[8214] | 238 | grok.implements(IObjectConverter) |
---|
[6263] | 239 | |
---|
[6273] | 240 | def __init__(self, iface): |
---|
| 241 | self.iface = iface |
---|
[7709] | 242 | # Omit known dictionaries since there is no widget available |
---|
| 243 | # for dictionary schema fields |
---|
[17787] | 244 | self.default_form_fields = form.Fields(iface).omit( |
---|
| 245 | 'description_dict', 'frontpage_dict') |
---|
[6273] | 246 | return |
---|
[6263] | 247 | |
---|
[8214] | 248 | def fromStringDict(self, data_dict, context, form_fields=None, |
---|
| 249 | mode='create'): |
---|
[6273] | 250 | """Convert values in `data_dict`. |
---|
[6263] | 251 | |
---|
[6273] | 252 | Converts data in `data_dict` into real values based on |
---|
| 253 | `context` and `form_fields`. |
---|
[6263] | 254 | |
---|
[6273] | 255 | `data_dict` is a mapping (dict) from field names to values |
---|
| 256 | represented as strings. |
---|
[6263] | 257 | |
---|
[6273] | 258 | The fields (keys) to convert can be given in optional |
---|
| 259 | `form_fields`. If given, form_fields should be an instance of |
---|
| 260 | :class:`zope.formlib.form.Fields`. Suitable instances are for |
---|
| 261 | example created by :class:`grok.AutoFields`. |
---|
[6263] | 262 | |
---|
[6273] | 263 | If no `form_fields` are given, a default is computed from the |
---|
| 264 | associated interface. |
---|
[6263] | 265 | |
---|
[6273] | 266 | The `context` can be an existing object (implementing the |
---|
| 267 | associated interface) or a factory name. If it is a string, we |
---|
| 268 | try to create an object using |
---|
| 269 | :func:`zope.component.createObject`. |
---|
[6263] | 270 | |
---|
[6273] | 271 | Returns a tuple ``(<FIELD_ERRORS>, <INVARIANT_ERRORS>, |
---|
| 272 | <DATA_DICT>)`` where |
---|
[6263] | 273 | |
---|
[6273] | 274 | ``<FIELD_ERRORS>`` |
---|
| 275 | is a list of tuples ``(<FIELD_NAME>, <ERROR>)`` for each |
---|
| 276 | error that happened when validating the input data in |
---|
| 277 | `data_dict` |
---|
[6258] | 278 | |
---|
[6273] | 279 | ``<INVARIANT_ERRORS>`` |
---|
| 280 | is a list of invariant errors concerning several fields |
---|
[6258] | 281 | |
---|
[6273] | 282 | ``<DATA_DICT>`` |
---|
| 283 | is a dict with the values from input dict converted. |
---|
| 284 | |
---|
[8215] | 285 | If mode is ``'create'`` or ``'update'`` then some additional |
---|
| 286 | filtering applies: |
---|
| 287 | |
---|
| 288 | - values set to DELETION_MARKER are set to missing_value (or |
---|
| 289 | default value if field is required) and |
---|
| 290 | |
---|
| 291 | - values set to IGNORE_MARKER are ignored and thus not part of |
---|
| 292 | the returned ``<DATA_DICT>``. |
---|
| 293 | |
---|
[6273] | 294 | If errors happen, i.e. the error lists are not empty, always |
---|
| 295 | an empty ``<DATA_DICT>`` is returned. |
---|
| 296 | |
---|
[7597] | 297 | If ``<DATA_DICT>`` is non-empty, there were no errors. |
---|
[6273] | 298 | """ |
---|
[6263] | 299 | if form_fields is None: |
---|
[6273] | 300 | form_fields = self.default_form_fields |
---|
[6263] | 301 | |
---|
[6273] | 302 | request = TestRequest(form={}) |
---|
[8214] | 303 | new_data = dict() |
---|
[6273] | 304 | for key, val in data_dict.items(): |
---|
[16818] | 305 | val = val.strip() |
---|
[7932] | 306 | field = form_fields.get(key, None) |
---|
| 307 | if field is not None: |
---|
| 308 | # let adapters to the respective schema fields do the |
---|
| 309 | # further fake-request processing |
---|
| 310 | schema_field = field.interface[field.__name__] |
---|
| 311 | field_converter = IFieldConverter(schema_field) |
---|
[8215] | 312 | if mode in ('update', 'create'): |
---|
| 313 | if val == IGNORE_MARKER: |
---|
| 314 | continue |
---|
| 315 | elif val == DELETION_MARKER: |
---|
[8214] | 316 | val = schema_field.missing_value |
---|
| 317 | if schema_field.required: |
---|
| 318 | val = schema_field.default |
---|
| 319 | new_data[key] = val |
---|
| 320 | continue |
---|
[7932] | 321 | request.form.update( |
---|
| 322 | field_converter.request_data( |
---|
| 323 | key, val, schema_field, 'form.%s' % key) |
---|
| 324 | ) |
---|
| 325 | else: |
---|
| 326 | request.form['form.%s' % key] = val |
---|
[6273] | 327 | |
---|
[6263] | 328 | obj = context |
---|
| 329 | if isinstance(context, basestring): |
---|
[8335] | 330 | # If we log initialization transitions in the __init__ |
---|
| 331 | # method of objects, a second (misleading) log entry |
---|
| 332 | # will be created here. |
---|
[6263] | 333 | obj = createObject(context) |
---|
[6273] | 334 | |
---|
| 335 | widgets = form.setUpInputWidgets( |
---|
[6263] | 336 | form_fields, 'form', obj, request) |
---|
[6273] | 337 | |
---|
| 338 | errors = getWidgetsData(widgets, 'form', new_data) |
---|
| 339 | |
---|
| 340 | invariant_errors = form.checkInvariants(form_fields, new_data) |
---|
[7932] | 341 | |
---|
[6273] | 342 | if errors or invariant_errors: |
---|
| 343 | err_messages = [(key, err.args[0]) for key, err in errors] |
---|
| 344 | invariant_errors = [err.message for err in invariant_errors] |
---|
| 345 | return err_messages, invariant_errors, {} |
---|
| 346 | |
---|
| 347 | return errors, invariant_errors, new_data |
---|