Example #1
0
    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_extract_and_add_attachments(self):
     file_field = self._create_test_file_field()
     extract_and_add_attachments(file_field, self.document)
     attachments = IAttachmentStorage(self.document)
     self.assertEquals(len(attachments.values()), 1)
     res = attachments.get(file_field.filename)
     self.assertEquals(res.id, file_field.filename)
Example #3
0
    def __call__(self):

        if not self.status_id:
            return self
        container = PLONESOCIAL.microblog
        status = container.get(self.status_id)
        if not self.attachment_id:
            # do we want to be able to traverse to the status update itself?
            # Returning only the id for now
            return self.status_id
        attachments = IAttachmentStorage(status)
        attachment = attachments.get(self.attachment_id)
        if not self.preview_type:
            self.request.response.setHeader('content-type',
                                            attachment.getContentType())
            self.request.response.setHeader(
                'content-disposition', 'inline; '
                'filename="{0}"'.format(self.attachment_id.encode('utf8')))
            return attachment
        if IDocconv is not None:
            docconv = IDocconv(attachment)
            if self.preview_type == 'thumb':
                if docconv.has_thumbs():
                    return self._prepare_imagedata(attachment,
                                                   docconv.get_thumbs()[0])
            elif self.preview_type == 'preview':
                if docconv.has_previews():
                    return self._prepare_imagedata(attachment,
                                                   docconv.get_previews()[0])
        raise NotFound
Example #4
0
 def test_extract_and_add_attachments(self):
     file_field = self._create_test_file_field()
     extract_and_add_attachments(file_field, self.document)
     attachments = IAttachmentStorage(self.document)
     self.assertEquals(len(attachments.values()), 1)
     res = attachments.get(file_field.filename)
     self.assertEquals(res.id, file_field.filename)
    def __call__(self):

        if not self.status_id:
            return self
        container = PLONESOCIAL.microblog
        status = container.get(self.status_id)
        if not self.attachment_id:
            # do we want to be able to traverse to the status update itself?
            # Returning only the id for now
            return self.status_id
        attachments = IAttachmentStorage(status)
        attachment = attachments.get(self.attachment_id)
        if not self.preview_type:
            self.request.response.setHeader(
                'content-type', attachment.getContentType())
            self.request.response.setHeader(
                'content-disposition', 'inline; '
                'filename="{0}"'.format(
                    self.attachment_id.encode('utf8')))
            return attachment
        if IDocconv is not None:
            docconv = IDocconv(attachment)
            if self.preview_type == 'thumb':
                if docconv.has_thumbs():
                    return self._prepare_imagedata(
                        attachment, docconv.get_thumbs()[0])
            elif self.preview_type == 'preview':
                if docconv.has_previews():
                    return self._prepare_imagedata(
                        attachment, docconv.get_previews()[0])
        raise NotFound
Example #6
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 __call__(self):

        if not self.status_id:
            return self

        container = PLONEINTRANET.microblog
        status = container.get(self.status_id)
        if not self.attachment_id:
            # do we want to be able to traverse to the status update itself?
            # Returning only the id for now
            return self.status_id
        attachments = IAttachmentStorage(status)
        attachment = attachments.get(self.attachment_id)
        if not self.preview_type:
            primary_field = IPrimaryFieldInfo(attachment).value
            mimetype = primary_field.contentType
            data = primary_field.data
            self.request.response.setHeader(
                'content-type', mimetype)
            self.request.response.setHeader(
                'content-disposition', 'inline; '
                'filename="{0}"'.format(
                    safe_unicode(self.attachment_id).encode('utf8')))
            return data
        if IDocconv is not None:
            docconv = IDocconv(attachment)
            if self.preview_type == 'thumb':
                if docconv.has_thumbs():
                    return self._prepare_imagedata(
                        attachment, docconv.get_thumbs()[0])
            elif self.preview_type == 'preview':
                if docconv.has_previews():
                    return self._prepare_imagedata(
                        attachment, docconv.get_previews()[0])
            elif self.preview_type == '@@images':
                images = api.content.get_view(
                    'images',
                    attachment.aq_base,
                    self.request,
                )
                return self._prepare_imagedata(
                    attachment,
                    str(images.scale(scale='preview').data.data)
                )

        raise NotFound
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())))
Example #10
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
 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_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))
Example #13
0
    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()
Example #14
0
    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()