1 | """Cataloging and searching components for access codes. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from grok import index |
---|
5 | from hurry.query import Eq, Text |
---|
6 | from hurry.query.query import Query |
---|
7 | from zope.index.text.parsetree import ParseError |
---|
8 | from waeup.sirp.interfaces import IUniversity, IQueryResultItem |
---|
9 | from waeup.sirp.accesscodes.interfaces import IAccessCode |
---|
10 | |
---|
11 | class AccessCodeIndexes(grok.Indexes): |
---|
12 | """A catalog for access codes. |
---|
13 | """ |
---|
14 | grok.site(IUniversity) |
---|
15 | grok.name('accesscodes_catalog') |
---|
16 | grok.context(IAccessCode) |
---|
17 | |
---|
18 | code = index.Field(attribute='representation') |
---|
19 | history = index.Text(attribute='history') |
---|
20 | batch_serial = index.Field(attribute='batch_serial') |
---|
21 | state = index.Field(attribute='state') |
---|
22 | |
---|
23 | class AccessCodeQueryResultItem(object): |
---|
24 | grok.implements(IQueryResultItem) |
---|
25 | |
---|
26 | title = u'Access Code Query Item' |
---|
27 | description = u'Some access code found in a search' |
---|
28 | |
---|
29 | def __init__(self, context, view): |
---|
30 | self.context = context |
---|
31 | self.url = view.url(context) |
---|
32 | self.code = context.representation |
---|
33 | self.history = context.history |
---|
34 | self.state = context.state |
---|
35 | self.batch_serial = context.batch_serial |
---|
36 | |
---|
37 | def search(query=None, searchtype=None, view=None): |
---|
38 | if not query: |
---|
39 | view.flash('Empty search string.') |
---|
40 | return |
---|
41 | hitlist = [] |
---|
42 | if searchtype == 'history': |
---|
43 | results = Query().searchResults( |
---|
44 | Text(('accesscodes_catalog', searchtype), query)) |
---|
45 | elif searchtype == 'batch_serial': |
---|
46 | results = Query().searchResults( |
---|
47 | Eq(('accesscodes_catalog', searchtype), int(query))) |
---|
48 | else: |
---|
49 | results = Query().searchResults( |
---|
50 | Eq(('accesscodes_catalog', searchtype), query)) |
---|
51 | for result in results: |
---|
52 | hitlist.append(AccessCodeQueryResultItem(result, view=view)) |
---|
53 | return hitlist |
---|