Ignore:
Timestamp:
21 Mar 2013, 16:12:06 (12 years ago)
Author:
Henrik Bettermann
Message:

Add two more webservice methods.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/students
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_webservices.py

    r10041 r10042  
    11# Tests for webservices
    22import xmlrpclib
     3import os
     4from cStringIO import StringIO
    35from zope.app.testing.xmlrpc import ServerProxy
    4 from zope.component import createObject
     6from zope.component import createObject, getUtility
    57from zope.component.hooks import setSite
    68from zope.testbrowser.testing import Browser
    79from waeup.kofa.app import University
     10from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser
    811from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
    912from waeup.kofa.students.tests.test_browser import StudentsFullSetup
     
    3538        ticket.semester = 2
    3639        study_level[ticket.code] = ticket
     40
     41    def create_passport_img(self, student):
     42        # create some passport file for `student`
     43        storage = getUtility(IExtFileStore)
     44        image_path = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
     45        self.image_contents = open(image_path, 'rb').read()
     46        file_id = IFileStoreNameChooser(student).chooseName(
     47            attr='passport.jpg')
     48        storage.createFile(file_id, StringIO(self.image_contents))
    3749
    3850    def XMLRPC_post(self, body):
     
    191203        self.assertEqual(xmlout, RESPONSE_XML)
    192204        return
     205
     206    def test_get_student_info(self):
     207        server = ServerProxy('http://mgr:mgrpw@localhost/app')
     208        self.setup_student(self.student)
     209        result = server.get_student_info('123')
     210        self.assertEqual(result,
     211            ['Anna Tester', 'CERT1', '1234', 'aa@aa.ng'])
     212        REQUEST_XML="""\
     213<?xml version="1.0"?>
     214<methodCall>
     215<methodName>get_student_info</methodName>
     216<params>
     217<param>
     218<value><string>K1000000</string></value>
     219</param>
     220</params>
     221</methodCall>"""
     222        RESPONSE_XML="""\
     223<?xml version='1.0'?>
     224<methodResponse>
     225<params>
     226<param>
     227<value><array><data>
     228<value><string>Anna Tester</string></value>
     229<value><string>CERT1</string></value>
     230<value><string>1234</string></value>
     231<value><string>aa@aa.ng</string></value>
     232</data></array></value>
     233</param>
     234</params>
     235</methodResponse>
     236"""
     237        xmlout = self.XMLRPC_post(REQUEST_XML)
     238        self.assertEqual(xmlout, RESPONSE_XML)
     239        return
     240
     241    def test_get_student_passport(self):
     242        server = ServerProxy('http://mgr:mgrpw@localhost/app')
     243        self.setup_student(self.student)
     244        self.create_passport_img(self.student)
     245        result = server.get_student_passport('123')
     246        img = getUtility(IExtFileStore).getFileByContext(
     247            self.student, attr='passport.jpg')
     248        binary = img.read()
     249        self.assertEqual(binary, result)
     250        REQUEST_XML="""\
     251<?xml version="1.0"?>
     252<methodCall>
     253<methodName>get_student_passport</methodName>
     254<params>
     255<param>
     256<value><string>K1000000</string></value>
     257</param>
     258</params>
     259</methodCall>"""
     260        RESPONSE_XML="""\
     261<?xml version='1.0'?>
     262<methodResponse>
     263<params>
     264<param>
     265<value><base64>
     266/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/b
     267"""
     268        xmlout = self.XMLRPC_post(REQUEST_XML)
     269        self.assertTrue(xmlout.startswith(RESPONSE_XML))
  • main/waeup.kofa/trunk/src/waeup/kofa/students/webservices.py

    r10041 r10042  
    1818
    1919import grok
     20import xmlrpclib
    2021from zope.component import getUtility, queryUtility
    2122from zope.catalog.interfaces import ICatalog
    22 from waeup.kofa.interfaces import IUniversity
     23from waeup.kofa.interfaces import IUniversity, IExtFileStore
     24   
    2325
    2426def get_student(students, identifier):
     
    136138                ticket.__parent__.validated_by))
    137139        return list(set(hitlist))
     140
     141    @grok.require('waeup.xmlrpc')
     142    def get_student_info(self, identifier=None):
     143        """Who is the student with matriculation number / student id?
     144
     145        """
     146        students = self.context['students']
     147        student = get_student(students, identifier)
     148        if student is None:
     149            return None
     150        return [student.display_fullname, student.certcode,
     151            student.phone, student.email]
     152
     153    @grok.require('waeup.xmlrpc')
     154    def get_student_passport(self, identifier=None):
     155        """Get passport picture of student with
     156        matriculation number / student id?
     157
     158        """
     159        students = self.context['students']
     160        student = get_student(students, identifier)
     161        if student is None:
     162            return None
     163        img = getUtility(IExtFileStore).getFileByContext(
     164            student, attr='passport.jpg')
     165        return xmlrpclib.Binary(img.read())
Note: See TracChangeset for help on using the changeset viewer.