Ignore:
Timestamp:
28 May 2014, 13:19:03 (10 years ago)
Author:
uli
Message:

Provide xmlrpc service for uploading .fpm files.

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

    r11670 r11674  
    384384        return
    385385
     386    def test_put_student_fingerprints_no_stud(self):
     387        # invalid student ids will result in `False`
     388        server = ServerProxy('http://mgr:mgrpw@localhost/app')
     389        self.assertRaises(
     390            xmlrpclib.Fault, server.put_student_fingerprints,
     391            'invalid id', {})
     392
     393    def test_put_student_fingerprints_non_dict(self):
     394        # fingerprints must be passed in in a dict
     395        server = ServerProxy('http://mgr:mgrpw@localhost/app')
     396        self.setup_student(self.student)
     397        self.assertRaises(
     398            xmlrpclib.Fault, server.put_student_fingerprints,
     399            self.student.student_id, 'not-a-dict')
     400
     401    def test_put_student_fingerprints_non_num_keys_ignored(self):
     402        # non-numeric keys in fingerprint dict are ignored
     403        server = ServerProxy('http://mgr:mgrpw@localhost/app')
     404        self.setup_student(self.student)
     405        result = server.put_student_fingerprints(
     406            self.student.student_id, {'not-a-num': 'foo',
     407                                      '12.2': 'bar',
     408                                      '123': 'baz'})
     409        self.assertEqual(result, False)
     410
     411    def test_put_student_fingerprints_non_fpm_data(self):
     412        # we cannot pass non-.fpm files as values
     413        server = ServerProxy('http://mgr:mgrpw@localhost/app')
     414        self.setup_student(self.student)
     415        self.assertRaises(
     416            xmlrpclib.Fault, server.put_student_fingerprints,
     417            self.student.student_id, {'1': 'not-a-fingerprint'})
     418
     419    def test_put_student_fingerprints_invalid_file_format(self):
     420        # invalid files will result in `False`
     421        server = ServerProxy('http://mgr:mgrpw@localhost/app')
     422        self.setup_student(self.student)
     423        invalid_fpm = xmlrpclib.Binary('invalid file')
     424        self.assertRaises(
     425            xmlrpclib.Fault, server.put_student_fingerprints,
     426            self.student.student_id, {'1': invalid_fpm})
     427
     428    def test_put_student_fingerprints(self):
     429        # we can store fingerprints
     430        server = ServerProxy('http://mgr:mgrpw@localhost/app')
     431        self.setup_student(self.student)
     432        fpm = xmlrpclib.Binary('FP1faked_fpm')
     433        result = server.put_student_fingerprints(
     434            self.student.student_id, {'1': fpm})
     435        self.assertEqual(result, True)
     436        stored_file = getUtility(IExtFileStore).getFileByContext(
     437            self.student, attr="1.fpm")
     438        self.assertEqual(stored_file.read(), 'FP1faked_fpm')
     439
    386440    def test_get_student_fingerprints_no_stud(self):
    387441        # invalid student ids result in empty dict
  • main/waeup.kofa/trunk/src/waeup/kofa/students/webservices.py

    r11671 r11674  
    1919import os
    2020import xmlrpclib
     21from cStringIO import StringIO
    2122from zope.component import getUtility, queryUtility
    2223from zope.catalog.interfaces import ICatalog
    23 from waeup.kofa.interfaces import IUniversity, IExtFileStore
     24from waeup.kofa.interfaces import (
     25    IUniversity, IExtFileStore, IFileStoreNameChooser,
     26    )
     27from waeup.kofa.utils.helpers import get_fileformat
    2428
    2529
     
    230234                    )
    231235
     236    @grok.require('waeup.putBiometricData')
     237    def put_student_fingerprints(self, identifier=None, fingerprints={}):
     238        """Store fingerprint files for student identified by `identifier`.
     239
     240        `fingerprints` is expected to be a dict with strings
     241        ``1``..``10`` as keys and binary data as values.
     242
     243        The keys ``1``..``10`` represent respective fingers: ``1`` is
     244        the left thumb, ``10`` the little finger of right hand.
     245
     246        The binary data values are expected to be fingerprint minutiae
     247        files as created by the libfprint library. With the std python
     248        `xmlrpclib` client you can create such values with
     249        `xmlrpclib.Binary(<BINARY_DATA_HERE>)`.
     250
     251        The following problems will raise errors:
     252
     253        - Invalid student identifiers (student does not exist or
     254          unknown format of identifier)
     255
     256        - Fingerprint files that are not put into a dict.
     257
     258        - Fingerprint dicts that contain non-FPM files (or otherwise
     259          invalid .fpm data).
     260
     261        Returns `True` in case of successful operation (at least one
     262        fingerprint was stored), `False` otherwise.
     263        """
     264        result = False
     265        students = self.context['students']
     266        student = get_student(students, identifier)
     267        if student is None:
     268            raise xmlrpclib.Fault(
     269                xmlrpclib.INVALID_METHOD_PARAMS,
     270                "No such student: '%s'" % identifier)
     271        if not isinstance(fingerprints, dict):
     272            raise xmlrpclib.Fault(
     273                xmlrpclib.INVALID_METHOD_PARAMS,
     274                "Invalid fingerprint data: must be in dict")
     275        for str_key, val in fingerprints.items():
     276            num = 0
     277            try:
     278                num = int(str_key)
     279            except ValueError:
     280                pass
     281            if num < 1 or num > 10:
     282                continue
     283            if not isinstance(val, xmlrpclib.Binary):
     284                raise xmlrpclib.Fault(
     285                    xmlrpclib.INVALID_METHOD_PARAMS,
     286                    "Invalid data for finger %s" % num)
     287            fmt = get_fileformat(None, val.data)
     288            if fmt != 'fpm':
     289                raise xmlrpclib.Fault(
     290                    xmlrpclib.INVALID_METHOD_PARAMS,
     291                    "Invalid file format for finger %s" % num)
     292            file_store = getUtility(IExtFileStore)
     293            file_id = IFileStoreNameChooser(student).chooseName(
     294                attr='%s.fpm' % num)
     295            file_store.createFile(file_id, StringIO(val.data))
     296            result = True
     297        return result
     298
    232299    @grok.require('waeup.getBiometricData')
    233300    def get_student_fingerprints(self, identifier=None):
Note: See TracChangeset for help on using the changeset viewer.