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

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

Extend get_students_by_course and return school fee paid in that session.

  • Property svn:keywords set to Id
File size: 6.5 KB
RevLine 
[10033]1## $Id: webservices.py 10044 2013-03-22 12:59:13Z 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
[10042]20import xmlrpclib
[10038]21from zope.component import getUtility, queryUtility
[10033]22from zope.catalog.interfaces import ICatalog
[10042]23from waeup.kofa.interfaces import IUniversity, IExtFileStore
24   
[10033]25
[10041]26def get_student(students, identifier):
27    if identifier is None:
28        return None
29    student = students.get(identifier, None)
30    if student is None:
31        cat = queryUtility(ICatalog, name='students_catalog')
32        results = list(
33            cat.searchResults(matric_number=(identifier, identifier)))
34        if len(results) == 1:
35            student = results[0]
36        else:
37            results = list(
38                cat.searchResults(reg_number=(identifier, identifier)))
39            if len(results) == 1:
40                student = results[0]
41    return student
[10033]42
43#class XMLRPCPermission(grok.Permission):
44#    """Permission for using XMLRPC functions.
45#    """
46#    grok.name('waeup.xmlrpc')
47
48#class XMLRPCUsers2(grok.Role):
49#    """Usergroup 2
50#    """
51#    grok.name('waeup.xmlrpcusers2')
52#    grok.title('XMLRPC Users Group 2')
53#    grok.permissions('waeup.xmlrpc',)
54
55class StudentsXMLRPC(grok.XMLRPC):
56    """Student related XMLRPC webservices.
57
58    Please note, that XMLRPC does not support real keyword arguments
59    but positional arguments only.
60    """
61
62    grok.context(IUniversity)
63
64    @grok.require('waeup.xmlrpc')
65    def get_student_id(self, reg_number=None):
66        """Get the id of a student with registration number `reg_number`.
67
68        Returns the student id as string if successful, ``None`` else.
69        """
70        if reg_number is not None:
71            cat = getUtility(ICatalog, name='students_catalog')
72            result = list(
73                cat.searchResults(reg_number=(reg_number, reg_number),
74                                  _limit=1))
75            if not len(result):
76                return None
77            return result[0].student_id
78        return None
[10036]79
80    @grok.require('waeup.xmlrpc')
[10041]81    def get_courses_by_session(self, identifier=None, session=None):
[10043]82        """1. What COURSES are registered by student X in session Y?
[10036]83
84        """
[10041]85        students = self.context['students']
86        student = get_student(students, identifier)
87        if student is None:
[10036]88            return None
89        try:
90            session = int(session)
91        except (TypeError, ValueError):
92            pass
93        sessionsearch = True
94        if session in (None, ''):
95            sessionsearch = False
96        studycourse = student['studycourse']
97        coursetickets = {}
98        for level in studycourse.values():
99            if sessionsearch and level.level_session != session:
100                continue
101            for ct in level.values():
102                coursetickets.update(
103                    {"%s|%s" % (level.level, ct.code): ct.title})
104        if not coursetickets:
105            return None
106        return coursetickets
[10038]107
108    @grok.require('waeup.xmlrpc')
109    def get_students_by_course(self, course=None, session=None):
[10043]110        """2. What STUDENTS registered (student id / matric no)
[10044]111        for course Z in session Y and did they pay school fee?
[10038]112
113        """
114        try:
115            session = int(session)
116        except (TypeError, ValueError):
117            pass
118        sessionsearch = True
119        if session in (None, ''):
120            sessionsearch = False
121        cat = queryUtility(ICatalog, name='coursetickets_catalog')
122        if sessionsearch:
123            coursetickets = cat.searchResults(
124                session=(session, session),
125                code=(course, course))
126        else:
127            coursetickets = cat.searchResults(
128                code=(course, course))
129        if not coursetickets:
130            return None
131        hitlist = []
[10044]132        for c_ticket in coursetickets:
133            amount = 0
134            for p_ticket in c_ticket.student['payments'].values():
135                if p_ticket.p_state == 'paid' and \
136                    p_ticket.p_category == 'schoolfee' and \
137                    p_ticket.p_session == c_ticket.__parent__.level_session:
138                    amount = p_ticket.amount_auth
[10040]139            hitlist.append((
[10044]140                c_ticket.student.student_id,
141                c_ticket.student.matric_number,
142                c_ticket.__parent__.validated_by,
143                amount
144                ))
[10038]145        return list(set(hitlist))
[10042]146
147    @grok.require('waeup.xmlrpc')
148    def get_student_info(self, identifier=None):
[10043]149        """3a. Who is the student with matriculation number / student id
[10042]150
151        """
152        students = self.context['students']
153        student = get_student(students, identifier)
154        if student is None:
155            return None
156        return [student.display_fullname, student.certcode,
157            student.phone, student.email]
158
159    @grok.require('waeup.xmlrpc')
160    def get_student_passport(self, identifier=None):
[10043]161        """3b. Get passport picture of student with
162        matriculation number / student id.
[10042]163
164        """
165        students = self.context['students']
166        student = get_student(students, identifier)
167        if student is None:
168            return None
169        img = getUtility(IExtFileStore).getFileByContext(
170            student, attr='passport.jpg')
171        return xmlrpclib.Binary(img.read())
[10043]172
173    @grok.require('waeup.xmlrpc')
174    def get_paid_sessions(self, identifier=None):
175        """6. Get list of SESSIONS school fees paid by student X.
176
177        """
178        students = self.context['students']
179        student = get_student(students, identifier)
180        if student is None:
181            return None
182        payments_dict = {}
183        for ticket in student['payments'].values():
184            if ticket.p_state == 'paid' and \
185                ticket.p_category == 'schoolfee':
186                payments_dict[str(ticket.p_session)] = ticket.amount_auth
187        if not payments_dict:
188            return None
189        return payments_dict
Note: See TracBrowser for help on using the repository browser.