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

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

Add matriculation number to student's base data.

Registration and matriculation numbers must be unique. Use UniqueField? indexes in catalog for both.

Test fails, I don't know why.

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