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

Last change on this file since 9700 was 9486, checked in by Henrik Bettermann, 12 years ago

Rename clearance_comment to officer_comment. That's more appropriate since the comment can also be used by other officers.

  • Property svn:keywords set to Id
File size: 4.7 KB
Line 
1## $Id: catalog.py 9486 2012-10-31 16:30:02Z 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
29from waeup.kofa.university.vocabularies import course_levels
30
31class StudentsCatalog(grok.Indexes):
32    """A catalog for students.
33    """
34    grok.site(IUniversity)
35    grok.name('students_catalog')
36    grok.context(IStudent)
37
38    student_id = index.Field(attribute='student_id')
39    fullname = index.Text(attribute='fullname')
40    email = index.Field(attribute='email')
41    reg_number = index.Field(attribute='reg_number')
42    matric_number = index.Field(attribute='matric_number')
43    state = index.Field(attribute='state')
44    certcode = index.Field(attribute='certcode')
45    depcode = index.Field(attribute='depcode')
46    faccode = index.Field(attribute='faccode')
47    current_session = index.Field(attribute='current_session')
48    current_mode = index.Field(attribute='current_mode')
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        self.translated_state = context.translated_state
65        self.current_level = context['studycourse'].current_level
66        try:
67            current_session = academic_sessions_vocab.getTerm(
68                context['studycourse'].current_session).title
69        except LookupError:
70            current_session = None
71        self.current_session = current_session
72        self.certificate = context['studycourse'].certificate
73        self.comment = getattr(context, 'officer_comment', None)
74
75def search(query=None, searchtype=None, view=None):
76    hitlist = []
77    if searchtype in ('fullname',):
78        results = Query().searchResults(
79            Text(('students_catalog', searchtype), query))
80    else:
81        # Temporary solution to display all students added
82        if query == '*':
83            cat = queryUtility(ICatalog, name='students_catalog')
84            results = cat.searchResults(student_id=(None, None))
85        else:
86            results = Query().searchResults(
87                Eq(('students_catalog', searchtype), query))
88    for result in results:
89        hitlist.append(StudentQueryResultItem(result, view=view))
90    return hitlist
91
92class SimpleFieldSearch(object):
93    """A programmatic (no UI required) search.
94
95    Looks up a given field attribute of the students catalog for a
96    single value. So normally you would call an instance of this
97    search like this:
98
99      >>> SimpleFieldSearch()(reg_number='somevalue')
100
101    """
102    catalog_name = 'students_catalog'
103    def __call__(self, **kw):
104        """Search students catalog programmatically.
105        """
106        if len(kw) != 1:
107            raise ValueError('must give exactly one index name to search')
108        cat = queryUtility(ICatalog, name=self.catalog_name)
109        index_name, query_term = kw.items()[0]
110        results = cat.searchResults(index_name=(query_term, query_term))
111        return results
112
113#: an instance of `SimpleFieldSearch` looking up students catalog.
114simple_search = SimpleFieldSearch()
115
116class CourseTicketIndexes(grok.Indexes):
117    """A catalog for course tickets.
118    """
119    grok.site(IUniversity)
120    grok.name('coursetickets_catalog')
121    grok.context(ICourseTicket)
122
123    level = index.Field(attribute='getLevel')
124    session = index.Field(attribute='getLevelSession')
125    code = index.Field(attribute='code')
Note: See TracBrowser for help on using the repository browser.