source: main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/catalog.py @ 6550

Last change on this file since 6550 was 6549, checked in by uli, 13 years ago

Make AccessCodeQueryResultItem? really implement the promised
interfaces.

Also change import of grok.index. For some strange reason the catalog
tests do not work if we import grok and then create grok.index
fields. Importing index from grok explicitly, however, works.

File size: 1.9 KB
Line 
1"""Cataloging and searching components for access codes.
2"""
3import grok
4from grok import index
5from hurry.query import Eq, Text
6from hurry.query.query import Query
7from zope.index.text.parsetree import ParseError
8from waeup.sirp.interfaces import IUniversity, IQueryResultItem
9from waeup.sirp.accesscodes.interfaces import IAccessCode
10
11class 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
23class 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
37def search(query=None, searchtype=None, view=None):
38    if not query:
39        view.flash('Empty search string.')
40        return
41    hitlist = []
42    try:
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    except ParseError:
55        view.flash('Search string not allowed.')
56        return
57    return hitlist
58
59
Note: See TracBrowser for help on using the repository browser.