我在处理如何在 Plone 5 上以编程方式创建一些 Dexterity 内容时遇到问题。非常感谢任何指点。
从 client2 debug 开始我运行以下命令:
from plone import api
portal = api.portal.get()
这很快就失败了:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/opt/plone/buildout-cache/eggs/plone.api-1.4.7-py2.7.egg/plone/api/portal.py", line 65, in get
"Unable to get the portal object. More info on "
CannotGetPortalError: Unable to get the portal object. More info on https://ploneapi.readthedocs.org/en/latest/api/exceptions.html#plone.api.exc.CannotGetPortalError
还是我缺少来自 the docs 的一些先决条件? ?
但是,这段代码改编自 Plone 4 的一些早期代码,一切正常:
from Testing.makerequest import makerequest
from Products.CMFCore.utils import getToolByName
# point to our plone instance (hsfintranet off of the zope root)
portal = makerequest(app.hsfintranet)
siteadmin = portal.acl_users.getUserById('siteadmin')
if siteadmin == None:
print "Could not locate admin account"
exit()
# Switch security to our automated site administrator
siteadmin = siteadmin.__of__(portal.acl_users)
newSecurityManager(None, siteadmin)
# Find the staff directory
allfolder = getattr(portal, 'all', None)
# Did we find the all folder?
if allfolder == None:
print "Could not locate the 'all' folder"
exit()
staffdir = getattr(allfolder, 'staff-directory', None)
if staffdir == None:
print "Could not locate the staff directory"
exit()
到那个时候效果很好。
portal_types = getToolByName(portal, "portal_types")
# Get the FTI for our Dexterity type
type_info = portal_types.getTypeInfo('employee')
from plone.dexterity.utils import createContentInContainer
检查 type_info 看起来不错
>>> type_info
<DexterityFTI at /hsfintranet/portal_types/employee>
但是item = createContentInContainer(staffdir, type_info, title="Test")失败:
ComponentLookupError: (<InterfaceClass plone.dexterity.interfaces.IDexterityFTI>, <DexterityFTI at /hsfintranet/portal_types/employee>)
那是在尝试来自 these other docs 的另一个例子
然而,给这只猫剥皮的方法似乎有无数种!尝试所有:
item = staffdir.invokeFactory("employee", "test")
item = type_info._constructInstance(staffdir, 'Test')
from Products.CMFPlone.utils import _createObjectByType
item = _createObjectByType("employee", staffdir, 'Test')
所有这些都失败了 ComponentLookupError: (<InterfaceClass zope.component.interfaces.IFactory>, 'employee')
令我震惊的是,即使这样也失败了:
item = api.content.create(container=staffdir,type='Document',title='Test')
与 ComponentLookupError: (<InterfaceClass zope.component.interfaces.IFactory>, 'Document') .
这是一个内置类型。
我似乎无法通过 Go here。
请您参考如下方法:
您需要为您的 plone 站点设置组件注册表。如果您使用的是调试控制台。
from zope.component.hooks import setSite
setSite(portal)
没有这个你的组件注册表将是空的。
