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

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

Convert getLevel and getLevelSession into property attributes.

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