Commit dca3d6d8 authored by Nicolas Delaby's avatar Nicolas Delaby

add support of HTML conversion to ODF output or format supported by oood

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@21034 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent d68d440c
...@@ -298,31 +298,7 @@ class EmailDocument(File, TextDocument): ...@@ -298,31 +298,7 @@ class EmailDocument(File, TextDocument):
index_html = TextDocument.index_html index_html = TextDocument.index_html
security.declareProtected(Permissions.View, 'convert') convert = TextDocument.convert
def convert(self, format, **kw):
"""
Convert text using portal_transforms
"""
# Accelerate rendering in Web mode
_setCacheHeaders(_ViewEmulator().__of__(self), {'format' : format})
# Return the raw content
if format == 'raw':
return 'text/plain', self.getTextContent()
mime_type = getToolByName(self, 'mimetypes_registry').lookupExtension('name.%s' % format)
src_mimetype = self.getTextFormat(DEFAULT_TEXT_FORMAT)
if not src_mimetype.startswith('text/'):
src_mimetype = 'text/%s' % src_mimetype
# check if document has set text_content and convert if necessary
text_content = self.getTextContent()
if text_content is not None:
portal_transforms = getToolByName(self, 'portal_transforms')
return mime_type, portal_transforms.convertTo(mime_type,
text_content,
object = self,
mimetype = src_mimetype)
else:
# text_content is not set, return empty string instead of None
return mime_type, ''
security.declareProtected(Permissions.AccessContentsInformation, 'hasBaseData') security.declareProtected(Permissions.AccessContentsInformation, 'hasBaseData')
def hasBaseData(self): def hasBaseData(self):
......
...@@ -147,7 +147,7 @@ class TextDocument(Document, TextContent): ...@@ -147,7 +147,7 @@ class TextDocument(Document, TextContent):
security.declareProtected(Permissions.View, 'convert') security.declareProtected(Permissions.View, 'convert')
def convert(self, format, **kw): def convert(self, format, **kw):
""" """
Convert text using portal_transforms Convert text using portal_transforms or oood
""" """
# Accelerate rendering in Web mode # Accelerate rendering in Web mode
_setCacheHeaders(_ViewEmulator().__of__(self), {'format' : format}) _setCacheHeaders(_ViewEmulator().__of__(self), {'format' : format})
...@@ -161,6 +161,11 @@ class TextDocument(Document, TextContent): ...@@ -161,6 +161,11 @@ class TextDocument(Document, TextContent):
# check if document has set text_content and convert if necessary # check if document has set text_content and convert if necessary
text_content = self.getTextContent() text_content = self.getTextContent()
if text_content is not None: if text_content is not None:
if src_mimetype == 'text/html':
#Works with oood
kw['REQUEST'] = self.REQUEST
return self._asFormat(text_content, format, src_mimetype, **kw)
else:
portal_transforms = getToolByName(self, 'portal_transforms') portal_transforms = getToolByName(self, 'portal_transforms')
return mime_type, portal_transforms.convertTo(mime_type, return mime_type, portal_transforms.convertTo(mime_type,
text_content, text_content,
...@@ -170,6 +175,48 @@ class TextDocument(Document, TextContent): ...@@ -170,6 +175,48 @@ class TextDocument(Document, TextContent):
# text_content is not set, return empty string instead of None # text_content is not set, return empty string instead of None
return mime_type, '' return mime_type, ''
def _asFormat(self, ooo, format, src_mimetype, REQUEST=None, batch_mode=0):
"""
Transform text_content into ODF or many output format supported by oood
"""
# Create a temp OOoDocument
from Products.ERP5Type.Document import newTempOOoDocument
tmp_ooo = newTempOOoDocument(self, self.title_or_id())
tmp_ooo.edit(base_data=ooo,
fname=self.title_or_id(),
source_reference=self.title_or_id(),
base_content_type=src_mimetype,)
tmp_ooo.oo_data = ooo
#Convert it into ODT
tmp_ooo.convert('odt')
if format == 'pdf' and not batch_mode:
# Slightly different implementation
# now convert it to pdf
tgts = [x[1] for x in tmp_ooo.getTargetFormatItemList()
if x[1].endswith('pdf')]
if len(tgts) > 1:
REQUEST.RESPONSE.setHeader('Content-type', 'text/html')
REQUEST.RESPONSE.setHeader('Content-disposition', 'inline;filename=%s.pdf' % self.title_or_id())
raise ValueError, 'multiple pdf formats found - this shouldnt happen'
if len(tgts) == 0:
REQUEST.RESPONSE.setHeader('Content-type', 'text/html')
REQUEST.RESPONSE.setHeader('Content-disposition', 'inline;filename=%s.pdf' % self.title_or_id())
raise ValueError, 'no pdf format found'
fmt = tgts[0]
#Apply transformation in output format
mime, data = tmp_ooo.convert(fmt)
if REQUEST is not None:
REQUEST.RESPONSE.setHeader('Content-type', 'application/pdf')
REQUEST.RESPONSE.setHeader('Content-disposition', 'attachment;filename=%s.pdf' % self.title_or_id())
return data
#Apply transformation in output format
mime, data = tmp_ooo.convert(format)
if REQUEST is not None and not batch_mode:
REQUEST.RESPONSE.setHeader('Content-type', mime)
REQUEST.RESPONSE.setHeader('Content-disposition', 'attachment;filename=%s.%s' % (self.title_or_id(),format))
# FIXME the above lines should return zip format when html was requested
return data
def __call__(self): def __call__(self):
_setCacheHeaders(_ViewEmulator().__of__(self), {}) _setCacheHeaders(_ViewEmulator().__of__(self), {})
return Document.__call__(self) return Document.__call__(self)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment