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)
Example #2
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)
Example #3
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 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_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)
Example #6
0
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
Example #7
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)
Example #8
0
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()))
Example #9
0
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()))