2

我在处理在 Plone 5 上以编程方式创建一些敏捷内容时遇到问题。任何指针将不胜感激。

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

还是我缺少文档中的一些先决条件?

但是,此代码改编自 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>)

那是从这些其他文档中尝试另一个示例

然而,似乎有无数种方法可以剥去这只猫的皮!尝试所有:

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。

4

1 回答 1

2

您需要为您的克隆站点设置组件注册表。如果您使用的是调试控制台。

from zope.component.hooks import setSite
setSite(portal)

如果没有这个,您的组件注册表将是空的。

于 2016-02-18T12:43:35.160 回答