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

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

Add indexes faccode, depcode and certcode to students_catalog.

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