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

Last change on this file since 9707 was 9632, checked in by uli, 12 years ago

Provide XMLRPC services. We start with a student_id service.

  • Property svn:keywords set to Id
File size: 4.0 KB
Line 
1## $Id: webservices.py 9632 2012-11-14 18:38:31Z uli $
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
21from zope.catalog.interfaces import ICatalog
22from waeup.kofa.payments.interfaces import IPaymentWebservice
23from waeup.kofa.interfaces import IUniversity, academic_sessions_vocab
24
25# Kofa's webservices to requery payments from outside
26
27class KofaPaymentRequest(grok.View):
28
29    grok.context(IUniversity)
30    grok.name('paymentrequest')
31    grok.require('waeup.Public')
32
33    #ACCEPTED_IP = ('174.36.230.28', )
34    ACCEPTED_IP = ('127.0.0.1', )
35   
36    def update(self, P_ID=None):
37        if P_ID == None:
38            self.output = '-1'
39            return
40        real_ip = self.request.get('HTTP_X_FORWARDED_FOR', None)
41        if real_ip:
42            self.context.logger.info('KofaPaymentRequest called: %s' % real_ip)
43        if real_ip and self.ACCEPTED_IP:
44            if real_ip not in  self.ACCEPTED_IP:
45                self.output = '-4'
46                return
47        cat = getUtility(ICatalog, name='payments_catalog')
48        results = list(
49            cat.searchResults(p_id=(P_ID, P_ID), p_state=('paid', 'paid')))
50        if len(results) != 1:
51            self.output = '-1'
52            return
53        try:
54            owner = IPaymentWebservice(results[0])
55            full_name = owner.display_fullname
56            matric_number = owner.reg_or_matric_number
57            faculty = owner.faculty
58            department = owner.department
59        except (TypeError, AttributeError):
60            self.output = '-3'
61            return
62        amount = results[0].amount_auth
63        payment_category = results[0].category
64        payment_item = results[0].p_item
65        academic_session = academic_sessions_vocab.getTerm(
66            results[0].p_session).title
67        self.output = (
68            'FULL_NAME=%s&' +
69            'FACULTY=%s&' +
70            'DEPARTMENT=%s&' +
71            'PAYMENT_ITEM=%s&' +
72            'PAYMENT_CATEGORY=%s&' +
73            'ACADEMIC_SESSION=%s&' +
74            'MATRIC_NUMBER=%s&' +
75            'FEE_AMOUNT=%s') % (full_name, faculty,
76            department, payment_item, payment_category,
77            academic_session, matric_number, amount)
78        return
79
80    def render(self):
81        return self.output
82
83class XMLRPCPermission(grok.Permission):
84    """Permission for using XMLRPC functions.
85    """
86    grok.name('waeup.xmlrpc')
87
88class UniversityXMLRPC(grok.XMLRPC):
89    """XMLRPC webservices for KOFA portals.
90
91    Please note, that XMLRPC does not support real keyword arguments
92    but positional arguments only.
93    """
94    grok.context(IUniversity)
95
96    @grok.require('waeup.Public')
97    def xmlrpc_api_version(self):
98        """Return the current API version for XMLRPC clients.
99        """
100        return u'0.1'
101
102    @grok.require('waeup.xmlrpc')
103    def get_student_id(self, reg_number=None):
104        """Get the id of a student with registration number `reg_number`.
105
106        Returns the student id as string if successful, ``None`` else.
107        """
108        if reg_number is not None:
109            cat = getUtility(ICatalog, name='students_catalog')
110            result = list(
111                cat.searchResults(reg_number=(reg_number, reg_number),
112                                  _limit=1))
113            if not len(result):
114                return None
115            return result[0].student_id
116        return None
Note: See TracBrowser for help on using the repository browser.