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_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 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_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 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 clean_up_temporary_attachments(workspace, maxage=1): temp_attachments = IAttachmentStorage(workspace) for key in temp_attachments.keys(): keyparts = key.split('-') datestr = keyparts[1] try: date = datetime.strptime(datestr, '%Y%m%d%H%M%S%f') except ValueError: date = datetime.min # No proper datestr, treat as old if datetime.now() - date > timedelta(maxage): temp_attachments.remove(key) log.info('Cleaned up temporary attachment {0} from ' '{1}'.format(key, workspace.Title()))
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 pop_temporary_attachment(workspace, file_field, token): """ Replace a temporary attachment on the workspace with the uploaded data """ temp_attachments = IAttachmentStorage(workspace) temp_id = '{0}-{1}'.format(token, file_field.filename) if temp_id in temp_attachments.keys(): temp_att = aq_base(temp_attachments.get(temp_id)) temp_att.id = file_field.filename temp_att.file = NamedBlobFile( data=file_field.read(), filename=file_field.filename.decode('utf-8'), ) temp_attachments.remove(temp_id) return temp_att return None
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 pop_temporary_attachment(workspace, file_field, token): """ Replace a temporary attachment on the workspace with the uploaded data """ temp_attachments = IAttachmentStorage(workspace) temp_id = getUtility(IURLNormalizer).normalize(u'{0}-{1}'.format( token, safe_unicode(file_field.filename))) if temp_id in temp_attachments.keys(): temp_att = aq_base(temp_attachments.get(temp_id)) temp_att.id = file_field.filename temp_att.file = NamedBlobFile( data=file_field.read(), filename=file_field.filename.decode('utf-8'), ) temp_attachments.remove(temp_id) return temp_att return None
def clean_up_temporary_attachments(workspace, maxage=1): """Garbage collect temporary attachments on a workspace -- these are used while creating a statusupdate but already removed when storing the statusupdate. This method removes any remaining temp attachments. maxage is age in days beyond which attachments are removed. """ temp_attachments = IAttachmentStorage(workspace) for key in temp_attachments.keys(): keyparts = key.split('-') datestr = keyparts[1] try: date = datetime.strptime(datestr, '%Y%m%d%H%M%S%f') except ValueError: date = datetime.min # No proper datestr, treat as old if datetime.now() - date > timedelta(maxage): temp_attachments.remove(key) log.info('Cleaned up temporary attachment {0} from ' '{1}'.format(key, workspace.Title()))
def clean_up_temporary_attachments(workspace, maxage=1): """Garbage collect temporary attachments on a workspace -- these are used while creating a statusupdate but already removed when storing the statusupdate. This method removes any remaining temp attachments. maxage is age in days beyond which attachments are removed. """ temp_attachments = IAttachmentStorage(workspace) for key in temp_attachments.keys(): keyparts = key.split('-') datestr = keyparts[1] try: date = datetime.strptime(datestr, '%Y%m%d%H%M%S%f') except ValueError: date = datetime.min # No proper datestr, treat as old if datetime.now() - date > timedelta(maxage): temp_attachments.remove(key) log.info('Cleaned up temporary attachment {0} from ' '{1}'.format(key, workspace.Title()))