def _process_widgets(form, widgets, modes, newFields): """Update the fields list with widgets """ for fieldName in newFields: fieldInstance = newFields[fieldName] baseName = _bn(fieldInstance) widgetName = widgets.get(baseName, None) widgetMode = modes.get(baseName, fieldInstance.mode) \ or form.mode \ or INPUT_MODE widgetFactory = None if widgetName is not None: if isinstance(widgetName, six.string_types): widgetFactory = resolveDottedName(widgetName) elif IFieldWidget.implementedBy(widgetName): widgetFactory = widgetName elif IParameterizedWidget.providedBy(widgetName): widgetFactory = widgetName if widgetFactory is not None: fieldInstance.widgetFactory[widgetMode] = widgetFactory if baseName in modes: newFields[fieldName].mode = widgetMode
def __call__(self, field, request): __traceback_info__ = ( '{0}, processing:\n' '- field "{1}"\n' '- widget: {2}\n' '- params: {3}\n'.format( self.__class__.__name__, field.__name__, repr(self.widget_factory), self.params ) ) if isinstance(self.widget_factory, six.string_types): __traceback_info__ += '- resolving dotted name\n' self.widget_factory = resolveDottedName(self.widget_factory) if self.widget_factory is None: # use default widget factory for this field type __traceback_info__ += '- using default widget factory\n' widget = getMultiAdapter((field, request), IFieldWidget) elif IWidget.implementedBy(self.widget_factory): __traceback_info__ += '- calling factory, then wrapping with ' \ 'FieldWidget\n' widget = FieldWidget(field, self.widget_factory(request)) elif IFieldWidget.implementedBy(self.widget_factory): __traceback_info__ += '- calling factory\n' widget = self.widget_factory(field, request) for k, v in self.params.items(): setattr(widget, k, v) return widget
def addFieldClass(widget): """Add a class to the widget that is based on the field type name. If the widget does not have field, then nothing is done. """ if IFieldWidget.providedBy(widget): klass = unicode(widget.field.__class__.__name__.lower() + '-field') widget.addClass(klass)
def __init__(self, widget_factory=None, **params): if widget_factory is not None: if not IFieldWidget.implementedBy(widget_factory) \ and not IWidget.implementedBy(widget_factory) \ and not isinstance(widget_factory, basestring): raise TypeError('widget_factory must be an IFieldWidget ' 'or an IWidget') self.widget_factory = widget_factory self.params = params
def __init__(self, widget_factory=None, **params): if widget_factory is not None: if not IFieldWidget.implementedBy(widget_factory) \ and not IWidget.implementedBy(widget_factory) \ and not isinstance(widget_factory, six.string_types): raise TypeError('widget_factory must be an IFieldWidget ' 'or an IWidget') self.widget_factory = widget_factory self.params = params
def update(self): """\ Customized update method. """ # Since we are interested in acquring what the context had first # before we are interested in what was submitted in the request, # we forcibly set ignoreRequest on and continue as normal. Once # that is done, process the request as normal. ignoreRequest = self.ignoreRequest self.ignoreRequest = True super(StorageFileSelectWidget, self).update() # Restore original value. self.ignoreRequest = ignoreRequest if not ignoreRequest: # Extract the request value like how the parent would have # done widget_value = self.extract() if widget_value not in (NO_VALUE, INVALID_VALUE): # Only set the widget with the value from request that # is valid self.value = widget_value # As this deferred self.value assignment from request # is complete, with everything else done, return here. return # The follow are steps that should also have been done inside # step 1.2 in that method. To begin validate that the # applicable conditions are available. if not IFieldWidget.providedBy(self): return # Always set errors, since context could provide an invalid # value due to changing conditions. self.setErrors = True # Now verify that the current widget value (which is extracted # by the data manager) is valid, attempt the conversion back to # the field value using toFieldValue as the reverse method # toWidgetValue does not and cannot raise exceptions due to # how and where it is used by the parent update method. converter = IDataConverter(self) try: converter.toFieldValue(self.value) except (zope.interface.Invalid, ValueError), error: # We have an exception, so we adapt and set the error view. view = zope.component.getMultiAdapter( (error, self.request, self, self.field, self.form, self.context), IErrorViewSnippet) view.update() self.error = view
def __call__(self, field, request): if isinstance(self.widget_factory, basestring): self.widget_factory = resolveDottedName(self.widget_factory) if self.widget_factory is None: # use default widget factory for this field type widget = getMultiAdapter((field, request), IFieldWidget) elif IWidget.implementedBy(self.widget_factory): widget = FieldWidget(field, self.widget_factory(request)) elif IFieldWidget.implementedBy(self.widget_factory): widget = self.widget_factory(field, request) for k, v in self.params.items(): setattr(widget, k, v) return widget
def get_type(self, item): """differ the object typ and return the type as string""" if isinstance(item, dict): return 'dict' elif ICatalogBrain.providedBy(item): return 'brain' elif IContentListingObject.providedBy(item): return 'contentlistingobject' elif IFieldWidget.providedBy(item): return 'widget' elif isinstance(item, Task): return 'globalindex_task' else: raise ValueError("Unknown item type: {!r}".format(item))
def factory(self, field_name=None, widget_class=None, **kw): widgets = {} if field_name is None: # Usage 3 for field_name, widget in kw.items(): if not isinstance(widget, basestring): widget = "%s.%s" % (widget.__module__, widget.__name__) widgets[field_name] = widget else: if widget_class is not None \ and not IFieldWidget.implementedBy(widget_class) \ and not IWidget.implementedBy(widget_class): raise TypeError('widget_class must implement IWidget or IFieldWidget') widgets[field_name] = ParameterizedWidget(widget_class, **kw) return widgets
def factory(self, field_name=None, widget_class=None, **kw): widgets = {} if field_name is None: # Usage 3 for field_name, widget in kw.items(): if not isinstance(widget, basestring): widget = "%s.%s" % (widget.__module__, widget.__name__) widgets[field_name] = widget else: if widget_class is not None \ and not IFieldWidget.implementedBy(widget_class) \ and not IWidget.implementedBy(widget_class): raise TypeError( 'widget_class must implement IWidget or IFieldWidget') widgets[field_name] = ParameterizedWidget(widget_class, **kw) return widgets
def test_widget(self): """ """ with open(os.path.join(FHIR_FIXTURE_PATH, 'Organization.json'), 'r') as f: json_object_str = f.read() request = TestRequest(form={'resource': json_object_str}) json_widget = widget.JSONWidget(request) json_widget.name = 'resource' self.assertTrue(widget.IJSONWidget.providedBy(json_widget)) self.assertIsNone(json_widget.value) json_widget.update() self.assertEqual(json_widget.value, json_object_str) json_field = getFields(ITestToken)['resource'] field_widget = widget.JSONFieldWidget(json_field, request) self.assertTrue(IFieldWidget.providedBy(field_widget))
def read(self, fieldNode, schema, field): name = field.__name__ widgetAttr = fieldNode.get(ns('widget', self.namespace)) mode = fieldNode.get(ns('mode', self.namespace)) omitted = fieldNode.get(ns('omitted', self.namespace)) before = fieldNode.get(ns('before', self.namespace)) after = fieldNode.get(ns('after', self.namespace)) validator = fieldNode.get(ns('validator', self.namespace)) if mode: self._add_interface_values(schema, MODES_KEY, name, mode) if omitted: self._add_interface_values(schema, OMITTED_KEY, name, omitted) if before: self._add_order(schema, name, 'before', before) if after: self._add_order(schema, name, 'after', after) if validator: self._add_validator(field, validator) widgetNode = fieldNode.find(ns('widget', self.namespace)) widget = None if widgetNode is not None: # form:widget element widgetFactory = widgetNode.get('type') if widgetFactory is not None: # resolve immediately so we don't have to each time # form is rendered widgetFactory = resolveDottedName(widgetFactory) widget = ParameterizedWidget(widgetFactory) widgetHandler = widget.getExportImportHandler(field) widgetHandler.read(widgetNode, widget.params) elif widgetAttr is not None: # BBB for old form:widget attributes obj = resolveDottedName(widgetAttr) if not IFieldWidget.implementedBy(obj): raise ValueError( 'IFieldWidget not implemented by {0}'.format(obj) ) widget = widgetAttr if widget is not None: self._add(schema, WIDGETS_KEY, name, widget)
def read(self, fieldNode, schema, field): name = field.__name__ widgetAttr = fieldNode.get(ns('widget', self.namespace)) mode = fieldNode.get(ns('mode', self.namespace)) omitted = fieldNode.get(ns('omitted', self.namespace)) before = fieldNode.get(ns('before', self.namespace)) after = fieldNode.get(ns('after', self.namespace)) validator = fieldNode.get(ns('validator', self.namespace)) if mode: self._add_interface_values(schema, MODES_KEY, name, mode) if omitted: self._add_interface_values(schema, OMITTED_KEY, name, omitted) if before: self._add_order(schema, name, 'before', before) if after: self._add_order(schema, name, 'after', after) if validator: self._add_validator(field, validator) widgetNode = fieldNode.find(ns('widget', self.namespace)) widget = None if widgetNode is not None: # form:widget element widgetFactory = widgetNode.get('type') if widgetFactory is not None: # resolve immediately so we don't have to each time # form is rendered widgetFactory = resolveDottedName(widgetFactory) widget = ParameterizedWidget(widgetFactory) widgetHandler = widget.getExportImportHandler(field) widgetHandler.read(widgetNode, widget.params) elif widgetAttr is not None: # BBB for old form:widget attributes obj = resolveDottedName(widgetAttr) if not IFieldWidget.implementedBy(obj): raise ValueError( 'IFieldWidget not implemented by {0}'.format(obj)) widget = widgetAttr if widget is not None: self._add(schema, WIDGETS_KEY, name, widget)
def __call__(self, field, request): __traceback_info__ = ('{0}, processing:\n' '- field "{1}"\n' '- widget: {2}\n' '- params: {3}\n'.format( self.__class__.__name__, field.__name__, repr(self.widget_factory), self.params)) if isinstance(self.widget_factory, six.string_types): __traceback_info__ += '- resolving dotted name\n' self.widget_factory = resolveDottedName(self.widget_factory) if self.widget_factory is None: # use default widget factory for this field type __traceback_info__ += '- using default widget factory\n' widget = getMultiAdapter((field, request), IFieldWidget) elif IWidget.implementedBy(self.widget_factory): __traceback_info__ += '- calling factory, then wrapping with ' \ 'FieldWidget\n' widget = FieldWidget(field, self.widget_factory(request)) elif IFieldWidget.implementedBy(self.widget_factory): __traceback_info__ += '- calling factory\n' widget = self.widget_factory(field, request) for k, v in self.params.items(): setattr(widget, k, v) return widget
def testCaptchaWidgetInterface(self): self.assertEqual(IFieldWidget.implementedBy(CaptchaWidgetFactory), True)