Commit 10c8f4f2 authored by Jérome Perrin's avatar Jérome Perrin

implement the two following points (the implementation is not so clean,

but this features is really needed, so I hoave to commit it "as is")

WARNING:
  - In order to render in UTF-8, Template must contain the expression:
    tal:define="dummy python:
request.RESPONSE.setHeader('Content-Type','text/xml;; charset=utf-8')"

TODO:
  - upload of OOo documents must be able to extract content.xml
    from the archive, remove DTD definition and include
    CR/LF to produce a nice looking XML source.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@5489 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 5b64ff1d
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
############################################################################## ##############################################################################
from Products.CMFCore.utils import getToolByName from Products.CMFCore.utils import getToolByName
from types import StringType
from Products.CMFCore.FSPageTemplate import FSPageTemplate from Products.CMFCore.FSPageTemplate import FSPageTemplate
from Products.CMFCore.DirectoryView import registerFileExtension, registerMetaType from Products.CMFCore.DirectoryView import registerFileExtension, registerMetaType
from Products.Formulator.Form import BasicForm from Products.Formulator.Form import BasicForm
...@@ -86,16 +87,6 @@ class OOoTemplate(ZopePageTemplate): ...@@ -86,16 +87,6 @@ class OOoTemplate(ZopePageTemplate):
A page template which is able to embed and OpenOffice A page template which is able to embed and OpenOffice
file (zip archive) and replace content.xml at render time file (zip archive) and replace content.xml at render time
with XML dynamically generated through TAL/TALES/METAL expressions with XML dynamically generated through TAL/TALES/METAL expressions
WARNING:
- In order to render in UTF-8, Template must contain the expression:
tal:define="dummy python:
request.RESPONSE.setHeader('Content-Type','text/xml;; charset=utf-8')"
TODO:
- upload of OOo documents must be able to extract content.xml
from the archive, remove DTD definition and include
CR/LF to produce a nice looking XML source.
""" """
meta_type = "ERP5 OOo Template" meta_type = "ERP5 OOo Template"
icon = "www/OOo.png" icon = "www/OOo.png"
...@@ -126,9 +117,27 @@ class OOoTemplate(ZopePageTemplate): ...@@ -126,9 +117,27 @@ class OOoTemplate(ZopePageTemplate):
) )
security.declareProtected('View management screens', 'formSettings') security.declareProtected('View management screens', 'formSettings')
formSettings = PageTemplateFile('www/formSettings', globals(), __name__='formSettings') formSettings = PageTemplateFile('www/formSettings', globals(),
__name__='formSettings')
formSettings._owner = None formSettings._owner = None
def pt_upload(self, REQUEST, file=''):
"""Replace the document with the text in file."""
if SUPPORTS_WEBDAV_LOCKS and self.wl_isLocked():
raise ResourceLockedError, "File is locked via WebDAV"
if type(file) is not StringType:
if not file: raise ValueError, 'File not specified'
file = file.read()
if file.startswith("PK") : # FIXME: this condition is probably not enough
# this is a OOo zip file, extract the content
builder = OOoBuilder(file)
self.content_type = builder.getMimeType()
file = builder.prepareContentXml()
return ZopePageTemplate.pt_upload(self, REQUEST, file)
security.declareProtected('Change Page Templates', 'doSettings') security.declareProtected('Change Page Templates', 'doSettings')
def doSettings(self, REQUEST, title, ooo_stylesheet): def doSettings(self, REQUEST, title, ooo_stylesheet):
""" """
...@@ -178,7 +187,8 @@ class OOoTemplate(ZopePageTemplate): ...@@ -178,7 +187,8 @@ class OOoTemplate(ZopePageTemplate):
if request and not batch_mode: if request and not batch_mode:
request.RESPONSE.setHeader('Content-Type','%s;; charset=utf-8' % self.content_type) request.RESPONSE.setHeader('Content-Type','%s;; charset=utf-8' % self.content_type)
request.RESPONSE.setHeader('Content-Length',len(ooo)) request.RESPONSE.setHeader('Content-Length',len(ooo))
request.RESPONSE.setHeader('Content-Disposition','inline;filename=%s' % self.id) request.RESPONSE.setHeader('Content-Disposition','inline;filename=%s' %
self.title_or_id())
return ooo return ooo
......
...@@ -65,7 +65,12 @@ class OOoBuilder: ...@@ -65,7 +65,12 @@ class OOoBuilder:
security.declarePrivate('__init__') security.declarePrivate('__init__')
def __init__(self, document): def __init__(self, document):
if hasattr(document, 'data') :
self._document = StringIO(document.data) self._document = StringIO(document.data)
elif hasattr(document, 'read') :
self._document = document
else :
self._document = StringIO(document)
self._image_count = 0 self._image_count = 0
security.declarePublic('replace') security.declarePublic('replace')
...@@ -81,6 +86,45 @@ class OOoBuilder: ...@@ -81,6 +86,45 @@ class OOoBuilder:
zf.writestr(filename, stream) zf.writestr(filename, stream)
zf.close() zf.close()
security.declarePublic('extract')
def extract(self, filename):
"""
Extracts a file from the archive
"""
try:
zf = ZipFile(self._document, mode='r', compression=ZIP_DEFLATED)
except RuntimeError:
zf = ZipFile(self._document, mode='r')
return zf.read(filename)
security.declarePublic('getMimeType')
def getMimeType(self):
return self.extract('mimetype')
security.declarePublic('prepareContentXml')
def prepareContentXml(self) :
"""
extracts content.xml text and prepare it :
- add tal namespace
- indent the xml
"""
import pprint
content_xml = self.extract('content.xml')
reader = PyExpat.Reader()
document = reader.fromString(content_xml)
document_element = document.documentElement
from xml.dom.ext import PrettyPrint
output = StringIO()
PrettyPrint(document_element, output)
return output.getvalue().replace(
"office:version='1.0'",
""" xmlns:tal='http://xml.zope.org/namespaces/tal'
xmlns:i18n='http://xml.zope.org/namespaces/i18n'
xmlns:metal='http://xml.zope.org/namespaces/metal'
tal:attributes='dummy python:request.RESPONSE.setHeader("Content-Type", "text/html;; charset=utf-8")'
office:version='1.0'""")
security.declarePublic('addImage')
def addImage(self, image, format='png'): def addImage(self, image, format='png'):
""" """
Add an image to the current document and return its id Add an image to the current document and return its id
......
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