3

是否可以使用每个插件扩展控制面板视图?

例如
ca.db.core -> 为数据库连接设置
ca.db.person 创建基本字段集/选项卡-> 如果已安装,则为“核心”设置添加一个新的字段集/选项卡,用于人员特定字段/设置
ca.db。 schema -> 如果已安装,还会为 schema.org 字段添加一个新的字段集/选项卡

4

1 回答 1

3

是的,有可能,我曾经和一个写过 bda.plone.shop 插件的人讨论过这个问题。

他们面临同样的问题,并通过使用 ContextProxy 对象解决了这个问题,该对象将不同的模式定义放在一个代理对象中。

恕我直言,使用代理是一种黑客行为,但我不知道更好的解决方案。

代理尝试从模式列表中获取/设置属性。请注意,您需要处理冲突的名称,这意味着如果您在多个模式中具有相同的字段名称。

class ContextProxy(object):

    def __init__(self, interfaces):
        self.__interfaces = interfaces
        alsoProvides(self, *interfaces)

    def __setattr__(self, name, value):
        if name.startswith('__') or name.startswith('_ContextProxy__'):
            return object.__setattr__(self, name, value)

        registry = getUtility(IRegistry)
        for interface in self.__interfaces:
            proxy = registry.forInterface(interface)
            try:
                getattr(proxy, name)
            except AttributeError:
                pass
            else:
                return setattr(proxy, name, value)
        raise AttributeError(name)

    def __getattr__(self, name):
        if name.startswith('__') or name.startswith('_ContextProxy__'):
            return object.__getattr__(self, name)

        registry = getUtility(IRegistry)
        for interface in self.__interfaces:
            proxy = registry.forInterface(interface)
            try:
                return getattr(proxy, name)
            except AttributeError:
                pass

        raise AttributeError(name)

现在您需要在 ControlPanel 表单中使用代理。我假设您正在使用RegistryEditFormfrom plone.registry

class SettingsEditForm(controlpanel.RegistryEditForm):
    schema = ISettings
    label = _(u"Settings")
    description = _(u"")

    # IMPORTANT Note 1 - This is where you hook in your proxy    
    def getContent(self):
        interfaces = [self.schema]  # Base schema from ca.db.core
        interfaces.extend(self.additionalSchemata) # List of additional schemas
        return ContextProxy(interfaces)

    # IMPORTANT Note 2 - You may get the additional schemas dynamically to extend the Settings Form. For example by name (startswith...)
    # In this case they have a separate interface, which marks the relevant interfaces.
    @property
    def additionalSchemata(self):
        registry = getUtility(IRegistry)
        interface_names = set(record.interfaceName for record
                              in registry.records.values())

        for name in interface_names:
            if not name:
                continue

            interface = None
            try:
                interface = resolve(name)
            except ImportError:
                # In case of leftover registry entries of uninstalled Products
                continue

            if ISettingsProvider.providedBy(interface):
                yield interface

     ...

你可以在这里找到完整的代码

于 2016-11-14T10:13:36.480 回答