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

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

Replace student's name attribute by fullname attributes to avoid confusion.

  • Property svn:keywords set to Id
File size: 2.8 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.catalog.interfaces import ICatalog
8from zope.component import queryUtility
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    fullname = index.Text(attribute='fullname')
21    reg_number = index.Field(attribute='reg_number')
22    matric_number = index.Field(attribute='matric_number')
23    state = index.Field(attribute='state')
24    certificate = index.Field(attribute='certificate')
25
26class StudentQueryResultItem(object):
27    grok.implements(IQueryResultItem)
28
29    title = u'Student Query Item'
30    description = u'Some student found in a search'
31
32    def __init__(self, context, view):
33        self.context = context
34        self.url = view.url(context)
35        self.student_id = context.student_id
36        self.fullname = context.fullname
37        self.reg_number = context.reg_number
38        self.matric_number = context.matric_number
39        self.state = context.state
40
41def search(query=None, searchtype=None, view=None):
42    hitlist = []
43    if searchtype in ('fullname',):
44        results = Query().searchResults(
45            Text(('students_catalog', searchtype), query))
46    else:
47        # Temporary solution to display all students added
48        if query == '*':
49            cat = queryUtility(ICatalog, name='students_catalog')
50            results = cat.searchResults(student_id=(None, None))
51        else:
52            results = Query().searchResults(
53                Eq(('students_catalog', searchtype), query))
54    for result in results:
55        hitlist.append(StudentQueryResultItem(result, view=view))
56    return hitlist
57
58class SimpleFieldSearch(object):
59    """A programmatic (no UI required) search.
60
61    Looks up a given field attribute of the students catalog for a
62    single value. So normally you would call an instance of this
63    search like this:
64
65      >>> SimpleFieldSearch()(reg_number='somevalue')
66
67    """
68    catalog_name = 'students_catalog'
69    def __call__(self, **kw):
70        """Search students catalog programmatically.
71        """
72        if len(kw) != 1:
73            raise ValueError('must give exactly one index name to search')
74        cat = queryUtility(ICatalog, name=self.catalog_name)
75        index_name, query_term = kw.items()[0]
76        results = cat.searchResults(index_name=(query_term, query_term))
77        return results
78
79#: an instance of `SimpleFieldSearch` looking up students catalog.
80simple_search = SimpleFieldSearch()
Note: See TracBrowser for help on using the repository browser.