以编程方式创建灵巧项目
Creating a dexterity item programmatically
我在 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')
.
这是内置类型。
我似乎无法通过这里。
您需要为克隆站点设置组件注册表。如果您使用的是调试控制台。
from zope.component.hooks import setSite
setSite(portal)
没有它,您的组件注册表将是空的。
我在 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')
.
这是内置类型。
我似乎无法通过这里。
您需要为克隆站点设置组件注册表。如果您使用的是调试控制台。
from zope.component.hooks import setSite
setSite(portal)
没有它,您的组件注册表将是空的。