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

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

Add current_session to students_catalog indexes.

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