source: main/waeup.kofa/trunk/src/waeup/kofa/students/webservices.py @ 10040

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

get_students_by_course: return list of lists.

Harmonize tests.

  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1## $Id: webservices.py 10040 2013-03-18 16:45:34Z henrik $
2##
3## Copyright (C) 2012 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
19import grok
20from zope.component import getUtility, queryUtility
21from zope.catalog.interfaces import ICatalog
22from waeup.kofa.interfaces import IUniversity
23
24
25#class XMLRPCPermission(grok.Permission):
26#    """Permission for using XMLRPC functions.
27#    """
28#    grok.name('waeup.xmlrpc')
29
30#class XMLRPCUsers2(grok.Role):
31#    """Usergroup 2
32#    """
33#    grok.name('waeup.xmlrpcusers2')
34#    grok.title('XMLRPC Users Group 2')
35#    grok.permissions('waeup.xmlrpc',)
36
37class StudentsXMLRPC(grok.XMLRPC):
38    """Student related XMLRPC webservices.
39
40    Please note, that XMLRPC does not support real keyword arguments
41    but positional arguments only.
42    """
43
44    grok.context(IUniversity)
45
46    @grok.require('waeup.xmlrpc')
47    def get_student_id(self, reg_number=None):
48        """Get the id of a student with registration number `reg_number`.
49
50        Returns the student id as string if successful, ``None`` else.
51        """
52        if reg_number is not None:
53            cat = getUtility(ICatalog, name='students_catalog')
54            result = list(
55                cat.searchResults(reg_number=(reg_number, reg_number),
56                                  _limit=1))
57            if not len(result):
58                return None
59            return result[0].student_id
60        return None
61
62    @grok.require('waeup.xmlrpc')
63    def get_courses_by_session(self, student_id=None, session=None):
64        """What COURSES are registered by student X in session Y?
65
66        """
67        if student_id is None:
68            return None
69        student = self.context['students'].get(student_id, None)
70        if student is None:
71            return  None
72        try:
73            session = int(session)
74        except (TypeError, ValueError):
75            pass
76        sessionsearch = True
77        if session in (None, ''):
78            sessionsearch = False
79        studycourse = student['studycourse']
80        coursetickets = {}
81        for level in studycourse.values():
82            if sessionsearch and level.level_session != session:
83                continue
84            for ct in level.values():
85                coursetickets.update(
86                    {"%s|%s" % (level.level, ct.code): ct.title})
87        if not coursetickets:
88            return None
89        return coursetickets
90
91    @grok.require('waeup.xmlrpc')
92    def get_students_by_course(self, course=None, session=None):
93        """What STUDENTS registered (student id / matric no)
94        for course Z in session Y?
95
96        (a) Authorized by course adviser.
97        (b) Not authorized by course adviser.
98        """
99        try:
100            session = int(session)
101        except (TypeError, ValueError):
102            pass
103        sessionsearch = True
104        if session in (None, ''):
105            sessionsearch = False
106        cat = queryUtility(ICatalog, name='coursetickets_catalog')
107        if sessionsearch:
108            coursetickets = cat.searchResults(
109                session=(session, session),
110                code=(course, course))
111        else:
112            coursetickets = cat.searchResults(
113                code=(course, course))
114        if not coursetickets:
115            return None
116        hitlist = []
117        for ticket in coursetickets:
118            hitlist.append((
119                ticket.student.student_id,
120                ticket.student.matric_number,
121                ticket.__parent__.validated_by))
122        return list(set(hitlist))
Note: See TracBrowser for help on using the repository browser.