1 | """Components to help cataloging and searching objects. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from grok import index |
---|
5 | from hurry.query import Eq |
---|
6 | from hurry.query.interfaces import IQuery |
---|
7 | from hurry.query.query import Query |
---|
8 | from zope.catalog.catalog import ResultSet |
---|
9 | from zope.component import getUtility |
---|
10 | from zope.intid.interfaces import IIntIds |
---|
11 | |
---|
12 | from waeup.sirp.app import University |
---|
13 | from waeup.sirp.interfaces import IQueryResultItem |
---|
14 | from waeup.sirp.student.interfaces import IStudent |
---|
15 | |
---|
16 | class WAeUPQuery(Query): |
---|
17 | """A hurry.query-like query that supports also ``apply``. |
---|
18 | """ |
---|
19 | grok.implements(IQuery) |
---|
20 | |
---|
21 | def apply(self, query): |
---|
22 | """Get a catalog's BTree set of intids conforming to a query. |
---|
23 | """ |
---|
24 | return query.apply() |
---|
25 | |
---|
26 | def searchResults(self, query): |
---|
27 | """Get a set of ZODB objects conforming to a query. |
---|
28 | """ |
---|
29 | results = self.apply(query) |
---|
30 | if results is not None: |
---|
31 | uidutil = getUtility(IIntIds) |
---|
32 | results = ResultSet(results, uidutil) |
---|
33 | return results |
---|
34 | |
---|
35 | grok.global_utility(WAeUPQuery) |
---|
36 | |
---|
37 | |
---|
38 | class StudentIndexes(grok.Indexes): |
---|
39 | grok.site(University) |
---|
40 | grok.name('students_catalog') |
---|
41 | grok.context(IStudent) |
---|
42 | |
---|
43 | name = grok.index.Field(attribute='name') |
---|
44 | |
---|
45 | class QueryResultItem(object): |
---|
46 | grok.implements(IQueryResultItem) |
---|
47 | url = None |
---|
48 | title = None |
---|
49 | description = None |
---|
50 | |
---|
51 | def __init__(self, context, view): |
---|
52 | self.context = context |
---|
53 | self.url = view.url(context) |
---|
54 | self.title = context.title |
---|
55 | self.description = '' |
---|
56 | |
---|
57 | def search_context(query): |
---|
58 | result = Query().searchResults( |
---|
59 | Eq(('students_catalog', 'name'), query) |
---|
60 | ) |
---|
61 | return result |
---|