[Zope] [python && zope] folder listings

Oliver Bleutgen myzope@gmx.net
Fri, 13 Sep 2002 11:44:06 +0200


Bas van Ombergen wrote:
> Hi,
> 
>  
> 
> I?m not sure what I?m doing wrong, but what I want is a folder listing, 
> including subfolders.
> 
>  
> 
> I have the following python script called ?dtmllist?:
> 
>  
> 
> list=[]
> 
>  
> 
> for i in context.objectValues():
> 
>     if i.meta_type == 'Folder':
> 
>         list.append(i)
> 
>         list.append(i.dtmllist())
> 
>     elif i.meta_type == 'DTML Document':
> 
>         list.append(i)
> 
>  
> 
> return list
> 
>  
> 
> When I test this script I get all the instances of objects like ?<Folder 
> instance at 8b5b670>, [<DTMLDocument instance at 8d79290>, ?]?.
> 
>  
> 
> So it does see contents of the subdirectories.
> 
>  
> 
> But?
> 
>  
> 
> When I create a dtml document and fill it with this code:
> 
>  
> 
> <dtml-in "dtmllist()">
> 
>     <dtml-var id>  -- <dtml-var title>  -- <dtml-var meta_type>
> 
> </dtml-in>

Well, they may be two problems.
First, if you are using a DTMLDocument (vs. DTMLMethod), it may be that 
the context for you python script is the actual document.
Second, even if that is no problem - because acquisition causes 
context.objectValues() actually to give the objectValues of the folder 
where the DTMLDocument is contained in, your dtml code is wrong:

The list dtmllist() returns is list of zope objects _and_ eventually lists:
[FolderObj,[<List of objects in that Folder], ImageObj, ...]
This structure of nested lists mirrors your structure of nested objects.
So, if you loop through the list with dtml-in, sometimes you get a 
normal zope object pushed on the namespace - where id, title and 
meta_type have a meaning -, but if you get to a list, your method will 
not work, because a list doesn't have an id, title, etc.

You could use something like

 > for i in context.objectValues():
 >
 >     if i.meta_type == 'Folder':
 >
 >         list.append(i)
 >
 >         list = list + i.dtmllist() # We join the lists of subobjects 
to the list instead of appending it as a list entry.
 >     elif i.meta_type == 'DTML Document':
 >
 >         list.append(i)

But the resulting list will not reflect your containment structure, and 
you would be better of using ZopeFind (-> google) anyway IMO.

Or you have to recurse into the list elements which are itself lists in 
dtml, but the you could do the whole thing in dtml, if your python 
script really does only that what you wrote.


HTH,
oliver