Getting folder listings
One of the best ways of killing the performance of a Plone site is to wake up all the content objects in a folder for no good reason. The old navigation tree was legendary for this. With Plone 2.1 and ExtendedPathIndex, those days are gone.
Instead of using contentVales() (or worse, objectValues()), you should always use a catalog query to get folder contents. Thanks to ExtendedPathIndex, you can limit the depth of a query to only include direct children of a path, using the depth modifier, and you can get objects in ordered folders returned in the correct order.
If you are in custom file-system code, you can do something like:
catalog = getToolByName(self, 'portal_catalog')
results = catalog.searchResults(path = {'query' : '/'.join(self.getPhysicalPath()),
'depth' : 1 },
sort_on = 'getObjPositionInParent',
)
The script getFolderContents exists to make getting folder contents in page templates and other scripts as easy as possible. RichDocument uses it in its widgets and view templates whenever it needs to iterate over its contained images and attachments. getFolderContents takes the following parameters:
- contentFilter
- a dict with extra parameters to pass to the query. For example, RichDocument passes
contentFilter={"portal_type" : ("FileAttachment",)}to only get file attachments inwidget_attachmentsmanager. - batch and b_size
- Used if you are using Plone's batching macro to batch the returned results. See the
folder_listingtemplate for an example. - full_objects
- Pass True to this parameter to get the actual objects returned instead of a catalog result set. This won't help you with performance, but sometimes it's unavoidable. For example,
richdocument_view_preview, it is not possible to render the scaled images using catalog brains (since Zope has to find the image objects to send them to the browser anyway, this is not so much of a problem).
with plone 3
results = catalog.searchResults(path = {'query' : '/'.join(self.context.getPhysicalPath()),
'depth' : 1 },
sort_on = 'getObjPositionInParent',
portal_type=['Folder'],
)