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