source: main/waeup.sirp/trunk/src/waeup/sirp/students/catalog.py @ 6778

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

Remove index package. We don't need UniqueFields? since they do not really help us.

  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1"""Cataloging and searching components for students.
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.students.interfaces import IStudent
10
11class StudentIndexes(grok.Indexes):
12    """A catalog for students.
13    """
14    grok.site(IUniversity)
15    grok.name('students_catalog')
16    grok.context(IStudent)
17
18    student_id = index.Field(attribute='student_id')
19    name = index.Text(attribute='name')
20    reg_number = index.Field(attribute='reg_number')
21    matric_number = index.Field(attribute='matric_number')
22    state = index.Field(attribute='state')
23
24class StudentQueryResultItem(object):
25    grok.implements(IQueryResultItem)
26
27    title = u'Student Query Item'
28    description = u'Some students found in a search'
29
30    def __init__(self, context, view):
31        self.context = context
32        self.url = view.url(context)
33        self.student_id = context.student_id
34        self.name = context.name
35        self.reg_number = context.reg_number
36        self.matric_number = context.matric_number
37        self.state = context.state
38
39def search(query=None, searchtype=None, view=None):
40    hitlist = []
41    #if not query:
42    #    view.flash('Empty search string.')
43    #    return
44    if searchtype in ('name',):
45        results = Query().searchResults(
46            Text(('students_catalog', searchtype), query))
47    else:
48        # Temporary solution to display all students added
49        if query == '*':
50            from zope.component import queryUtility
51            from zope.catalog.interfaces import ICatalog
52            cat = queryUtility(ICatalog, name='students_catalog')
53            results = cat.searchResults(student_id=(None, None))
54        else:
55            results = Query().searchResults(
56                Eq(('students_catalog', searchtype), query))
57    for result in results:
58        hitlist.append(StudentQueryResultItem(result, view=view))
59    return hitlist
Note: See TracBrowser for help on using the repository browser.