2

Suppose:

Folder
  - Document
  - Document
  - Folder1
    - Document
    - Document

If I do a portal_catalog using Folder as path, I get:

[<Products.ZCatalog.Catalog.mybrains object at 0xdba8d9c>, 

<Products.ZCatalog.Catalog.mybrains object at 0xdd71234>, 

<Products.ZCatalog.Catalog.mybrains object at 0xdd71324>, 

<Products.ZCatalog.Catalog.mybrains object at 0xdd712fc>, 

<Products.ZCatalog.Catalog.mybrains object at 0xdd71194>]

But I would like to have a portal_catalog that keeps the hierarchical context, like:

{
    "Folder": 
             [
             <Products.ZCatalog.Catalog.mybrains object at 0xdba8d9c>,
<Products.ZCatalog.Catalog.mybrains object at 0xdba8d9c>, 
              {
               "Folder1":[
                          <Products.ZCatalog.Catalog.mybrains object at 0xdba8d9c>, <Products.ZCatalog.Catalog.mybrains object at 0xdba8d9c>
                         ]
              }
             ]
}

So, if it's a folderish type, it's id is a key from a dict.

Is is possible to have a similar data strcuture (that keeps the object's hierarchical structure), or will I have to create my own recursive function (I know the structure above will possibly don't exist, but I think you can get the idea)? I'm thinking about using portal_catalog and brains because of performace issues.

Thanks!

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • 1
    Firstly, you should be careful with this. It's likely you're trying to solve the wrong problem or trying to solve a good problem the wrong way. Feel free to say more about why you're doing this and there may be better answers. – Ross Patterson Aug 19 '11 at 01:27
  • I'm replicating an object structure from a context to another context, but only with some content-types. The second context will not have the same type of objects, it'll have others but with the same id and title. – Somebody still uses you MS-DOS Aug 19 '11 at 01:33
  • But why are you trying to do this? This needs a lot more context. – Ross Patterson Aug 19 '11 at 01:43
  • I cant give more details... :( The fact is, the first context will have a lot of content types that Im interested in ther id/name, that will be used in another context, that will have local permissions, but need to have the same structure. – Somebody still uses you MS-DOS Aug 19 '11 at 01:59

2 Answers2

4

I think you can sort_on the path index. That means your catalog query results will be sorted by hierarchy. Then you can use itertools.groupby in a recursive manner to get an iterator over the structure you describe. The tricky part will be the recursion. You'll want to use a key func for groupby that selects the appropriate portion of brain.getPath() for the current folder depth/level of recursion.

Ross Patterson
  • 5,702
  • 20
  • 38
2

You might also have a look at the sitemap generator, which delivers a nested navigation structure.

Here, a (bit) related link: Navigation portlet: all childen always expanded

Community
  • 1
  • 1
Davi Lima
  • 800
  • 1
  • 6
  • 20