我有一个界面:
class ISomething(Interface):
something = schema.Dict(
title=u"Something",
description=u"Define something.",
key_type=schema.TextLine(title=u"Some Title"),
value_type=schema.Text(title=u"Some Text"))
用于创建在注册表中保存值的表单(ControlPanelFormWrapper
,RegistryEditForm
)。
在registry.xml
:
<record name="something">
<field type="plone.registry.field.Dict">
<title>Something</title>
<key_type type="plone.registry.field.TextLine" />
<value_type type="plone.registry.field.Text" />
</field>
</record>
它的工作原理:我可以添加键值项{'Some Title': 'Some Text'}
。
我需要修改我的表单以包含多个字段而不是Some text
,但保留Dict
。例如:
{'Some Title': {
'field_1': 'Value 1',
'field_2': 'Value 2'
}
}
我希望这可以工作:
registry = getUtility(IRegistry)
reg_something = registry.get("something")
print reg_something['Some Title']['field_1']
>>> Value 1
那么,如何更改我的界面和注册表记录以便以这种方式更新表单?
答案 0 :(得分:3)
Mark van Lent的一篇文章对此进行了描述: https://www.vlent.nl/weblog/2011/09/07/dict-list-value-ploneappregistry/
相应地调整registry.xml,与您的记录名交换:
<record name="my.package.example">
<field type="plone.registry.field.Dict">
<title>Verification filesnames</title>
<key_type type="plone.registry.field.TextLine">
<title>Key</title>
</key_type>
<value_type type="plone.registry.field.List">
<title>Value list</title>
<value_type type="plone.registry.field.TextLine">
<title>Values</title>
</value_type>
</value_type>
</field>
<value purge="false" />
另见这个问题Luca Fabbri和Gil Forcada各自提供了替代方法,从长远来看可能是真正的节省时间: Plone- How can I create a control panel for a record in registry that is a dictionary type?
答案 1 :(得分:1)
registry.xml
(通过升级步骤导入):
<registry>
<records interface="my.package.something.ISomethingItems">
<record name="mypackage_multiplesomething">
<field type="plone.registry.field.List">
<title>Something Items</title>
<value_type type="collective.z3cform.datagridfield.DictRow">
<schema>my.package.something.ISomething</schema>
</value_type>
</field>
</record>
</records>
</registry>
在something.py
中,只需定义接口:
from collective.z3cform.datagridfield import BlockDataGridFieldFactory
from collective.z3cform.datagridfield.registry import DictRow
from plone import api
from plone.app.registry.browser.controlpanel import ControlPanelFormWrapper
from plone.app.registry.browser.controlpanel import RegistryEditForm
from plone.autoform import directives
from zope import schema
from zope.interface import Interface
from zope.interface import implementer
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary
class ISomething(Interface):
id = schema.ASCIILine(
title=u"Something ID",
description=u"Some description."
)
text = schema.Text(
title=u"A text field",
description=u"Human readable text"
)
url = schema.URI(
title=u"An URL",
description=u"Don't forget http:// or https://"
)
class ISomethingItems(Interface):
# the field is the same used in registry.xml
mypackage_multiplesomething = schema.List(
title=u"Something Items",
description=u"Define something items",
value_type=DictRow(title=u"Something", schema=ISomething)
)
directives.widget(mypackage_multiplesomething=BlockDataGridFieldFactory)
现在我们可以有一个编辑表单(在something.py
中):
class SomethingItemsEditForm(RegistryEditForm):
schema = ISomethingItems
label = u"Something items definition"
class SomethingItemsView(ControlPanelFormWrapper):
""" Something items edit form """
form = SomethingItemsEditForm
定义为浏览器页面(configure.zcml
):
<browser:page
name="something-items-settings"
for="Products.CMFPlone.interfaces.IPloneSiteRoot"
class=".something.SomethingItemsView"
permission="cmf.ManagePortal"
/>
使用api轻松获取注册表中的值:
>>> from plone import api
>>> reg_something_items = api.portal.get_registry_record(
'mypackage_somethingitems', interface=ISomethingItems)
[{'id': 'some id', 'text': 'some text', 'url': 'http://something.com'}, {'id': 'some id other', 'text': 'some text other', 'url': 'http://something-other.com'}]
>>> item = reg_something_items[0]
{'id': 'some id', 'text': 'some text', 'url': 'http://something.com'}
>>> item['id']
some id
>>> item['text']
some text
>>> item['url']
http://something.com
如果您向产品添加了卸载配置文件,最好在其中添加registry.xml
:
<registry>
<record name="my.package.something.ISomethingItems.mypackage_somethingitems"
delete="True" remove="True" />
</registry>
确保卸载后注册表将是干净的。
您可以随时检查SITE/portal_registry
(网站设置 - >配置注册表)中注册表中的值