source: main/waeup.kofa/trunk/src/waeup/kofa/catalog.py @ 9767

Last change on this file since 9767 was 9767, checked in by uli, 12 years ago

Add more reuseable query components.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.1 KB
Line 
1## $Id: catalog.py 9767 2012-12-05 06:03:12Z uli $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""Components to help cataloging and searching objects.
19"""
20import grok
21from hurry.query.interfaces import IQuery
22from hurry.query.query import Query
23from zope.catalog.catalog import ResultSet
24from zope.catalog.interfaces import ICatalog
25from zope.component import getUtility, queryUtility
26from zope.interface import implementer
27from zope.intid.interfaces import IIntIds
28from waeup.kofa.interfaces import (
29    IQueryResultItem, IFilteredQuery, IFilteredCatalogQuery)
30
31# not yet used
32@implementer(IQuery)
33class KofaQuery(Query):
34    """A hurry.query-like query that supports also ``apply``.
35    """
36    def apply(self, query):
37        """Get a catalog's BTree set of intids conforming to a query.
38        """
39        return query.apply()
40
41    def searchResults(self, query):
42        """Get a set of ZODB objects conforming to a query.
43        """
44        results = self.apply(query)
45        if results is not None:
46            uidutil = getUtility(IIntIds)
47            results = ResultSet(results, uidutil)
48        return results
49
50grok.global_utility(KofaQuery)
51
52# not yet used
53@implementer(IQueryResultItem)
54class QueryResultItem(object):
55    url = None
56    title = None
57    description = None
58
59    def __init__(self, context, view):
60        self.context = context
61        self.url = view.url(context)
62        self.title = context.title
63        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 TracBrowser for help on using the repository browser.