Plone 4:如何获取短名称中带有连字符的子文件夹的上下文
我正在编写一个 python 脚本,将内容从另一个 CMS 导入到 Plone 4.1 中。由于多种原因,我像这样运行它: bin/instance run path/to/myscript
我的问题是如何获取 ID/短名称中带有连字符的文件夹的正确上下文。例如,从名为 mysite 的 plone 站点的根目录中,我可以使用名为“子文件夹”的文件夹,如下所示:
from Products.CMFCore.utils import getToolByName
urltool = getToolByName(app.mysite, "portal_url")
portal = urltool.getPortalObject()
folder = getattr(portal, 'sub-folder')
但是,如果我随后想要在该子文件夹中创建文件夹或页面,则会引发错误: “AttributeError: sub”
urltool = getToolByName(app.mysite.sub-folder, "portal_url")
portal = urltool.getPortalObject()
在新闻文件夹(没有连字符)上执行相同的操作不会产生错误:
urltool = getToolByName(app.mysite.news, "portal_url")
portal = urltool.getPortalObject()
简单地尝试 Portal.sub-folder 会引发相同的错误。
那么,获取“http://localhost:8080/mysite/sub-folder”的正确上下文的 python 代码是什么,以便我可以成功调用 invokeFactory 方法并在 mysite/sub-folder 中创建文件夹或页面?
如果我需要查找“http://localhost:8080/mysite/sub-folder/2nd-level”的上下文怎么办?
我找到的在线文档似乎只考虑名为“dog”或“news”的文件夹,这些文件夹的 ID/简称中没有连字符。但是,如果您在 Plone 中手动创建这些项目,短名称显然带有连字符,因此必须有一种方法来获取正确的文件夹上下文。
I am writing a python script to import content from another CMS into Plone 4.1. For a number of reasons I am running it like so: bin/instance run path/to/myscript
The question I have is how to get the correct context for a folder with a hyphen in the ID/shortname. For example from the root of a plone site called mysite, I can work with a folder called "sub-folder" like so:
from Products.CMFCore.utils import getToolByName
urltool = getToolByName(app.mysite, "portal_url")
portal = urltool.getPortalObject()
folder = getattr(portal, 'sub-folder')
But if I then want to create a folder or page within that sub-folder, the following throws an error: "AttributeError: sub"
urltool = getToolByName(app.mysite.sub-folder, "portal_url")
portal = urltool.getPortalObject()
And performing the same on the News folder, (which has no hyphen) produces no error:
urltool = getToolByName(app.mysite.news, "portal_url")
portal = urltool.getPortalObject()
Simply trying portal.sub-folder throws the same Error.
So what would be the python code to get the proper context of "http://localhost:8080/mysite/sub-folder" so that I can then successfully call the invokeFactory method and create a folder or page within mysite/sub-folder?
What if I needed to find the context of "http://localhost:8080/mysite/sub-folder/2nd-level" ?
The online documentation I have found seems to only account for folders named dog or news, which have no hyphen in the ID/Shortname. However, if you create these items by hand in Plone, the shortnames obviously have hyphens, and so there must be a way to get the correct folder context.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为如果您使用:
python 认为您正在尝试区分
app.mysite.sub
和folder
。相反,您必须使用以下语法:
或
That's because if you use:
python thinks that you're trying to do a difference between
app.mysite.sub
andfolder
.Instead you have to use this syntax:
or
您已经这样做了:
因此您的文件夹带有连字符,在“文件夹”变量中引用。
您可以使用文件夹变量来调用invokeFactory,然后就完成了。它知道自己在 Plone 中的位置。
您不需要使用portal_url 工具来运行invokeFactory。但如果你想要它,而不是像你写的那样:
你可以这样做:
但是目前(并且可能永远)你甚至不需要这样做,因为子文件夹的portal_url工具与整个网站的工具相同,所以你可以写:
You already do:
So you have the folder with a hyphen right there, referenced in the "folder" variable.
You can use the folder variable to call invokeFactory and you're done. It knows where it is in Plone.
You do not need to get hold of the portal_url tool to run invokeFactory. But if you want it, instead of as you write:
You could do:
However presently (and probably forever) you do not even need to do that, since the portal_url tool for your sub folder is the same one as for the entire site, so you could just write: