def __call__(self): token = self.request.get('attachment-form-token') uploaded_attachments = self.request.get('form.widgets.attachments') if not uploaded_attachments: return self.index() normalizer = getUtility(IURLNormalizer) self.attachments = [] attachments = IAttachmentStorage(self.context) attachment_objs = utils.extract_attachments(uploaded_attachments) for obj in attachment_objs: obj.id = normalizer.normalize(u'{0}-{1}'.format(token, obj.id)) try: attachments.add(obj) except DuplicateIDError: pass obj = attachments.get(obj.id) try: handle_file_creation(obj, None, async=False) except Exception as e: log.warn('Could not get previews for attachment: {0}, ' u'{1}: {2}'.format(obj.id, e.__class__.__name__, e)) self.attachments.append(obj) return self.index()
def test_attachments(self): su = StatusUpdate('foo bar') attachments = IAttachmentStorage(su) f = ATFile('data.dat') attachments.add(f) self.assertEqual([k for k in attachments.keys()], [f.getId()]) attachments.remove(f.getId()) self.assertEqual(len(attachments.keys()), 0)
def test_attachments(self): su = StatusUpdate("foo bar") attachments = IAttachmentStorage(su) f = ATFile("data.dat") attachments.add(f) self.assertEqual([k for k in attachments.keys()], [f.getId()]) attachments.remove(f.getId()) self.assertEqual(len(attachments.keys()), 0)
def test_temporary_attachment(self): token = "{0}-{1}".format(TEST_USER_ID, datetime.utcnow().strftime('%Y%m%d%H%M%S%f')) attachment = self._create_test_temp_attachment(token) temp_attachments = IAttachmentStorage(self.workspace) temp_attachments.add(attachment) file_field = self._create_test_file_field() res = pop_temporary_attachment(self.workspace, file_field, token) self.assertEquals(res.id, attachment.id) self.assertTrue(res.file.size > 0) clean_up_temporary_attachments(self.workspace, maxage=0) self.assertEquals(len(temp_attachments.keys()), 0)
def test_temporary_attachment(self): token = "{0}-{1}".format( TEST_USER_ID, datetime.utcnow().strftime('%Y%m%d%H%M%S%f')) attachment = self._create_test_temp_attachment(token) temp_attachments = IAttachmentStorage(self.workspace) temp_attachments.add(attachment) file_field = self._create_test_file_field() res = pop_temporary_attachment(self.workspace, file_field, token) self.assertEquals(res.id, attachment.id) self.assertTrue(res.file.size > 0) clean_up_temporary_attachments(self.workspace, maxage=0) self.assertEquals(len(temp_attachments.keys()), 0)
def test_extract_and_add_attachments_with_token(self): token = "{0}-{1}".format(TEST_USER_ID, datetime.utcnow().strftime('%Y%m%d%H%M%S%f')) temp_attachment = self._create_test_temp_attachment(token) temp_attachments = IAttachmentStorage(self.workspace) temp_attachments.add(temp_attachment) file_field = self._create_test_file_field() extract_and_add_attachments(file_field, self.document, self.workspace, token) attachments = IAttachmentStorage(self.document) self.assertEquals(len(attachments.values()), 1) self.assertTrue(file_field.filename in attachments.keys()) res = attachments.get(file_field.filename) self.assertEquals(res.id, file_field.filename) self.assertTrue('/'.join(res.getPhysicalPath()).startswith('/'.join( self.workspace.getPhysicalPath())))
def test_extract_and_add_attachments_with_token(self): token = "{0}-{1}".format( TEST_USER_ID, datetime.utcnow().strftime('%Y%m%d%H%M%S%f')) temp_attachment = self._create_test_temp_attachment(token) temp_attachments = IAttachmentStorage(self.workspace) temp_attachments.add(temp_attachment) file_field = self._create_test_file_field() extract_and_add_attachments( file_field, self.document, self.workspace, token) attachments = IAttachmentStorage(self.document) self.assertEquals(len(attachments.values()), 1) self.assertTrue(file_field.filename in attachments.keys()) res = attachments.get(file_field.filename) self.assertEquals(res.id, file_field.filename) self.assertTrue( '/'.join(res.getPhysicalPath()).startswith( '/'.join(self.workspace.getPhysicalPath())))
def test_remove(self): """ """ question = createObject('slc.underflow.question') attachments = IAttachmentStorage(question) self.assertEqual(len(attachments.keys()), 0) self.assertEqual(len(attachments.values()), 0) for fname in ['data1.dat', 'data2.dat']: attachments.add(file.ATFile(fname)) self.assertEqual(len(attachments.keys()), 2) self.assertEqual(len(attachments.values()), 2) self.assertRaises(KeyError, attachments.remove, 'data3.dat') attachments.remove('data1.dat') self.assertEqual(len(attachments.keys()), 1) self.assertTrue('data2.dat' in attachments.keys()) attachments.remove('data2.dat') self.assertEqual(len(attachments.keys()), 0)
def test_remove(self): """ """ question = createObject('Document') alsoProvides(question, IAttachmentStoragable) attachments = IAttachmentStorage(question) self.assertEqual(len(attachments.keys()), 0) self.assertEqual(len(attachments.values()), 0) for fname in ['data1.dat', 'data2.dat']: attachments.add(file.ATFile(fname)) self.assertEqual(len(attachments.keys()), 2) self.assertEqual(len(attachments.values()), 2) self.assertRaises(KeyError, attachments.remove, 'data3.dat') attachments.remove('data1.dat') self.assertEqual(len(attachments.keys()), 1) self.assertTrue('data2.dat' in attachments.keys()) attachments.remove('data2.dat') self.assertEqual(len(attachments.keys()), 0)
def __call__(self): token = self.request.get('attachment-form-token') attachments = self.request.get('form.widgets.attachments') self.attachments = [] if attachments: temp_attachments = IAttachmentStorage(self.context) attachment_objs = utils.extract_attachments(attachments) for obj in attachment_objs: obj.id = '{0}-{1}'.format(token, obj.id) temp_attachments.add(obj) obj = temp_attachments.get(obj.id) if IPreviewFetcher is not None: try: IPreviewFetcher(obj)() except Exception as e: log.warn('Could not get previews for attachment: {0}, ' '{1}: {2}'.format( obj.id, e.__class__.__name__, e)) self.attachments.append(obj) return self.index()
def test_traverse(self): id = self.workspace.invokeFactory( 'slc.underflow.question', 'question', title=u'Question') question = self.workspace._getOb(id) attachments = IAttachmentStorage(question) f = file.ATFile('data1.dat') attachments.add(f) response = self.portal.restrictedTraverse( '%s/++attachments++default/data1.dat' % ('/'.join(question.getPhysicalPath()))) self.assertEqual(f, response) # Test traversal to a non-existing attachment self.assertRaises( NotFound, self.portal.restrictedTraverse, '%s/++attachments++default/non-existing.dat' % ('/'.join(question.getPhysicalPath())))
def test_add(self): """ """ comment1 = createObject('plone.Comment') attachments = IAttachmentStorage(comment1) self.assertEqual(len(attachments.keys()), 0) self.assertEqual(len(attachments.values()), 0) f = file.ATFile('data.dat') attachments.add(f) self.assertEqual([k for k in attachments.keys()], [f.getId()]) self.assertEqual([v for v in attachments.values()], [f]) # DuplicateIDError is thrown when an object with the same id is # added again. self.assertRaises(DuplicateIDError, attachments.add, f) i = image.ATImage('image.jpg') attachments.add(i) self.assertEqual(len(attachments.keys()), 2) self.assertEqual(len(attachments.values()), 2) self.assertTrue(i.getId()in attachments.keys()) self.assertTrue(i in attachments.values())
def test_path(self): id = self.workspace.invokeFactory( 'slc.underflow.question', 'question', title=u'Question') question = self.workspace._getOb(id) attachments = IAttachmentStorage(question) f = file.ATFile('data1.dat') attachments.add(f) attachment_path = attachments.get('data1.dat').getPhysicalPath() self.assertEquals( '/'.join(attachment_path), '%s/++attachments++default/data1.dat' % ('/'.join(question.getPhysicalPath()))) response = self.portal.restrictedTraverse(attachment_path) self.assertEqual( '/'.join(response.getPhysicalPath()), '/'.join(attachment_path))
def test_add(self): """ """ doc1 = createObject('Document') alsoProvides(doc1, IAttachmentStoragable) attachments = IAttachmentStorage(doc1) self.assertEqual(len(attachments.keys()), 0) self.assertEqual(len(attachments.values()), 0) f = file.ATFile('data.dat') attachments.add(f) self.assertEqual([k for k in attachments.keys()], [f.getId()]) self.assertEqual([v for v in attachments.values()], [f]) # DuplicateIDError is thrown when an object with the same id is # added again. self.assertRaises(DuplicateIDError, attachments.add, f) i = image.ATImage('image.jpg') attachments.add(i) self.assertEqual(len(attachments.keys()), 2) self.assertEqual(len(attachments.values()), 2) self.assertTrue(i.getId() in attachments.keys()) self.assertTrue(i in attachments.values())
def __call__(self): token = self.request.get("attachment-form-token") uploaded_attachments = self.request.get("form.widgets.attachments") if not uploaded_attachments: return self.index() normalizer = getUtility(IURLNormalizer) self.attachments = [] attachments = IAttachmentStorage(self.context) attachment_objs = utils.extract_attachments(uploaded_attachments) for obj in attachment_objs: obj.id = normalizer.normalize(u"{0}-{1}".format(token, obj.id)) attachments.add(obj) obj = attachments.get(obj.id) try: IPreviewFetcher(obj)() except Exception as e: log.warn( "Could not get previews for attachment: {0}, " u"{1}: {2}".format(obj.id, e.__class__.__name__, e) ) self.attachments.append(obj) return self.index()
def __call__(self): token = self.request.get('attachment-form-token') uploaded_attachments = self.request.get('form.widgets.attachments') if not uploaded_attachments: return self.index() normalizer = getUtility(IURLNormalizer) self.attachments = [] attachments = IAttachmentStorage(self.context) attachment_objs = utils.extract_attachments(uploaded_attachments) for obj in attachment_objs: obj.id = normalizer.normalize(u'{0}-{1}'.format(token, obj.id)) attachments.add(obj) obj = attachments.get(obj.id) try: handle_file_creation(obj, None) except Exception as e: log.warn('Could not get previews for attachment: {0}, ' u'{1}: {2}'.format( obj.id, e.__class__.__name__, e)) self.attachments.append(obj) return self.index()