gpt4 book ai didi

plone - 扩展 Plone 控制面板表单

转载 作者:行者123 更新时间:2023-12-04 19:51:59 27 4
gpt4 key购买 nike

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

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

最佳答案

是的,这是可能的,我曾经和一个写了 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 表单中使用代理。我假设您正在使用 plone.registry 中的 RegistryEditForm:

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

...

您可以找到完整代码 here

关于plone - 扩展 Plone 控制面板表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40585762/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com