[Zope] Get Access to Object from its record in the ZCatalog

Thierry FLORAC thierry.florac@onf.fr
Fri, 7 Mar 2003 10:20:52 +0100


On Thursday 06 March 2003 23:30, Edward Pollard wrote:
> I'm pulling my hair out here. I'm sure the answer is simple.
>
> How can I get to properties of an object from its ZCatalog entry?
>
> I'm iterating through results, and want to do something special if the
> result is a record for a PDF file. I know how to check the file to see
> if its a PDF, but ZCatalog does not keep track of all the file contents.
> I know there is a way to resolve the record ID into the actual object,
> but I forget how and cannot find a reference to it.
>
> ----
> For clarity, here is some of my code. I am looking for what to use
> instead of ??
> zIndex_Catalog is my Catalog. zIndexFileItem is my CatalogAware wrapper
> ZClass for File.
>
> searchresults = list(context.zIndex_Catalog.searchResults(blah blah
> search params blah blah))
> searchresults.sort( lambda x, y: cmp(x.title, y.title))
> for result in searchresults:
> if ((??.meta_type == 'File') or (??.meta_type == 'zIndexFileItem')):
>      if (??.getContentType == "application/pdf"):
>        PDF Specific Code Here


The method to use on the ZCatalog result record is "getObject()".
So you may have an improved version of your code like this :

  query = {}
  query['param1'] = value1
  query['param2'] = value2
  query['sort_on'] = 'title'
  for result in context.zIndex_Catalog.searchResults (query):
    if result.meta_type in ['File','zIndexFileItem']:
      object = result.getObject()
      if object.getContentType == 'application/pdf':
        <PDF specific code>

This should work (as long as 'meta_type' is in your ZCatalog metadatas).

  Thierry