src/pyams_catalog/utils.py
changeset 5 1f95776705d9
child 6 5a5b6b43f977
equal deleted inserted replaced
4:1f4b299e041c 5:1f95776705d9
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 from zope.keyreference.interfaces import NotYet
       
    13 
       
    14 __docformat__ = 'restructuredtext'
       
    15 
       
    16 
       
    17 # import standard library
       
    18 
       
    19 # import interfaces
       
    20 from hypatia.interfaces import ICatalog
       
    21 from zope.intid.interfaces import IIntIds
       
    22 
       
    23 # import packages
       
    24 from pyams_utils.registry import query_utility
       
    25 
       
    26 
       
    27 def index_object(obj, catalog, ignore_notyet=False):
       
    28     """Index given object into catalog"""
       
    29     intids = query_utility(IIntIds)
       
    30     if intids is not None:
       
    31         try:
       
    32             object_id = intids.register(obj)
       
    33         except NotYet:
       
    34             if not ignore_notyet:
       
    35                 raise
       
    36         else:
       
    37             if isinstance(catalog, str):
       
    38                 catalog = query_utility(ICatalog, name=catalog)
       
    39             if catalog is not None:
       
    40                 catalog.index_doc(object_id, obj)
       
    41 
       
    42 
       
    43 def reindex_object(obj, catalog):
       
    44     """Reindex given object into catalog"""
       
    45     intids = query_utility(IIntIds)
       
    46     if intids is not None:
       
    47         object_id = intids.queryId(obj)
       
    48         if object_id is not None:
       
    49             if isinstance(catalog, str):
       
    50                 catalog = query_utility(ICatalog, name=catalog)
       
    51             if catalog is not None:
       
    52                 catalog.reindex_doc(object_id, obj)
       
    53 
       
    54 
       
    55 def unindex_object(obj, catalog):
       
    56     """Unindex given object from catalog"""
       
    57     intids = query_utility(IIntIds)
       
    58     if intids is not None:
       
    59         object_id = intids.queryId(obj)
       
    60         if object_id is not None:
       
    61             if isinstance(catalog, str):
       
    62                 catalog = query_utility(ICatalog, name=catalog)
       
    63             if catalog is not None:
       
    64                 catalog.unindex_doc(object_id)