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

Last change on this file since 10009 was 10007, checked in by uli, 12 years ago

Create a dedicated demo user for webservice XMLRPC.

  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1## $Id: webservices.py 10007 2013-02-28 10:53:33Z 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            reg_number = owner.reg_number
57            matric_number = owner.matric_number
58            faculty = owner.faculty
59            department = owner.department
60        except (TypeError, AttributeError):
61            self.output = '-3'
62            return
63        amount = results[0].amount_auth
64        payment_category = results[0].category
65        payment_item = results[0].p_item
66        academic_session = academic_sessions_vocab.getTerm(
67            results[0].p_session).title
68        self.output = (
69            'FULL_NAME=%s&' +
70            'FACULTY=%s&' +
71            'DEPARTMENT=%s&' +
72            'PAYMENT_ITEM=%s&' +
73            'PAYMENT_CATEGORY=%s&' +
74            'ACADEMIC_SESSION=%s&' +
75            'MATRIC_NUMBER=%s&' +
76            'REG_NUMBER=%s&' +
77            'FEE_AMOUNT=%s') % (full_name, faculty,
78            department, payment_item, payment_category,
79            academic_session, matric_number, reg_number, amount)
80        return
81
82    def render(self):
83        return self.output
84
85class XMLRPCPermission(grok.Permission):
86    """Permission for using XMLRPC functions.
87    """
88    grok.name('waeup.xmlrpc')
89
90class XMLRPCUsers1(grok.Role):
91    """Usergroup 1
92    """
93    grok.name('waeup.xmlrpcusers1')
94    grok.title('XMLRPC Users Group 1')
95    grok.permissions('waeup.xmlrpc',)
96
97class UniversityXMLRPC(grok.XMLRPC):
98    """XMLRPC webservices for KOFA portals.
99
100    Please note, that XMLRPC does not support real keyword arguments
101    but positional arguments only.
102    """
103    grok.context(IUniversity)
104
105    @grok.require('waeup.Public')
106    def xmlrpc_api_version(self):
107        """Return the current API version for XMLRPC clients.
108        """
109        return u'0.1'
110
111    @grok.require('waeup.xmlrpc')
112    def get_student_id(self, reg_number=None):
113        """Get the id of a student with registration number `reg_number`.
114
115        Returns the student id as string if successful, ``None`` else.
116        """
117        if reg_number is not None:
118            cat = getUtility(ICatalog, name='students_catalog')
119            result = list(
120                cat.searchResults(reg_number=(reg_number, reg_number),
121                                  _limit=1))
122            if not len(result):
123                return None
124            return result[0].student_id
125        return None
Note: See TracBrowser for help on using the repository browser.