ztfy/utils/catalog.py
changeset 18 f268aaac34e8
child 21 d18c6cb028a8
equal deleted inserted replaced
17:d2e8f6a30505 18:f268aaac34e8
       
     1 ### -*- coding: utf-8 -*- ####################################################
       
     2 ##############################################################################
       
     3 #
       
     4 # Copyright (c) 2008-2009 Thierry Florac <tflorac AT ulthar.net>
       
     5 # All Rights Reserved.
       
     6 #
       
     7 # This software is subject to the provisions of the Zope Public License,
       
     8 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
    10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    12 # FOR A PARTICULAR PURPOSE.
       
    13 #
       
    14 ##############################################################################
       
    15 
       
    16 __docformat__ = "restructuredtext"
       
    17 
       
    18 # import standard packages
       
    19 
       
    20 # import Zope3 interfaces
       
    21 from zope.annotation.interfaces import IAnnotations
       
    22 from zope.app.catalog.interfaces import ICatalog
       
    23 from zope.app.container.interfaces import IContainer
       
    24 from zope.app.file.interfaces import IFile
       
    25 from zope.app.intid.interfaces import IIntIds
       
    26 
       
    27 # import local interfaces
       
    28 
       
    29 # import Zope3 packages
       
    30 from zope.app import zapi
       
    31 
       
    32 # import local packages
       
    33 import request as request_utils
       
    34 
       
    35 from ztfy.utils import _
       
    36 
       
    37 
       
    38 #
       
    39 # IntIds utility functions
       
    40 #
       
    41 
       
    42 def getIntIdUtility(name='', request=None, context=None):
       
    43     """Look for a named IIntIds utility"""
       
    44     if request is None:
       
    45         request = request_utils.getRequest()
       
    46     intids = request_utils.getRequestData('IntIdsUtility::' + name, request)
       
    47     if intids is None:
       
    48         intids = zapi.queryUtility(IIntIds, name, context=context)
       
    49         if intids is not None:
       
    50             request_utils.setRequestData('IntIdsUtility::' + name, intids, request)
       
    51     return intids
       
    52 
       
    53 
       
    54 def getObjectId(object, intids_name='', request=None, context=None):
       
    55     """Look for an object Id as recorded by given IIntIds utility"""
       
    56     if request is None:
       
    57         request = request_utils.getRequest()
       
    58     intids = getIntIdUtility(intids_name, request, context)
       
    59     if intids is not None:
       
    60         return intids.queryId(object)
       
    61     return None
       
    62 
       
    63 
       
    64 def getObject(id, intids_name='', request=None, context=None):
       
    65     """Look for an object recorded by given IIntIds utility and id"""
       
    66     if request is None:
       
    67         request = request_utils.getRequest()
       
    68     intids = getIntIdUtility(intids_name, request, context)
       
    69     if intids is not None:
       
    70         return intids.queryObject(id)
       
    71     return None
       
    72 
       
    73 
       
    74 #
       
    75 # Catalog utility functions
       
    76 #
       
    77 
       
    78 def queryCatalog(name='', context=None):
       
    79     """Look for a registered catalog"""
       
    80     return zapi.queryUtility(ICatalog, name, context=context)
       
    81 
       
    82 
       
    83 def indexObject(object, catalog_name='', index_name='', request=None, context=None):
       
    84     """Index object into a registered catalog"""
       
    85     if request is None:
       
    86         request = request_utils.getRequest()
       
    87     intids = getIntIdUtility('', request, context)
       
    88     if intids is not None:
       
    89         catalog = queryCatalog(catalog_name, context)
       
    90         if catalog is not None:
       
    91             id = intids.register(object)
       
    92             if index_name:
       
    93                 catalog[index_name].index_doc(id, object)
       
    94             else:
       
    95                 catalog.index_doc(id, object)
       
    96             return True
       
    97     return False
       
    98 
       
    99 
       
   100 def unindexObject(object, catalog_name='', index_name='', request=None, context=None):
       
   101     """Remove object from a registered catalog"""
       
   102     if request is None:
       
   103         request = request_utils.getRequest()
       
   104     id = getObjectId(object, '', request, context)
       
   105     if id is not None:
       
   106         catalog = queryCatalog(catalog_name, context)
       
   107         if catalog is not None:
       
   108             if index_name:
       
   109                 catalog[index_name].unindex_doc(id)
       
   110             else:
       
   111                 catalog.unindex_doc(id)
       
   112             return True
       
   113     return False
       
   114 
       
   115 
       
   116 def _indexObject(object, intids, catalogs):
       
   117     """Index object data into given set of catalogs"""
       
   118     id = intids.register(object)
       
   119     for catalog in catalogs:
       
   120         catalog.index_doc(id, object)
       
   121 
       
   122 def _indexObjectValues(object, intids, catalogs):
       
   123     """Index object values into given set of catalogs"""
       
   124     container = IContainer(object, None)
       
   125     if container is not None:
       
   126         for subobject in container.values():
       
   127             _indexAllObject(subobject, intids, catalogs)
       
   128 
       
   129 def _indexObjectAnnotations(object, intids, catalogs):
       
   130     """Index object annotations into given set of catalogs"""
       
   131     annotations = IAnnotations(object, None)
       
   132     if annotations is not None:
       
   133         keys = annotations.keys()
       
   134         for key in keys:
       
   135             _indexAllObject(annotations[key], intids, catalogs)
       
   136             file = IFile(annotations[key], None)
       
   137             if file is not None:
       
   138                 _indexObject(file, intids, catalogs)
       
   139 
       
   140 def _indexAllObject(object, intids, catalogs):
       
   141     """Index object, object values and annotations into given set of catalogs"""
       
   142     _indexObject(object, intids, catalogs)
       
   143     _indexObjectValues(object, intids, catalogs)
       
   144     _indexObjectAnnotations(object, intids, catalogs)
       
   145 
       
   146 def indexAllObjectValues(object, context=None):
       
   147     """Reindex a whole container properties and contents (including annotations) into site's catalogs"""
       
   148     if context is None:
       
   149         context = object
       
   150     intids = zapi.queryUtility(IIntIds, context=context)
       
   151     if intids is not None:
       
   152         catalogs = zapi.getAllUtilitiesRegisteredFor(ICatalog, context)
       
   153         if catalogs:
       
   154             _indexAllObject(object, intids, catalogs)