Ejemplo n.º 1
0
 def get_weight(prop):
     if is_onetoone(prop):
         return 4
     elif is_onetomany(prop):
         return 3
     elif is_manytoone(prop):
         return 2
     elif is_manytomany(prop):
         return 1
     return 0
Ejemplo n.º 2
0
    def factory(cls, prop):
        widget = None
        widget_kw = {}
        factory_widget = None
        cols = getattr(prop, 'columns', [])
        if cls.hint_name:
            if is_relation(prop):
                widget = prop.info.get(cls.hint_name)
            elif cols:
                widget = cols[0].info.get(cls.hint_name)
        if widget:
            if issubclass(widget, NoWidget):
                # We don't want to display this field!
                return None
            if issubclass(widget, FactoryWidget):
                factory_widget = widget
                widget = None

        if widget:
            pass
        elif is_onetomany(prop):
            if not cls.onetomany_widget:
                raise twc.WidgetError(
                    "Cannot automatically create a widget " +
                    "for one-to-many relation '%s'" % prop.key)
            prop_cls = prop.mapper.class_
            edit_link = getattr(prop_cls, 'tws_edit_link', None)
            if cls.add_edit_link:
                widget_kw['link'] = edit_link
            widget_kw.update({
                'entity': prop_cls,
            })
            widget = cls.onetomany_widget
        elif sum([c.primary_key for c in getattr(prop, 'columns', [])]):
            widget = cls.pkey_widget
        elif is_manytoone(prop):
            if not cls.manytoone_widget:
                raise twc.WidgetError(
                    "Cannot automatically create a widget " +
                    "for many-to-one relation '%s'" % prop.key)
            widget_kw = {
                'entity': prop.mapper.class_
            }
            widget = cls.manytoone_widget
        elif is_manytomany(prop):
            # Use the same widget as onetomany
            if not cls.onetomany_widget:
                raise twc.WidgetError(
                    "Cannot automatically create a widget " +
                    "for many-to-many relation '%s'" % prop.key)
            prop_cls = prop.mapper.class_
            edit_link = getattr(prop_cls, 'tws_edit_link', None)
            if cls.add_edit_link:
                widget_kw['link'] = edit_link

            widget = cls.onetomany_widget
            widget_kw.update({
                'id': prop.key,
                'entity': prop_cls,
                'reverse_property_name': get_reverse_property_name(prop),
            })
        elif is_onetoone(prop):
            if not cls.onetoone_widget:
                raise twc.WidgetError(
                    "Cannot automatically create a widget " +
                    "for one-to-one relation '%s'" % prop.key)
            required = required_widget(prop)
            widget = cls.onetoone_widget
            widget_kw = {
                'id': prop.key,
                'entity': prop.mapper.class_,
                'required': required,
                'reverse_property_name': get_reverse_property_name(prop),
                'required_on_parent': (not required),
            }
        elif prop.key in cls.name_widgets:
            widget = cls.name_widgets[prop.key]
        else:
            for t, c in product(cls.type_widgets, cols):
                if isinstance(c.type, t):
                    widget = cls.type_widgets[t]
                    break
            else:
                if not cls.default_widget:
                    raise twc.WidgetError(
                        "Cannot automatically create a widget " +
                        "for '%s'" % prop.key)
                widget = cls.default_widget

        if widget:
            widget_kw['id'] = prop.key
            if factory_widget:
                for k, v in widget._all_params.items():
                    value = getattr(factory_widget, k, None)
                    if value and value != v.default:
                        widget_kw[k] = value
            if 'validator' not in widget_kw and not getattr(widget, 'validator', None) and required_widget(prop):
                widget_kw['validator'] = twc.Required
            widget = widget(**widget_kw)

        return widget