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
def attachments(self): """ Get preview image for content-related updates""" if self.is_preview_supported(): docconv = IDocconv(self.context.context) if docconv.has_thumbs(): return [self.context.context.absolute_url() + '/docconv_image_thumb.jpg']
def render(self): docconv = IDocconv(self.context) if not docconv.has_pdf(): return self.request.RESPONSE.redirect( self.context.absolute_url() + '/pdf-not-available') pdfversion = docconv.get_pdf() R = self.request.RESPONSE R.setHeader('content-type', 'application/pdf') R.setHeader( 'content-disposition', 'attachment; filename="%s"' % '.'.join( (self.context.getId(), u'pdf')).encode('utf8')) if isinstance(pdfversion, basestring): length = len(pdfversion) R.setHeader('content-length', length) return pdfversion else: length = pdfversion.get_size(self.context) R.setHeader('content-length', length) blob = pdfversion.get(self.context, raw=True) charset = 'utf-8' return blob.index_html( REQUEST=self.request, RESPONSE=R, charset=charset )
def item2attachments(self, item): ''' Take the attachment sotrage item and transform it into an attachment ''' docconv = IDocconv(item) item_url = '/'.join(( self.attachment_base_url, item.getId(), )) if docconv.has_thumbs(): url = '/'.join((item_url, 'thumb')) elif isinstance(item, Image): images = api.content.get_view( 'images', item.aq_base, self.request, ) url = '/'.join(( item_url, images.scale(scale='preview').url.lstrip('/'), )) else: # We need a better fallback image. See #122 url = '/'.join(( api.portal.get().absolute_url(), '++theme++ploneintranet.theme/generated/media/logo.svg' )) return {'img_src': url, 'link': item_url}
def generate_attachment_preview_images(obj): if (IAttachmentStoragable is not None and IAttachmentStoragable.providedBy(obj)): attachment_storage = IAttachmentStorage(obj) for att_id in attachment_storage.keys(): docconv = IDocconv(attachment_storage.get(att_id)) if not docconv.has_thumbs(): docconv.generate_all()
def test_fetch_docconv_data(self): fetchPreviews(self.testfile, virtual_url_parts=['dummy', ], vr_path='/plone') docconv = IDocconv(self.testfile) self.assertTrue(docconv.has_pdf()) self.assertTrue(docconv.has_previews()) self.assertTrue(docconv.has_thumbs())
def attachments(self): """ Get preview image for content-related updates""" if self.is_preview_supported(): docconv = IDocconv(self.context.context) if docconv.has_thumbs(): base_url = self.context.context.absolute_url() return [dict( img_src="{0}/docconv_image_thumb.jpg".format(base_url), link=base_url)]
def attachments(self): """ Get preview image for content-related updates""" if self.is_preview_supported(): docconv = IDocconv(self.context.context) if docconv.has_thumbs(): return [ self.context.context.absolute_url() + '/docconv_image_thumb.jpg' ]
def test_event_handler(self): portal = self.layer['portal'] setRoles(portal, TEST_USER_ID, ('Manager',)) fileob = api.content.create( type='File', title=u"Test File", file=NamedBlobFile(data=self.filedata, filename=TEST_FILENAME), container=self.workspace) docconv = IDocconv(fileob) self.assertTrue(docconv.has_previews())
def number_of_file_previews(self): """The number of previews generated for a file.""" context = aq_inner(self.context) if context.portal_type != 'File': return try: docconv = IDocconv(self.context) except TypeError: # TODO: prevent this form happening in tests return if docconv.has_previews(): return docconv.get_number_of_pages()
def update(self): docconv = IDocconv(self.context) if docconv.generate_all(): self.message = ( 'Your request for a PDF version of this document ' 'is being processed. It will take a few minutes until it is ' 'ready for download. Please check back later.') else: self.message = ( 'A PDF version of this document has been requested' ' and is in preparation. It will be available for download ' 'shortly. Please select the download button from the gearbox' ' again in a few minutes.')
def get_docconv_thumbs_urls(self, attachment): """ If we have a IDocconv adapted object, we ask docconv for thumbs urls """ docconv = IDocconv(attachment, None) if not docconv: return [] base_url = "%s/docconv_image_thumb.jpg?page=" % (docconv.context.absolute_url()) pages = docconv.get_number_of_pages() if pages <= 0: # we have no previews when pages is 0 or -1 return [] else: return [(base_url + str(i + 1)) for i in range(pages)]
def test_document(self): testdoc = api.content.create( type='Document', id='test-doc', title=u"Test Document", text=u'The main text', container=self.workspace) fetchPreviews(testdoc, virtual_url_parts=['dummy', ], vr_path='/plone') docconv = IDocconv(testdoc) self.assertTrue(docconv.has_pdf()) self.assertTrue(docconv.has_previews()) self.assertTrue(docconv.has_thumbs())
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 attachments(self): """ Get preview images for status update attachments """ if all([ self.is_attachment_supported(), self.is_preview_supported(), IAttachmentStoragable.providedBy(self.status), ]): storage = IAttachmentStorage(self.status) attachments = storage.values() for attachment in attachments: docconv = IDocconv(attachment) if docconv.has_thumbs(): url = api.portal.get().absolute_url() yield ('{portal_url}/@@status-attachments/{status_id}/' '{attachment_id}/thumb').format( portal_url=url, status_id=self.status.getId(), attachment_id=attachment.getId())
def test_docconv_adapter_on_new_object(self): docconv = IDocconv(self.testfile) self.assertFalse(docconv.has_pdf()) self.assertFalse(docconv.has_previews()) self.assertFalse(docconv.has_thumbs()) self.assertEquals(docconv.get_pdf(), None) self.assertEquals(docconv.get_previews(), None) self.assertEquals(docconv.get_thumbs(), None)
def attachments(self): """ Get preview images for status update attachments """ if not self.is_attachment_supported(): return [] if not self.is_preview_supported(): return [] if not IAttachmentStoragable.providedBy(self.status): return [] storage = IAttachmentStorage(self.status) items = storage.values() if not items: return [] attachments = [] portal_url = api.portal.get().absolute_url() base_url = '{portal_url}/@@status-attachments/{status_id}'.format( portal_url=portal_url, status_id=self.status.getId(), ) for item in items: item_url = '/'.join((base_url, item.getId())) docconv = IDocconv(item) if docconv.has_thumbs(): url = '/'.join((item_url, 'thumb')) elif isinstance(item, Image): images = api.content.get_view( 'images', item.aq_base, self.request, ) url = '/'.join(( item_url, images.scale(scale='preview').url.lstrip('/') )) else: # We need a better fallback image. See #See #122 url = '/'.join(( api.portal.get().absolute_url(), '++theme++ploneintranet.theme/generated/media/logo.svg')) if url: attachments.append(dict(img_src=url, link=item_url)) return attachments
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
def render_attachments(self, attachments): """replaces ploneintranet/docconv/client/view.py helpers""" attachment = attachments[self.attachment_id] if not self.preview_type or self.preview_type in ("pdf-not-available", "request-pdf"): return self._render_nopreview(attachment) docconv = IDocconv(attachment) # upload stage if self.preview_type == "docconv_image_thumb.jpg": if docconv.has_thumbs(): previews = docconv.get_thumbs() imgdata = self._get_page_imgdata(previews) return self._prepare_imagedata(attachment, imgdata) elif self.preview_type == "docconv_image_preview.jpg": if docconv.has_previews(): previews = docconv.get_previews() imgdata = self._get_page_imgdata(previews) return self._prepare_imagedata(attachment, imgdata) elif self.preview_type == "pdf": if docconv.has_pdf(): pdfdata = docconv.get_pdf() return self._prepare_pdfdata(pdfdata) # normal view stage elif 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