예제 #1
0
    def setUpWidgets(self, context=None):
        """Set up the widgets using the view's form fields and the context.

        If no context is given, the view's context is used."""
        for field in self.form_fields:
            # Honour the custom_widget value if it was already set.  This is
            # important for some existing forms.
            if field.custom_widget is None:
                widget = getattr(self, 'custom_widget_%s' % field.__name__,
                                 None)
                if widget is not None:
                    if IWidgetFactory.providedBy(widget):
                        field.custom_widget = widget
                    else:
                        # Allow views to save some typing in common cases.
                        field.custom_widget = CustomWidgetFactory(widget)
        if context is None:
            context = self.context
        self.widgets = form.setUpWidgets(self.form_fields,
                                         self.prefix,
                                         context,
                                         self.request,
                                         data=self.initial_values,
                                         adapters=self.adapters,
                                         ignore_request=False)
        for field_name, help_link in self.help_links.iteritems():
            self.widgets[field_name].help_link = help_link
예제 #2
0
    def widget(self, _context, field, **kw):
        attrs = kw
        class_ = attrs.pop("class_", None)
        # Try to do better than accepting the string value by looking through
        # the interfaces and trying to find the field, so that we can use
        # 'fromUnicode()'
        if isinstance(class_, type):
            ifaces = implementedBy(class_)
            for name, value in kw.items():
                for iface in ifaces:
                    if name in iface:
                        attrs[name] = iface[name].fromUnicode(value)
                        break
        if class_ is None:
            # The _default_widget_factory is required to allow the
            # <widget> directive to be given without a "class"
            # attribute.  This can be used to override some of the
            # presentational attributes of the widget implementation.
            class_ = self._default_widget_factory

        # don't wrap a factory into a factory
        if IWidgetFactory.providedBy(class_):
            factory = class_
        else:
            factory = CustomWidgetFactory(class_, **attrs)

        self._widgets[field+'_widget'] = factory
예제 #3
0
def setUpWidget(view,
                name,
                field,
                viewType,
                value=no_value,
                prefix=None,
                ignoreStickyValues=False,
                context=None):
    """Sets up a single view widget.

    The widget will be an attribute of the `view`. If there is already
    an attribute of the given name, it must be a widget and it will be
    initialized with the given `value` if not ``no_value``.

    If there isn't already a `view` attribute of the given name, then a
    widget will be created and assigned to the attribute.
    """
    if context is None:
        context = view.context
    widgetName = name + '_widget'

    # check if widget already exists
    widget = getattr(view, widgetName, None)
    if widget is None:
        # does not exist - create it
        widget = _createWidget(context, field, viewType, view.request)
        setattr(view, widgetName, widget)
    elif IWidgetFactory.providedBy(widget):
        # exists, but is actually a factory - use it to create the widget
        widget = widget(field.bind(context), view.request)
        setattr(view, widgetName, widget)

    # widget must implement IWidget
    if not IWidget.providedBy(widget):
        raise TypeError(
            "Unable to configure a widget for %s - attribute %s does not "
            "implement IWidget" % (name, widgetName))

    if prefix:
        widget.setPrefix(prefix)

    if value is not no_value and (ignoreStickyValues
                                  or not _widgetHasStickyValue(widget)):
        widget.setRenderedValue(value)
예제 #4
0
파일: utility.py 프로젝트: Vinsurya/Plone
def setUpWidget(view, name, field, viewType, value=no_value, prefix=None,
                ignoreStickyValues=False, context=None):
    """Sets up a single view widget.

    The widget will be an attribute of the `view`. If there is already
    an attribute of the given name, it must be a widget and it will be
    initialized with the given `value` if not ``no_value``.

    If there isn't already a `view` attribute of the given name, then a
    widget will be created and assigned to the attribute.
    """
    if context is None:
        context = view.context
    widgetName = name + '_widget'

    # check if widget already exists
    widget = getattr(view, widgetName, None)
    if widget is None:
        # does not exist - create it
        widget = _createWidget(context, field, viewType, view.request)
        setattr(view, widgetName, widget)
    elif IWidgetFactory.providedBy(widget):
        # exists, but is actually a factory - use it to create the widget
        widget = widget(field.bind(context), view.request)
        setattr(view, widgetName, widget)

    # widget must implement IWidget
    if not IWidget.providedBy(widget):
        raise TypeError(
            "Unable to configure a widget for %s - attribute %s does not "
            "implement IWidget" % (name, widgetName))

    if prefix:
        widget.setPrefix(prefix)

    if value is not no_value and (
        ignoreStickyValues or not _widgetHasStickyValue(widget)):
        widget.setRenderedValue(value)