## $Id: webservices.py 10040 2013-03-18 16:45:34Z henrik $ ## ## Copyright (C) 2012 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## import grok from zope.component import getUtility, queryUtility from zope.catalog.interfaces import ICatalog from waeup.kofa.interfaces import IUniversity #class XMLRPCPermission(grok.Permission): # """Permission for using XMLRPC functions. # """ # grok.name('waeup.xmlrpc') #class XMLRPCUsers2(grok.Role): # """Usergroup 2 # """ # grok.name('waeup.xmlrpcusers2') # grok.title('XMLRPC Users Group 2') # grok.permissions('waeup.xmlrpc',) class StudentsXMLRPC(grok.XMLRPC): """Student related XMLRPC webservices. Please note, that XMLRPC does not support real keyword arguments but positional arguments only. """ grok.context(IUniversity) @grok.require('waeup.xmlrpc') def get_student_id(self, reg_number=None): """Get the id of a student with registration number `reg_number`. Returns the student id as string if successful, ``None`` else. """ if reg_number is not None: cat = getUtility(ICatalog, name='students_catalog') result = list( cat.searchResults(reg_number=(reg_number, reg_number), _limit=1)) if not len(result): return None return result[0].student_id return None @grok.require('waeup.xmlrpc') def get_courses_by_session(self, student_id=None, session=None): """What COURSES are registered by student X in session Y? """ if student_id is None: return None student = self.context['students'].get(student_id, None) if student is None: return None try: session = int(session) except (TypeError, ValueError): pass sessionsearch = True if session in (None, ''): sessionsearch = False studycourse = student['studycourse'] coursetickets = {} for level in studycourse.values(): if sessionsearch and level.level_session != session: continue for ct in level.values(): coursetickets.update( {"%s|%s" % (level.level, ct.code): ct.title}) if not coursetickets: return None return coursetickets @grok.require('waeup.xmlrpc') def get_students_by_course(self, course=None, session=None): """What STUDENTS registered (student id / matric no) for course Z in session Y? (a) Authorized by course adviser. (b) Not authorized by course adviser. """ try: session = int(session) except (TypeError, ValueError): pass sessionsearch = True if session in (None, ''): sessionsearch = False cat = queryUtility(ICatalog, name='coursetickets_catalog') if sessionsearch: coursetickets = cat.searchResults( session=(session, session), code=(course, course)) else: coursetickets = cat.searchResults( code=(course, course)) if not coursetickets: return None hitlist = [] for ticket in coursetickets: hitlist.append(( ticket.student.student_id, ticket.student.matric_number, ticket.__parent__.validated_by)) return list(set(hitlist))