- Timestamp:
- 4 Nov 2011, 18:36:27 (13 years ago)
- Location:
- main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/__init__.py
r6632 r7002 3 3 # Make this a package. 4 4 from waeup.sirp.applicants.applicant import ( 5 ResultEntry, Applicant, ApplicantFactory, ApplicantTraverser, 5 ResultEntry, Applicant, ApplicantFactory, ApplicantImageStoreHandler, 6 get_regno_or_ac, ApplicantImageNameChooser, 6 7 ) 7 8 from waeup.sirp.applicants.container import ApplicantsContainer … … 14 15 'Applicant', 15 16 'ApplicantFactory', 17 'ApplicantImageNameChooser', 18 'ApplicantImageStoreHandler', 16 19 'ApplicantsContainer', 17 20 'ApplicantsRoot', 18 'ApplicantTraverser',19 21 'application_exists', 20 22 'get_applicant_data', 23 'get_regno_or_ac', 21 24 ] -
main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/applicant.py
r6632 r7002 20 20 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 21 ## 22 import os 22 23 import grok 23 24 from grok import index 25 from zope.component import getUtility 24 26 from zope.component.interfaces import IFactory 25 27 from zope.interface import implementedBy 26 28 from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState 27 from waeup.sirp.interfaces import IObjectHistory 29 from waeup.sirp.interfaces import IObjectHistory, IExtFileStore 28 30 from waeup.sirp.app import University 29 31 from waeup.sirp.applicants.interfaces import ( 30 32 IResultEntry, IApplicant, IApplicantEdit, default_passport_image, 31 33 ) 34 from waeup.sirp.image import WAeUPImageFile 32 35 from waeup.sirp.utils.helpers import attrs_to_fields 36 37 def get_regno_or_ac(context): 38 reg_no = getattr(context, 'reg_no', None) 39 if reg_no is None: 40 return getattr(context, 'access_code', None) 41 return reg_no 33 42 34 43 class ResultEntry(grok.Context): … … 81 90 access_code = index.Field(attribute='access_code') 82 91 83 class ApplicantTraverser(grok.Traverser): 84 """Get image of the context applicant. 85 86 Each applicant can provide a passport photograph which will be 87 returned by this traverser if: 88 89 - we request the exact filename of the picture or 90 91 - ask for a picture named 'passport.jpg'. 92 93 If no picture was stored yet, we get a placeholder image when 94 asking for `passport.jpg`. 95 96 If none of the above applies, we return ``None``, most probably 97 resulting a :exc:`NotFound` exception. 98 99 """ 100 grok.context(IApplicant) 101 def traverse(self, name): 102 passport_filename = getattr(self.context.passport, 'filename', None) 103 if name == passport_filename: 104 return self.context.passport 105 if name == 'passport.jpg': 106 if self.context.passport is not None: 107 return self.context.passport 108 return 92 #class ApplicantTraverser(grok.Traverser): 93 # """Get image of the context applicant. 94 # 95 # Each applicant can provide a passport photograph which will be 96 # returned by this traverser if: 97 # 98 # - we request the exact filename of the picture or 99 # 100 # - ask for a picture named 'passport.jpg'. 101 # 102 # If no picture was stored yet, we get a placeholder image when 103 # asking for `passport.jpg`. 104 # 105 # If none of the above applies, we return ``None``, most probably 106 # resulting a :exc:`NotFound` exception. 107 # 108 # """ 109 # grok.context(IApplicant) 110 # def traverse(self, name): 111 # if name != 'passport.jpg': 112 # return 113 # marked_filename = '__img_appl__%s.jpg' % ( 114 # get_regno_or_ac(self.context)) 115 # image = getUtility(IExtFileStore).getFile(marked_filename) 116 # if image is None: 117 # # Return placeholder 118 # from waeup.sirp.applicants.interfaces import IMAGE_PATH 119 # return open(os.path.join(IMAGE_PATH, 'placeholder_m.jpg'), 'rb') 120 # pass 121 # return image #WAeUPImageFile(marked_filename, image.read()) 122 # if not hasattr(self.context, 'passport'): 123 # return None 124 # passport_filename = getattr(self.context.passport, 'filename', None) 125 # if name == passport_filename: 126 # return self.context.passport 127 # if name == 'passport.jpg': 128 # if self.context.passport is not None: 129 # return self.context.passport 130 # return 109 131 110 132 class ApplicantFactory(grok.GlobalUtility): … … 121 143 def getInterfaces(self): 122 144 return implementedBy(Applicant) 145 146 from waeup.sirp.interfaces import IFileStoreHandler, IFileStoreNameChooser 147 from waeup.sirp.imagestorage import DefaultFileStoreHandler 148 149 APPLICANT_IMAGE_STORE_NAME = 'img-applicant' 150 151 class ApplicantImageNameChooser(grok.Adapter): 152 grok.context(IApplicant) 153 grok.implements(IFileStoreNameChooser) 154 155 def checkName(self, name=None): 156 return name == self.chooseName(name, self.context) 157 158 def chooseName(self, name=None): 159 parent_name = getattr( 160 getattr(self.context, '__parent__', None), 161 '__name__', '_default') 162 marked_filename = '__%s__%s/%s.jpg' % ( 163 APPLICANT_IMAGE_STORE_NAME, 164 parent_name, get_regno_or_ac(self.context)) 165 return marked_filename 166 167 168 class ApplicantImageStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility): 169 """Applicant specific image handling. 170 171 This handler knows in which path in a filestore to store applicant 172 images and how to turn this kind of data into some (browsable) 173 file object. 174 175 It is called from the global file storage, when it wants to 176 get/store a file with a file id starting with 177 ``__img-applicant__`` (the marker string for applicant images). 178 179 Like each other file store handler it does not handle the files 180 really (this is done by the global file store) but only computes 181 paths and things like this. 182 """ 183 grok.implements(IFileStoreHandler) 184 grok.name(APPLICANT_IMAGE_STORE_NAME) 185 186 def pathFromFileID(self, store, root, file_id): 187 """All applicants images are filed in directory ``applicants``. 188 """ 189 marker, filename, basename, ext = store.extractMarker(file_id) 190 return os.path.join(root, 'applicants', filename) 191 192 def createFile(self, store, root, filename, file_id, file): 193 """Create a browsable file-like object. 194 """ 195 # possible other actions: check for jpeg format 196 path = self.pathFromFileID(store, root, file_id) 197 return file, path, WAeUPImageFile(filename, file_id) -
main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/browser.py
r6816 r7002 22 22 """UI components for basic applicants and related components. 23 23 """ 24 import os 24 25 import sys 25 26 import grok … … 57 58 ) 58 59 from waeup.sirp.image.image import createWAeUPImageFile 59 from waeup.sirp.interfaces import IWAeUPObject, ILocalRolesAssignable 60 from waeup.sirp.interfaces import ( 61 IWAeUPObject, ILocalRolesAssignable, IExtFileStore, IFileStoreNameChooser) 60 62 from waeup.sirp.permissions import get_users_with_local_roles 61 63 from waeup.sirp.university.interfaces import ICertificate … … 65 67 from waeup.sirp.widgets.objectwidget import ( 66 68 WAeUPObjectWidget, WAeUPObjectDisplayWidget) 67 from waeup.sirp.applicants import ResultEntry, Applicant, get_applicant_data 69 from waeup.sirp.applicants import ( 70 ResultEntry, Applicant, get_applicant_data, get_regno_or_ac) 68 71 from waeup.sirp.applicants.interfaces import ( 69 72 IApplicant, IApplicantPrincipal,IApplicantEdit, IApplicantsRoot, 70 IApplicantsContainer, IApplicantsContainerAdd, application_types_vocab 73 IApplicantsContainer, IApplicantsContainerAdd, application_types_vocab, 74 IMAGE_PATH, 71 75 ) 72 76 from waeup.sirp.applicants.workflow import INITIALIZED, STARTED … … 551 555 'locked').omit('course_admitted') 552 556 #form_fields['fst_sit_results'].custom_widget = list_results_display_widget 553 form_fields['passport'].custom_widget = ThumbnailWidget557 #form_fields['passport'].custom_widget = ThumbnailWidget 554 558 form_fields['date_of_birth'].custom_widget = FriendlyDateDisplayWidget('le') 555 559 label = 'Applicant' 556 560 grok.template('form_display') 557 561 pnav = 3 562 563 def update(self): 564 self.passport_url = self.url(self.context, 'passport.jpg') 565 return 558 566 559 567 @property … … 705 713 grok.require('waeup.manageApplications') 706 714 form_fields = grok.AutoFields(IApplicant) 707 form_fields['passport'].custom_widget = EncodingImageFileWidget715 #form_fields['passport'].custom_widget = EncodingImageFileWidget 708 716 form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') 709 717 grok.template('form_edit') … … 718 726 if upload: 719 727 # We got a fresh upload 720 image = createWAeUPImageFile(upload.filename, upload) 728 #from waeup.sirp.interfaces import IExtFileStorage 729 #from zope.component import queryUtility 730 #storage = queryUtility(IExtFileStorage) 731 # filename = '__img_appl__' + str(self.context.reg_no) #upload.filename 732 from waeup.sirp.interfaces import ( 733 IExtFileStore, IFileStoreNameChooser) 734 store = getUtility(IExtFileStore) 735 file_id = IFileStoreNameChooser(self.context).chooseName() 736 store.createFile(file_id, upload) 737 #filename = '__img_appl__' + str( 738 # self.context.access_code) + '.jpg' #upload.filename 739 print "FILENAME: ", file_id 740 # image = createWAeUPImageFile(upload.filename, upload) 741 #image = createWAeUPImageFile(filename, upload) 721 742 # This would normally be totally illegal. We set context 722 743 # data without the complete form data being … … 731 752 # we set the image here and not in 'save()' or other 732 753 # methods. 733 self.context.passport = image754 #self.context.passport = image 734 755 upload.seek(0) 735 756 self.passport_changed = True … … 782 803 'screening_score', 783 804 ) 784 form_fields['passport'].custom_widget = EncodingImageFileWidget805 #form_fields['passport'].custom_widget = EncodingImageFileWidget 785 806 form_fields['date_of_birth'].custom_widget = FriendlyDateWidget('le-year') 786 807 grok.template('form_edit') … … 868 889 text = 'View application record' 869 890 target = 'index' 891 892 class PassportImage(grok.View): 893 """Renders the passport image for applicants. 894 """ 895 grok.name('passport.jpg') 896 grok.context(IApplicant) 897 grok.require('waeup.handleApplication') 898 899 def render(self): 900 # A filename chooser turns a context into a filename suitable 901 # for file storage. 902 chooser = IFileStoreNameChooser(self.context) 903 file_id = chooser.chooseName() 904 image = getUtility(IExtFileStore).getFile(file_id) 905 self.response.setHeader( 906 'Content-Type', 'image/jpeg') 907 if image is None: 908 # show placeholder image 909 return open(os.path.join(IMAGE_PATH, 'placeholder_m.jpg'), 'rb') 910 return image -
main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/browser_templates/form_display.pt
r6350 r7002 13 13 <table class="zebra"> 14 14 <tbody> 15 <tr><td></td> 16 <td><img src="" tal:attributes="src view/passport_url" /></td> 17 </tr> 15 18 <tal:block repeat="widget view/widgets"> 16 19 <tr> -
main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/browser_templates/form_edit.pt
r6756 r7002 46 46 </tr> 47 47 </tal:block> 48 <tr> 49 <td class="label">Photograph</td> 50 <td class="field"> 51 <span class="widget"> 52 <img src="passport.jpg" /><br /> 53 <input type="file" name="form.passport" /> 54 </span> 55 </tr> 48 56 <tr tal:condition="view/manage_applications"> 49 57 <td class="label"><label>Application Transition:</label></td> -
main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/interfaces.py
r6915 r7002 370 370 required = False, 371 371 ) 372 passport = ImageFile(373 title = u'Passport Photograph',374 #default = DEFAULT_PASSPORT_IMAGE_MALE,375 defaultFactory = default_passport_image,376 description = u'Maximun file size is 20 kB.',377 required = True,378 max_size = 20480,379 )372 #passport = ImageFile( 373 # title = u'Passport Photograph', 374 # #default = DEFAULT_PASSPORT_IMAGE_MALE, 375 # defaultFactory = default_passport_image, 376 # description = u'Maximun file size is 20 kB.', 377 # required = True, 378 # max_size = 20480, 379 # ) 380 380 381 381 # -
main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/tests/test_applicant.py
r6657 r7002 22 22 """Tests for applicants and related. 23 23 """ 24 import grok 25 import md5 24 26 import unittest 25 27 from StringIO import StringIO 26 28 from hurry.file.interfaces import IFileRetrieval 27 29 from zope.component import ( 28 provideAdapter, adapts, getGlobalSiteManager, provideUtility) 30 provideAdapter, adapts, getGlobalSiteManager, provideUtility, 31 queryUtility) 29 32 from zope.component.hooks import setSite 30 33 from zope.component.interfaces import IFactory … … 36 39 from waeup.sirp.image import WAeUPImageFile, createWAeUPImageFile 37 40 from waeup.sirp.image.interfaces import IWAeUPImageFile 41 from waeup.sirp.imagestorage import ImageStorageFileRetrieval, DefaultStorage 42 from waeup.sirp.interfaces import IFileStoreHandler, IFileStoreNameChooser 38 43 from waeup.sirp.applicants import ( 39 ResultEntry, Applicant, ApplicantFactory, ApplicantTraverser, 44 ResultEntry, Applicant, ApplicantFactory, get_regno_or_ac, 45 ApplicantImageStoreHandler, ApplicantImageNameChooser, 40 46 ) 41 47 from waeup.sirp.applicants.interfaces import ( 42 48 IResultEntry, IApplicant, 43 49 ) 44 from waeup.sirp.imagestorage import ImageStorageFileRetrieval 50 45 51 from waeup.sirp.testing import (FunctionalTestCase, FunctionalLayer) 46 52 … … 51 57 pass 52 58 59 class HelperTests(FunctionalTestCase): 60 61 layer = FunctionalLayer 62 63 def test_get_regno_or_ac(self): 64 # we can get reg_no or access_code of an applicants if it is set 65 appl1 = Applicant() 66 appl2 = Applicant() 67 appl2.reg_no = u'foo' 68 appl3 = Applicant() 69 appl3.access_code = u'bar' 70 appl4 = Applicant() 71 appl4.reg_no = u'foo' 72 appl4.access_code = u'bar' 73 self.assertTrue( 74 get_regno_or_ac(appl1) is None) 75 self.assertEqual( 76 get_regno_or_ac(appl2), u'foo') 77 self.assertEqual( 78 get_regno_or_ac(appl3), u'bar') 79 self.assertEqual( 80 get_regno_or_ac(appl4), u'foo') 81 return 82 83 def test_image_store_handler_util_accessible(self): 84 # we can get an IFileStoreHandler utility for applicants 85 handler = queryUtility(IFileStoreHandler, name='img-applicant') 86 self.assertTrue( 87 isinstance(handler, ApplicantImageStoreHandler)) 88 return 89 90 def test_image_store_handler(self): 91 store = DefaultStorage() 92 handler = queryUtility(IFileStoreHandler, name='img-applicant') 93 result1 = handler.pathFromFileID( 94 store, '/fake-root', '__img-applicant__sample.jpg') 95 result2 = handler.pathFromFileID( 96 store, '/fake-root', '__img-applicant__dir1/sample.jpg') 97 result3 = handler.pathFromFileID( 98 store, '/fake-root', '__img-applicant__dir1/dir2/sample.jpg') 99 self.assertEqual( 100 result1, '/fake-root/applicants/sample.jpg') 101 self.assertEqual( 102 result2, '/fake-root/applicants/dir1/sample.jpg') 103 self.assertEqual( 104 result3, '/fake-root/applicants/dir1/dir2/sample.jpg') 105 return 106 107 class ApplicantImageNameChooserTests(FunctionalTestCase): 108 109 layer = FunctionalLayer 110 111 def test_iface(self): 112 # make sure we implement promised interfaces 113 obj = ApplicantImageNameChooser(None) # needs a context normally 114 verify.verifyClass(IFileStoreNameChooser, ApplicantImageNameChooser) 115 verify.verifyObject(IFileStoreNameChooser, obj) 116 return 117 118 def test_name_chooser_available(self): 119 # we can get a name chooser for applicant objects as adapter 120 appl = Applicant() 121 chooser = IFileStoreNameChooser(appl) 122 self.assertTrue(chooser is not None) 123 return 124 125 def test_name_chooser_applicant_wo_container(self): 126 # we can get an image filename for applicants not in a container 127 appl = Applicant() 128 appl.reg_no = u'MY_REG_NO' 129 chooser = IFileStoreNameChooser(appl) 130 result = chooser.chooseName() 131 # the file would be stored in a ``_default`` directory. 132 self.assertEqual( 133 result, '__img-applicant___default/MY_REG_NO.jpg') 134 return 135 136 def test_name_chooser_applicant_w_container(self): 137 appl = Applicant() 138 appl.reg_no = u'MY_REG_NO' 139 fake_container = grok.Container() 140 fake_container.__name__ = 'folder' 141 fake_container['appl'] = appl 142 appl.__parent__ = fake_container 143 chooser = IFileStoreNameChooser(appl) 144 result = chooser.chooseName() 145 self.assertEqual( 146 result, '__img-applicant__folder/MY_REG_NO.jpg') 147 return 148 53 149 class ResultEntryTest(unittest.TestCase): 54 150 … … 87 183 return 88 184 89 def test_passport_no_site(self):185 def DISABLEDtest_passport_no_site(self): 90 186 # make sure we get a real image stored in passport attr 91 187 image = self.applicant.passport.file.read() … … 93 189 return 94 190 95 def test_passport_insite(self):191 def DISABLEDtest_passport_insite(self): 96 192 # make sure we get a real image in passport attr when inside a site. 97 193 # When an applicant is created 'inside a site', its passport … … 103 199 applicant = Applicant() 104 200 image = self.applicant.passport.file.read() 201 # The image is really the placeholder_m.jpg image 105 202 self.assertEqual(len(image), 2059) 203 self.assertEqual(md5.new(image).hexdigest(), 204 u'b48a1d39bbcb32e955d9ff2dea4ed0e6') 205 # As the applicant has no reg_no yet, the image data is 'empty' 206 self.assertEqual(self.applicant.passport.data, 207 '__img_appl__placeholder_m.jpg') 106 208 # The image contains a file_id instead of real image-data 107 209 self.assertEqual(self.applicant.passport.data, … … 118 220 # Install a IFileRetrieval utility that returns WAeUPImageFiles. 119 221 storage = ImageStorageFileRetrieval() 120 provideUtility(storage, IFileRetrieval)222 #provideUtility(storage, IFileRetrieval) 121 223 self.factory = ApplicantFactory() 122 224 return … … 137 239 implemented_by = self.factory.getInterfaces() 138 240 assert implemented_by.isOrExtends(IApplicant) 139 140 141 class ApplicantTraverserTest(FunctionalTestCase):142 143 layer = FunctionalLayer144 145 def setUp(self):146 super(ApplicantTraverserTest, self).setUp()147 provideAdapter(FakeImageLocation)148 self.applicant = Applicant()149 150 self.request = TestRequest('')151 return152 153 def tearDown(self):154 gsm = getGlobalSiteManager()155 gsm.unregisterAdapter(FakeImageLocation)156 super(ApplicantTraverserTest, self).tearDown()157 return158 159 def test_traverse_wo_passport(self):160 # Ask for some attribute not provided161 traverser = ApplicantTraverser(162 self.applicant, self.request163 )164 self.assertRaises(165 NotFound,166 traverser.publishTraverse, self.request, 'passport'167 )168 return169 170 def test_traverse_wo_image_passport_jpg(self):171 # Ask for applicant pic if we didn't provided one172 # We get a placeholder.173 traverser = ApplicantTraverser(174 self.applicant, self.request175 )176 result = traverser.publishTraverse(self.request, 'passport.jpg')177 self.assertTrue(isinstance(result, FakeImageLocation))178 return179 180 def test_traverse_w_image_passport_jpg(self):181 # Ask for applicant pic that's named 'passport.jpg'182 traverser = ApplicantTraverser(183 self.applicant, self.request184 )185 self.applicant.passport = createWAeUPImageFile(186 'nofile.jpg', StringIO('no-content'))187 self.applicant.passport.filename = 'mypic.jpg'188 result = traverser.publishTraverse(self.request, 'passport.jpg')189 self.assertTrue(isinstance(result, FakeImageLocation))190 return191 192 def test_traverse_w_image_some_jpg(self):193 # Ask for applicant pic by correct name194 traverser = ApplicantTraverser(195 self.applicant, self.request196 )197 self.applicant.passport = WAeUPImageFile('nofile.jpg', '')198 self.applicant.passport.filename = 'mypic.jpg'199 result = traverser.publishTraverse(self.request, 'mypic.jpg')200 self.assertTrue(isinstance(result, FakeImageLocation))201 return -
main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/tests/test_browser.py
r6816 r7002 28 28 from datetime import datetime, date, timedelta 29 29 from mechanize import LinkNotFoundError 30 from zope.component import createObject 30 from zope.component import createObject, getUtility 31 31 from zope.component.hooks import setSite, clearSite 32 32 from zope.security.interfaces import Unauthorized … … 37 37 from waeup.sirp.applicants.container import ApplicantsContainer 38 38 from waeup.sirp.applicants.applicant import Applicant 39 from waeup.sirp.interfaces import IExtFileStore, IFileStoreNameChooser 39 40 from waeup.sirp.university.faculty import Faculty 40 41 from waeup.sirp.university.department import Department … … 350 351 self.browser.getControl(name="SUBMIT").click() 351 352 pin = self.pins[2] 352 appl = self.getRootFolder()['app']['applicants']['app2009']353 appl = appl[pin]354 passp = appl.passport355 passp_len = len(passp.file.read())356 self.assertEqual(passp_len, PH_LEN)353 #appl = self.getRootFolder()['app']['applicants']['app2009'] 354 #appl = appl[pin] 355 #passp = appl.passport 356 #passp_len = len(passp.file.read()) 357 #self.assertEqual(passp_len, PH_LEN) 357 358 #image_url = "%s/%s" % (self.browser.url, 'placeholder.jpg') 358 359 image_url = "%s/%s" % (self.browser.url, 'passport.jpg') … … 639 640 # There is a correct <img> link included 640 641 self.assertTrue( 641 '<img src="p laceholder_m.jpg" />' in self.browser.contents)642 '<img src="passport.jpg" />' in self.browser.contents) 642 643 # Browsing the link shows a real image 643 self.browser.open(self.image_url('p laceholder_m.jpg'))644 self.browser.open(self.image_url('passport.jpg')) 644 645 self.assertEqual( 645 646 self.browser.headers['content-type'], 'image/jpeg') 646 647 self.assertEqual(len(self.browser.contents), PH_LEN) 647 648 648 def test_after_login_default_stored_in_imagestorage(self):649 def DISABLEDtest_after_login_default_stored_in_imagestorage(self): 649 650 # After login the applicants placeholder image is stored in 650 651 # an imagestorage … … 672 673 # There is a correct <img> link included 673 674 self.assertTrue( 674 '<img src="p laceholder_m.jpg" />' in self.browser.contents)675 '<img src="passport.jpg" />' in self.browser.contents) 675 676 # Browsing the link shows a real image 676 self.browser.open(self.image_url('p laceholder_m.jpg'))677 self.browser.open(self.image_url('passport.jpg')) 677 678 self.assertEqual( 678 679 self.browser.headers['content-type'], 'image/jpeg') 679 680 self.assertEqual(len(self.browser.contents), PH_LEN) 680 681 def test_after_submit_default_stored_in_imagestorage(self):682 # After submitting an applicant form the default image is683 # correctly stored in an imagestorage684 self.login()685 self.browser.getControl("Save").click() # submit form686 storage = self.app['images']687 self.assertEqual(688 [x for x in storage.keys()],689 [u'b48a1d39bbcb32e955d9ff2dea4ed0e6'])690 file_id = self.applicant.passport.data691 self.assertEqual(692 file_id, u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1')693 # The stored image can be fetched694 fd = storage.retrieveFile(file_id)695 file_len = len(fd.read())696 self.assertEqual(file_len, PH_LEN)697 681 698 682 def test_uploaded_image_browsable_w_errors(self): … … 709 693 # There is a correct <img> link included 710 694 self.assertTrue( 711 '<img src=" myphoto.jpg" />' in self.browser.contents)695 '<img src="passport.jpg" />' in self.browser.contents) 712 696 # Browsing the link shows a real image 713 self.browser.open(self.image_url(' myphoto.jpg'))697 self.browser.open(self.image_url('passport.jpg')) 714 698 self.assertEqual( 715 699 self.browser.headers['content-type'], 'image/jpeg') … … 726 710 file_ctrl.add_file(pseudo_image, filename='myphoto.jpg') 727 711 self.browser.getControl("Save").click() # submit form 728 storage = self.app['images'] 729 self.assertTrue( 730 u'18e57c7eac6ca7fb15b54b5b2bd4106d' in storage.keys()) 731 # The stored image can be fetched 732 fd = storage.retrieveFile(u'18e57c7eac6ca7fb15b54b5b2bd4106d-1') 733 file_len = len(fd.read()) 734 self.assertEqual(file_len, 31) 735 # The image uploaded belongs to the applicant 736 file_id = self.applicant.passport.data 712 storage = getUtility(IExtFileStore) 713 file_id = IFileStoreNameChooser(self.applicant).chooseName() 714 pseudo_image.seek(0) # reset our file data source 737 715 self.assertEqual( 738 file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1') 716 storage.getFile(file_id).read(), pseudo_image.read()) 717 return 739 718 740 719 def test_uploaded_image_browsable_wo_errors(self): … … 751 730 # There is a correct <img> link included 752 731 self.assertTrue( 753 '<img src=" myphoto.jpg" />' in self.browser.contents)732 '<img src="passport.jpg" />' in self.browser.contents) 754 733 # Browsing the link shows a real image 755 self.browser.open(self.image_url(' myphoto.jpg'))734 self.browser.open(self.image_url('passport.jpg')) 756 735 self.assertEqual( 757 736 self.browser.headers['content-type'], 'image/jpeg') … … 770 749 self.browser.getControl("Save").click() # submit form 771 750 storage = self.app['images'] 772 s elf.assertTrue(773 u'18e57c7eac6ca7fb15b54b5b2bd4106d' in storage.keys())751 storage = getUtility(IExtFileStore) 752 file_id = IFileStoreNameChooser(self.applicant).chooseName() 774 753 # The stored image can be fetched 775 fd = storage.retrieveFile(u'18e57c7eac6ca7fb15b54b5b2bd4106d-1') 776 #fd = storage.retrieveFile(file_id) 754 fd = storage.getFile(file_id) 777 755 file_len = len(fd.read()) 778 756 self.assertEqual(file_len, 31) 779 # The image uploaded belongs to the applicant780 file_id = self.applicant.passport.data781 self.assertEqual(782 file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')783 757 784 758 def test_uploaded_images_equal(self): … … 797 771 file_ctrl = ctrl.mech_control 798 772 file_ctrl.add_file(pseudo_image, filename='myphoto.jpg') 799 passport0 = self.applicant.passport 773 file_id = IFileStoreNameChooser(self.applicant).chooseName() 774 setSite(self.app) 775 passport0 = getUtility(IExtFileStore).getFile(file_id) 800 776 self.browser.getControl("Save").click() # submit form with changed pic 801 passport1 = self.applicant.passport777 passport1 = getUtility(IExtFileStore).getFile(file_id).read() 802 778 self.browser.getControl("Save").click() # submit form w/o changes 803 passport2 = self.applicant.passport 779 passport2 = getUtility(IExtFileStore).getFile(file_id).read() 780 self.assertTrue(passport0 is None) 804 781 self.assertTrue(passport0 != passport1) 805 782 self.assertTrue(passport1 == passport2) 806 self.assertTrue(passport1 is passport2)807 783 return 808 784
Note: See TracChangeset for help on using the changeset viewer.