1 | ## $Id: catalog.py 7748 2012-03-02 08:31:28Z henrik $ |
---|
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 | ## |
---|
18 | """Cataloging and searching components for access codes. |
---|
19 | """ |
---|
20 | import grok |
---|
21 | from grok import index |
---|
22 | from hurry.query import Eq, Text |
---|
23 | from hurry.query.query import Query |
---|
24 | from zope.index.text.parsetree import ParseError |
---|
25 | from waeup.sirp.interfaces import IUniversity, IQueryResultItem |
---|
26 | from waeup.sirp.accesscodes.interfaces import IAccessCode |
---|
27 | |
---|
28 | class AccessCodeIndexes(grok.Indexes): |
---|
29 | """A catalog for access codes. |
---|
30 | """ |
---|
31 | grok.site(IUniversity) |
---|
32 | grok.name('accesscodes_catalog') |
---|
33 | grok.context(IAccessCode) |
---|
34 | |
---|
35 | code = index.Field(attribute='representation') |
---|
36 | history = index.Text(attribute='history') |
---|
37 | batch_serial = index.Field(attribute='batch_serial') |
---|
38 | state = index.Field(attribute='state') |
---|
39 | |
---|
40 | class AccessCodeQueryResultItem(object): |
---|
41 | grok.implements(IQueryResultItem) |
---|
42 | |
---|
43 | title = u'Access Code Query Item' |
---|
44 | description = u'Some access code found in a search' |
---|
45 | |
---|
46 | def __init__(self, context, view): |
---|
47 | self.context = context |
---|
48 | self.url = view.url(context) |
---|
49 | self.code = context.representation |
---|
50 | self.history = context.history |
---|
51 | self.state = context.state |
---|
52 | self.owner = context.owner |
---|
53 | self.batch_serial = context.batch_serial |
---|
54 | |
---|
55 | def search(query=None, searchtype=None, view=None): |
---|
56 | if not query: |
---|
57 | view.flash('Empty search string.') |
---|
58 | return |
---|
59 | hitlist = [] |
---|
60 | if searchtype == 'history': |
---|
61 | results = Query().searchResults( |
---|
62 | Text(('accesscodes_catalog', searchtype), query)) |
---|
63 | elif searchtype == 'batch_serial': |
---|
64 | try: |
---|
65 | results = Query().searchResults( |
---|
66 | Eq(('accesscodes_catalog', searchtype), int(query))) |
---|
67 | except ValueError: |
---|
68 | return |
---|
69 | else: |
---|
70 | results = Query().searchResults( |
---|
71 | Eq(('accesscodes_catalog', searchtype), query)) |
---|
72 | for result in results: |
---|
73 | hitlist.append(AccessCodeQueryResultItem(result, view=view)) |
---|
74 | return hitlist |
---|