首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >扩展Plone-Controlpanel表单

扩展Plone-Controlpanel表单
EN

Stack Overflow用户
提问于 2016-11-14 09:45:50
回答 1查看 67关注 0票数 3

是否可以用每个加号扩展Controlpanel-View?

例如

ca.db.core ->为DB连接设置制作基本字段集/选项卡

如果安装了ca.db.person ->,则在“核心”设置中为特定于个人的字段/设置添加一个新的字段集/选项卡

如果安装了ca.db.schema ->,还会为schema.org字段添加一个新的字段集/选项卡。

EN

回答 1

Stack Overflow用户

发布于 2016-11-14 10:13:36

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

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

使用代理是IMHO的一种攻击,但我不知道更好的解决方案。

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

代码语言:javascript
复制
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表单中使用代理。我想你使用的是来自plone.registryplone.registry

代码语言:javascript
复制
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

     ...

您可以找到完整的代码这里

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40585762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档