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

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

Add catalogs (indexes) for payments and course tickets.

  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1## $Id: catalog.py 7633 2012-02-11 23:02:24Z 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 (
27    IUniversity, IQueryResultItem, academic_sessions_vocab)
28from waeup.sirp.students.interfaces import (IStudent, ICourseTicket,
29    IStudentOnlinePayment)
30from waeup.sirp.university.vocabularies import course_levels
31
32class StudentIndexes(grok.Indexes):
33    """A catalog for students.
34    """
35    grok.site(IUniversity)
36    grok.name('students_catalog')
37    grok.context(IStudent)
38
39    student_id = index.Field(attribute='student_id')
40    fullname = index.Text(attribute='fullname')
41    email = index.Field(attribute='email')
42    reg_number = index.Field(attribute='reg_number')
43    matric_number = index.Field(attribute='matric_number')
44    state = index.Field(attribute='state')
45    certcode = index.Field(attribute='certcode')
46    depcode = index.Field(attribute='depcode')
47    faccode = index.Field(attribute='faccode')
48    current_session = index.Field(attribute='current_session')
49
50class StudentQueryResultItem(object):
51    grok.implements(IQueryResultItem)
52
53    title = u'Student Query Item'
54    description = u'Some student found in a search'
55
56    def __init__(self, context, view):
57        self.context = context
58        self.url = view.url(context)
59        self.student_id = context.student_id
60        self.display_fullname = context.display_fullname
61        self.reg_number = context.reg_number
62        self.matric_number = context.matric_number
63        self.state = context.state
64        try:
65            current_level = course_levels.getTerm(
66                context['studycourse'].current_level).title
67        except LookupError:
68            current_level = None
69        self.current_level = current_level
70        try:
71            current_session = academic_sessions_vocab.getTerm(
72                context['studycourse'].current_session).title
73        except LookupError:
74            current_session = None
75        self.current_session = current_session
76        self.certificate = context['studycourse'].certificate
77
78def search(query=None, searchtype=None, view=None):
79    hitlist = []
80    if searchtype in ('fullname',):
81        results = Query().searchResults(
82            Text(('students_catalog', searchtype), query))
83    else:
84        # Temporary solution to display all students added
85        if query == '*':
86            cat = queryUtility(ICatalog, name='students_catalog')
87            results = cat.searchResults(student_id=(None, None))
88        else:
89            results = Query().searchResults(
90                Eq(('students_catalog', searchtype), query))
91    for result in results:
92        hitlist.append(StudentQueryResultItem(result, view=view))
93    return hitlist
94
95class SimpleFieldSearch(object):
96    """A programmatic (no UI required) search.
97
98    Looks up a given field attribute of the students catalog for a
99    single value. So normally you would call an instance of this
100    search like this:
101
102      >>> SimpleFieldSearch()(reg_number='somevalue')
103
104    """
105    catalog_name = 'students_catalog'
106    def __call__(self, **kw):
107        """Search students catalog programmatically.
108        """
109        if len(kw) != 1:
110            raise ValueError('must give exactly one index name to search')
111        cat = queryUtility(ICatalog, name=self.catalog_name)
112        index_name, query_term = kw.items()[0]
113        results = cat.searchResults(index_name=(query_term, query_term))
114        return results
115
116#: an instance of `SimpleFieldSearch` looking up students catalog.
117simple_search = SimpleFieldSearch()
118
119class CourseTicketIndexes(grok.Indexes):
120    """A catalog for course tickets.
121    """
122    grok.site(IUniversity)
123    grok.name('coursetickets_catalog')
124    grok.context(ICourseTicket)
125
126    level = index.Field(attribute='getLevel')
127    session = index.Field(attribute='getLevelSession')
128    code = index.Field(attribute='code')
129
130class StudentPaymentIndexes(grok.Indexes):
131    """A catalog for payments.
132    """
133    grok.site(IUniversity)
134    grok.name('payments_catalog')
135    grok.context(IStudentOnlinePayment)
136
137    p_session = index.Field(attribute='p_session')
138    p_category = index.Field(attribute='p_category')
139    p_item = index.Field(attribute='p_item')
140    p_state = index.Field(attribute='p_state')
141    r_pay_reference = index.Text(attribute='r_pay_reference')
Note: See TracBrowser for help on using the repository browser.