循环浏览一堆文件夹式内容,这些内容本身可能就是文件夹式的?

发布于 2024-11-10 14:05:22 字数 1084 浏览 2 评论 0原文

假设我有一个内容类型“文件夹”,其中包含 4 个项目。

+ MyObject
 - Child1
 - Child2
 - Child3
 + Child4
  - Child5
 - Child6

假设我有另一种内容类型(我们称之为 Alias)。这个Alias主要是对另一个对象的引用,但是是文件夹式的:它可以包含一堆其他别名。我将在下面的树表示中使用 --> 来指示此引用。(“Reference”主要是一个名为“reference”的属性,它从目标对象接收 UID)。

假设 MyAlias 现在引用我的 MyObject

+ MyAlias --> MyObject
 - (Nothing)

引用 MyObject 时,MyAlias 不知道 MyObject 是一个文件夹,因此内部 MyAlias 子级不存在。我需要循环遍历每个人,并在 MyAlias 中手动创建一个别名,这是对 MyObject 子项的引用(具有相同的结构)。一棵小树显示应该发生什么:

+ MyAlias --> MyObject
 - Alias --> Child1
 - Alias --> Child2
 - Alias --> Child3
 + Alias --> Child4
  - Alias --> Child5
 - Alias --> Child6

我想知道迭代 MyObject 项的最佳方法,并使用某种循环并使用 invokeFactory< 与其他对象创建相同的结构/code> 在订阅者中。最后,我将同时存在两棵树:一棵是实际的文件夹和子项,另一棵是对同一文件夹和子项的引用。

(总结:类似于collective.alias,但以一种非常原始的形式,只是文件夹文档,因为我不能使用collective.alias。)

Suppose I have a content type, Folder, with 4 items.

+ MyObject
 - Child1
 - Child2
 - Child3
 + Child4
  - Child5
 - Child6

Suppose I have another content type (let's call it Alias). This Alias is mainly a reference to another object but folderish: it can contain a bunch of other aliases. I'm going to use --> to indicate this reference in the following tree representations.("Reference" is mainly an attribute called "reference" that receives the UID from the target object).

Suppose MyAlias now references my MyObject.

+ MyAlias --> MyObject
 - (Nothing)

When referencing to MyObject, MyAlias doesn't know that MyObject is a Folder, so the internal MyAlias children don't exist. I need to loop through everyone, and create, manually, an Alias inside MyAlias, that is a reference (having the same structure) to MyObject children. A little tree showing what should happen:

+ MyAlias --> MyObject
 - Alias --> Child1
 - Alias --> Child2
 - Alias --> Child3
 + Alias --> Child4
  - Alias --> Child5
 - Alias --> Child6

I would like to know the best way to iterate through MyObject items, and create the same structure with another objects, using some kind of loop and using invokeFactory in a subscriber. In the end, I would have BOTH trees existing: one of the actual Folder and children, and another of references to this same Folder and children.

(Summarizing: something like collective.alias, but in a really primitive form, just folders an documents, since I can't use collective.alias.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

那小子欠揍 2024-11-17 14:05:22

最优雅和 Pythonic 的解决方案是编写一个递归生成器。假设这是一个方法:

def iter_preorder(self):
    yield self
    # check for folderishness here if a non-folderish
    # node may have children as well
    for x in self.children:
        for y in x.iter_preorder():
            yield y

那么这样

for x in tree.iter_preorder():
    do_action(x)

,您实际上不必将操作包装到函数/可调用中,并且没有控制反转。

The most elegant and Pythonic solution is to write a recursive generator. Assuming this is a method:

def iter_preorder(self):
    yield self
    # check for folderishness here if a non-folderish
    # node may have children as well
    for x in self.children:
        for y in x.iter_preorder():
            yield y

Then

for x in tree.iter_preorder():
    do_action(x)

This way, you don't actually have to wrap your action into a function/callable and there's no inversion of control.

疏忽 2024-11-17 14:05:22

递归可能有帮助

def do_action(child):
    if child.isfolder():
        for i in child:
            do_action(i)
    else:
        child.setSomething()

do_action(MyObject)

Recursivity may help

def do_action(child):
    if child.isfolder():
        for i in child:
            do_action(i)
    else:
        child.setSomething()

do_action(MyObject)
油焖大侠 2024-11-17 14:05:22

为什么不使用目录?该目录的存在是为了快速、安全地为您解决这些问题,以便您可以专注于重要的事情。因此,要查找某个路径中的所有对象:

ct = getToolByName(context, 'portal_catalog')
path = '/'.join(context.getPhysicalPath())
brains = ct.searchResults(path=path)

您当然可以在查询中过滤更多内容。然后,

for brain in brains:
    obj = brain.getObject()
    obj.setSomething()

目录是您的朋友,阅读如何使用它

Why would you not use the catalog? The catalog exists for solving these problems fast and securely for you, so that you can concentrate on things that matter. So, to find all the objects in some path:

ct = getToolByName(context, 'portal_catalog')
path = '/'.join(context.getPhysicalPath())
brains = ct.searchResults(path=path)

You can of course filter more in your query. Then,

for brain in brains:
    obj = brain.getObject()
    obj.setSomething()

The catalog is your friend, read how to use it.

无戏配角 2024-11-17 14:05:22

听起来您可能正在尝试进行内容类型迁移。如果是这种情况,请查看 Products.contentmigration

It sounds like you may be trying to do a content type migration. If that's the case, take a look at Products.contentmigration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文