使用createContentInContain创建内容

2024-10-04 07:33:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试用灵巧的工具createContentInContainer在我的plone站点中创建内容。在

我编写了一个在zopepy实例下运行的脚本,它完成了以下任务:

  • 从SQL表中选择数据。在
  • 创建一个元组列表,该列表镜像产品中定义的自定义内容类型。在

我知道我的方法非常幼稚,但我通过以下方式创建了到应用程序数据库的连接:

storage = FileStorage.FileStorage('.../var/filestorage/Data.fs')
db = DB(storage)
conn = db.open()
dbroot = conn.root()

我尝试通过以下方式创建内容:

^{pr2}$

portal_type以前设置为我的自定义内容类型。item既是传递给内容接口的元组列表(这将抛出Could not adaptTypeError),又是从接口继承的未注册适配器。在

类型的接口注册在profiles/defualt/types中的mysite.Widget.xml中,但脚本不断抛出:

Traceback (most recent call last):
  File "./bin/zopepy", line 345, in <module>
    execfile(__file__)
  File "importdex.py", line 105, in <module>
    createContentInContainer(dbroot['Application']['myapp']['existingfolder'], portal_type, checkConstraints=False, content=item)
  File "env/mysite/eggs/plone.dexterity-1.0-py2.7.egg/plone/dexterity/utils.py", line 149, in createContentInContainer
    content = createContent(portal_type, **kw)
  File "env/mysite/eggs/plone.dexterity-1.0-py2.7.egg/plone/dexterity/utils.py", line 105, in createContent
    fti = getUtility(IDexterityFTI, name=portal_type)
  File "env/mysite/eggs/zope.component-3.9.5-py2.7.egg/zope/component/_api.py", line 169, in getUtility
    raise ComponentLookupError(interface, name)
zope.component.interfaces.ComponentLookupError: (<InterfaceClass plone.dexterity.interfaces.IDexterityFTI>, 'mysite.Widget')

正如我所提到的,我知道我的方法非常幼稚,我可能应该挨一巴掌。如果我以令人困惑的方式提出我的问题,我很抱歉。在

我的问题是:

  • 我能从zopepy实例化createContentInContainer吗?我操纵的连接是否足够,或者脚本是否需要在应用程序中运行以继承Dexterity/FTI需要的东西来完成我的要求?在
  • 我需要适配器吗?我已经从grok.Adapter继承并将接口传递给grok.provides和{},但它是否应该基于整个内容架构声明属性?在
  • 元组的列表是任意的。考虑到ZODB的结构,这似乎是应该做的事情。如果我在已注册的适配器中将内容类型的架构声明为属性,那么数据应该被精心设计以符合对象(适配器)的属性,对吗?在

Tags: inpy类型内容列表plonetypeline
1条回答
网友
1楼 · 发布于 2024-10-04 07:33:37

您需要为您的代码设置更多的上下文。例如,Plone站点充当本地组件注册表。在

最好使用bin/instance run [scriptname]命令,它将为您设置数据库连接,并将根对象作为app传递给脚本。在该脚本中,使用以下样板将脚手架的其余部分安装起来:

import transaction
from zope.app.component.hooks import setSite
from Testing.makerequest import makerequest
from AccessControl.SecurityManagement import newSecurityManager

plone_site_id = 'Plone' # Adjust as needed.

app = makerequest(app)
site = app[plone_site_id]
setSite(site)
user = app.acl_users.getUser('admin').__of__(site.acl_users)
newSecurityManager(None, user)

有了这些,您就可以拥有运行代码所需的一切。别忘了最后打电话给transaction.commit()。你的Plone站点可以在局部变量site中访问。在

相关问题 更多 >