Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top