source: main/waeup.sirp/branches/henrik-bootstrap/src/waeup/sirp/accesscodes/catalog.py @ 9819

Last change on this file since 9819 was 7195, checked in by Henrik Bettermann, 13 years ago

More copyright adjustments.

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1## $Id: catalog.py 7195 2011-11-25 07:34:07Z 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"""
20import grok
21from grok import index
22from hurry.query import Eq, Text
23from hurry.query.query import Query
24from zope.index.text.parsetree import ParseError
25from waeup.sirp.interfaces import IUniversity, IQueryResultItem
26from waeup.sirp.accesscodes.interfaces import IAccessCode
27
28class 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
40class 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
55def 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        results = Query().searchResults(
65            Eq(('accesscodes_catalog', searchtype), int(query)))
66    else:
67        results = Query().searchResults(
68            Eq(('accesscodes_catalog', searchtype), query))
69    for result in results:
70        hitlist.append(AccessCodeQueryResultItem(result, view=view))
71    return hitlist
Note: See TracBrowser for help on using the repository browser.