Changeset 7121 for main


Ignore:
Timestamp:
16 Nov 2011, 18:07:03 (13 years ago)
Author:
uli
Message:

Let applicants cooperate with new imagestorage and accept at least jpg and png.

Location:
main/waeup.sirp/trunk/src/waeup/sirp/applicants
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/applicant.py

    r7105 r7121  
    201201        """
    202202        marker, filename, basename, ext = store.extractMarker(file_id)
    203         return os.path.join(root, 'applicants', filename)
     203        sub_root = os.path.join(root, 'applicants')
     204        return super(ApplicantImageStoreHandler, self).pathFromFileID(
     205            store, sub_root, basename)
    204206
    205207    def createFile(self, store, root, filename, file_id, file):
    206208        """Create a browsable file-like object.
    207209        """
    208         # possible other actions: check for jpeg format
    209         path = self.pathFromFileID(store, root, file_id)
    210         return file, path, WAeUPImageFile(filename, file_id)
     210        ext = os.path.splitext(filename)[1].lower()
     211        if ext not in ['.jpg', '.png']:
     212            raise ValueError('Only .jpg and .png allowed')
     213        # call super method to ensure that any old files with
     214        # different filename extension are deleted.
     215        file, path, file_obj =  super(
     216            ApplicantImageStoreHandler, self).createFile(
     217            store, root,  filename, file_id, file)
     218        return file, path, WAeUPImageFile(
     219            file_obj.filename, file_obj.data)
  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_applicant.py

    r7063 r7121  
    2323"""
    2424import grok
     25import os
     26import shutil
     27import tempfile
    2528import unittest
     29from StringIO import StringIO
    2630from zope.component import adapts, queryUtility
    2731from zope.component.interfaces import IFactory
     
    4852    layer = FunctionalLayer
    4953
     54    def setUp(self):
     55        super(HelperTests, self).setUp()
     56        self.workdir = tempfile.mkdtemp()
     57        return
     58
     59    def tearDown(self):
     60        super(HelperTests, self).tearDown()
     61        shutil.rmtree(self.workdir)
     62        return
     63
    5064    def test_get_regno_or_ac(self):
    5165        # we can get reg_no or access_code of an applicants if it is set
     
    8599            store, '/fake-root', '__img-applicant__dir1/dir2/sample.jpg')
    86100        self.assertEqual(
    87             result1, '/fake-root/applicants/sample.jpg')
    88         self.assertEqual(
    89             result2, '/fake-root/applicants/dir1/sample.jpg')
    90         self.assertEqual(
    91             result3, '/fake-root/applicants/dir1/dir2/sample.jpg')
     101            result1, '/fake-root/applicants/sample')
     102        self.assertEqual(
     103            result2, '/fake-root/applicants/dir1/sample')
     104        self.assertEqual(
     105            result3, '/fake-root/applicants/dir1/dir2/sample')
     106        return
     107
     108    def test_image_store_handler_create(self):
     109        # we can create files in image store with the applicant image
     110        # store handler
     111        store = DefaultStorage(self.workdir)
     112        handler = queryUtility(IFileStoreHandler, name='img-applicant')
     113        file_obj, path, waeup_file = handler.createFile(
     114            store, store.root, 'sample.jpg', '__img_applicant__sample',
     115            StringIO('I am a JPG file'))
     116        self.assertEqual(path[-21:], 'applicants/sample.jpg')
     117        return
     118
     119    def test_image_store_handler_invalid_filename_ext(self):
     120        # we only accept '.jpg' and '.png' as filename extensions.
     121        store = DefaultStorage()
     122        handler = queryUtility(IFileStoreHandler, name='img-applicant')
     123        self.assertRaises(
     124            ValueError,
     125            handler.createFile,
     126            store, store.root, 'sample.txt', '__img_applicant__sample',
     127            StringIO('I am a txt file'))
    92128        return
    93129
Note: See TracChangeset for help on using the changeset viewer.