source: main/waeup.kofa/trunk/src/waeup/kofa/students/catalog.py @ 8629

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

Implement search page for applicants. Add fullname to applicants_catalog.
Plugins must be updated and /reindex?ctlg=applicants must be performed.

Tests will follow.

Rename ApplicantCatalog? to ApplicantsCatalog?. This does not affect persistent data.

Rename StudentIndexes? to StudentsCatalog?.

Add more localization.

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