[7193] | 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 | ## |
---|
[5090] | 18 | """Components to help cataloging and searching objects. |
---|
| 19 | """ |
---|
[3521] | 20 | import grok |
---|
[5090] | 21 | from hurry.query.interfaces import IQuery |
---|
| 22 | from hurry.query.query import Query |
---|
| 23 | from zope.catalog.catalog import ResultSet |
---|
[9767] | 24 | from zope.catalog.interfaces import ICatalog |
---|
| 25 | from zope.component import getUtility, queryUtility |
---|
| 26 | from zope.interface import implementer |
---|
[5090] | 27 | from zope.intid.interfaces import IIntIds |
---|
[9767] | 28 | from waeup.kofa.interfaces import ( |
---|
| 29 | IQueryResultItem, IFilteredQuery, IFilteredCatalogQuery) |
---|
[3521] | 30 | |
---|
[6447] | 31 | # not yet used |
---|
[9767] | 32 | @implementer(IQuery) |
---|
[7819] | 33 | class KofaQuery(Query): |
---|
[6207] | 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() |
---|
[5090] | 40 | |
---|
[6207] | 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 |
---|
[5090] | 49 | |
---|
[7819] | 50 | grok.global_utility(KofaQuery) |
---|
[5090] | 51 | |
---|
[6447] | 52 | # not yet used |
---|
[9767] | 53 | @implementer(IQueryResultItem) |
---|
[4789] | 54 | class QueryResultItem(object): |
---|
[6207] | 55 | url = None |
---|
| 56 | title = None |
---|
| 57 | description = None |
---|
[6116] | 58 | |
---|
[6207] | 59 | def __init__(self, context, view): |
---|
| 60 | self.context = context |
---|
| 61 | self.url = view.url(context) |
---|
| 62 | self.title = context.title |
---|
| 63 | self.description = '' |
---|
[9767] | 64 | |
---|
| 65 | @implementer(IFilteredQuery) |
---|
| 66 | class 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) |
---|
| 82 | class 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) |
---|