Changeset 9767 for main


Ignore:
Timestamp:
5 Dec 2012, 06:03:12 (12 years ago)
Author:
uli
Message:

Add more reuseable query components.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/catalog.py

    r9217 r9767  
    2222from hurry.query.query import Query
    2323from zope.catalog.catalog import ResultSet
    24 from zope.component import getUtility
     24from zope.catalog.interfaces import ICatalog
     25from zope.component import getUtility, queryUtility
     26from zope.interface import implementer
    2527from zope.intid.interfaces import IIntIds
    26 from waeup.kofa.interfaces import IQueryResultItem
     28from waeup.kofa.interfaces import (
     29    IQueryResultItem, IFilteredQuery, IFilteredCatalogQuery)
    2730
    2831# not yet used
     32@implementer(IQuery)
    2933class KofaQuery(Query):
    3034    """A hurry.query-like query that supports also ``apply``.
    3135    """
    32     grok.implements(IQuery)
    33 
    3436    def apply(self, query):
    3537        """Get a catalog's BTree set of intids conforming to a query.
     
    4951
    5052# not yet used
     53@implementer(IQueryResultItem)
    5154class QueryResultItem(object):
    52     grok.implements(IQueryResultItem)
    5355    url = None
    5456    title = None
     
    6062        self.title = context.title
    6163        self.description = ''
     64
     65@implementer(IFilteredQuery)
     66class FilteredQueryBase(object):
     67    """A filter to find objects that match certain parameters.
     68    """
     69    defaults = dict()
     70
     71    def __init__(self, **kw):
     72        self._kw = dict(self.defaults)
     73        self._kw.update(kw)
     74        return
     75
     76    def query(self, context=None):
     77        err_msg = 'class %s does not implement the query() method.' % (
     78            self.__class__.__name__, )
     79        raise NotImplementedError(err_msg)
     80
     81@implementer(IFilteredCatalogQuery)
     82class FilteredCatalogQueryBase(FilteredQueryBase):
     83    cat_name = None
     84
     85    def query_catalog(self, catalog):
     86        query = dict()
     87        for idx_name, value in self._kw.items():
     88            if not isinstance(value, tuple):
     89                value = (value, value)
     90            query[idx_name] = value
     91        result = catalog.searchResults(**query)
     92        return result
     93
     94    def query(self):
     95        catalog = queryUtility(
     96            ICatalog, name=self.cat_name, default=None)
     97        if catalog is None:
     98            return []
     99        return self.query_catalog(catalog)
Note: See TracChangeset for help on using the changeset viewer.