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.owner = context.owner |
---|
36 | self.batch_serial = context.batch_serial |
---|
37 | |
---|
38 | def search(query=None, searchtype=None, view=None): |
---|
39 | if not query: |
---|
40 | view.flash('Empty search string.') |
---|
41 | return |
---|
42 | hitlist = [] |
---|
43 | if searchtype == 'history': |
---|
44 | results = Query().searchResults( |
---|
45 | Text(('accesscodes_catalog', searchtype), query)) |
---|
46 | elif searchtype == 'batch_serial': |
---|
47 | results = Query().searchResults( |
---|
48 | Eq(('accesscodes_catalog', searchtype), int(query))) |
---|
49 | else: |
---|
50 | results = Query().searchResults( |
---|
51 | Eq(('accesscodes_catalog', searchtype), query)) |
---|
52 | for result in results: |
---|
53 | hitlist.append(AccessCodeQueryResultItem(result, view=view)) |
---|
54 | return hitlist |
---|