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

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

Adjust copyright statement and svn keyword in students.

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