Commit 42b05aa8 authored by Jérome Perrin's avatar Jérome Perrin

Support the case where CMFReportTool is not installed.

In this case PDFTemplates will not work, but it does not prevent zope startup.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@25331 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 325114fa
...@@ -203,143 +203,149 @@ registerFileExtension('pdft', FSPDFTemplate) ...@@ -203,143 +203,149 @@ registerFileExtension('pdft', FSPDFTemplate)
registerMetaType('ERP5 PDF Template', FSPDFTemplate) registerMetaType('ERP5 PDF Template', FSPDFTemplate)
# Dynamic Patch # Dynamic Patch
from Products.CMFReportTool.ReportTool import ReportTool
try: try:
from Products.CMFReportTool.ReportTool import ZODBResourceHandler from Products.CMFReportTool.ReportTool import ReportTool
HAS_ZODB_RESOURCE_HANDLER=1
except ImportError: except ImportError:
from Products.CMFReportTool.ReportTool import ZODBHandler, ResourceHandler ReportTool = None
HAS_ZODB_RESOURCE_HANDLER=0
from Products.CMFReportTool.RenderPDF.Parser import TemplateParser,DocumentParser if ReportTool:
try:
from Products.CMFReportTool.ReportTool import ZODBResourceHandler
HAS_ZODB_RESOURCE_HANDLER=1
except ImportError:
from Products.CMFReportTool.ReportTool import ZODBHandler, ResourceHandler
HAS_ZODB_RESOURCE_HANDLER=0
from Products.CMFReportTool.RenderPDF.Parser import TemplateParser,DocumentParser
try:
# Zope 2.10 and later.
from Products.PageTemplates.Expressions import boboAwareZopeTraverse
except ImportError:
# Zope 2.9 and earlier.
boboAwareZopeTraverse = None
from Products.PageTemplates.Expressions import restrictedTraverse
from StringIO import StringIO
import xml.dom.minidom
import urllib,os.path
if HAS_ZODB_RESOURCE_HANDLER:
class ERP5ResourceHandler(ZODBResourceHandler):
''' Wrapper for ZODB Resources and files'''
def handleZODB(self,path):
path = path.split('/')
if boboAwareZopeTraverse is None:
obj = restrictedTraverse(self.context,path,getSecurityManager())
else:
# XXX only the request should be required, but this looks ad-hoc..
econtext = dict(request=self.context.REQUEST)
obj = boboAwareZopeTraverse(self.context, path, econtext)
# check type and e.g. call object if script ...
if callable(obj):
try:
obj = obj()
except (ConflictError, RuntimeError):
raise
except:
pass
## for OFS.Image-like objects
if hasattr(obj,'_original'):
obj = obj._original._data()
elif hasattr(obj,'_data'):
obj = obj._data
elif hasattr(obj,'data'):
obj = obj.data
return StringIO(str(obj))
else:
class ERP5ResourceHandler(ResourceHandler):
''' Wrapper for ZODB Resources and files'''
def __init__(self, context=None, resource_path=None):
zodbhandler = ERP5ZODBHandler(context)
self.opener = urllib2.build_opener(zodbhandler)
class ERP5ZODBHandler(ZODBHandler):
def zodb_open(self, req):
path = req.get_selector()
path = path.split('/')
if boboAwareZopeTraverse is None:
obj = restrictedTraverse(self.context,path,getSecurityManager())
else:
# XXX only the request should be required, but this looks ad-hoc..
econtext = dict(request=self.context.REQUEST)
obj = boboAwareZopeTraverse(self.context, path, econtext)
# check type and e.g. call object if script ...
if callable(obj):
try:
obj = obj()
except (ConflictError, RuntimeError):
raise
except:
pass
## for OFS.Image-like objects
if hasattr(obj,'_original'):
obj = obj._original._data()
elif hasattr(obj,'_data'):
obj = obj._data
elif hasattr(obj,'data'):
obj = obj.data
return StringIO(str(obj))
def ReportTool_renderPDF(self, templatename, document_xml, *args, **kwargs):
"""
Render document using template
"""
try: context = kwargs.get('context',None)
# Zope 2.10 and later. if context is None:
from Products.PageTemplates.Expressions import boboAwareZopeTraverse context = self
except ImportError:
# Zope 2.9 and earlier. encoding = kwargs.get('encoding') or 'UTF-8'
boboAwareZopeTraverse = None #LOG('ReportTool_renderPDF', 0, 'encoding = %r' % encoding)
from Products.PageTemplates.Expressions import restrictedTraverse rhandler = ERP5ResourceHandler(context, getattr(self, 'resourcePath', None))
from StringIO import StringIO # if zope gives us the xml in unicode
import xml.dom.minidom # we need to encode it before it can be parsed
import urllib,os.path template_xml = getattr(context, templatename)(*args, **kwargs)
if type(template_xml) is type(u''):
template_xml = self._encode(template_xml, encoding)
if HAS_ZODB_RESOURCE_HANDLER: if type(document_xml) is type(u''):
class ERP5ResourceHandler(ZODBResourceHandler): document_xml = self._encode(document_xml, encoding)
''' Wrapper for ZODB Resources and files''' #LOG('ReportTool_renderPDF', 0, 'template_xml = %r, document_xml = %r' % (template_xml, document_xml))
def handleZODB(self,path): # XXXXX Because reportlab does not support UTF-8, use Latin-1. What a mess.
template_xml = unicode(template_xml,encoding).encode('iso-8859-1')
path = path.split('/') document_xml = unicode(document_xml,encoding).encode('iso-8859-1','replace')
if boboAwareZopeTraverse is None: encoding = 'iso-8859-1'
obj = restrictedTraverse(self.context,path,getSecurityManager())
else: # create the PDFTemplate from xml
# XXX only the request should be required, but this looks ad-hoc.. template_dom = xml.dom.minidom.parseString(template_xml)
econtext = dict(request=self.context.REQUEST) template_dom.encoding = encoding
obj = boboAwareZopeTraverse(self.context, path, econtext) template = TemplateParser(template_dom,encoding,resourceHandler=rhandler)()
# create the PDFDocment from xml
# check type and e.g. call object if script ... document_dom = xml.dom.minidom.parseString(document_xml)
if callable(obj): document_dom.encoding = encoding
try: document = DocumentParser(document_dom,encoding,resourceHandler=rhandler)
obj = obj()
except (ConflictError, RuntimeError): # create the PDF itself using the document and the template
raise buf = StringIO()
except: document(template,buf)
pass buf.seek(0)
return buf.read()
## for OFS.Image-like objects
if hasattr(obj,'_original'):
obj = obj._original._data() ReportTool.renderPDF = ReportTool_renderPDF
elif hasattr(obj,'_data'):
obj = obj._data
elif hasattr(obj,'data'):
obj = obj.data
return StringIO(str(obj))
else:
class ERP5ResourceHandler(ResourceHandler):
''' Wrapper for ZODB Resources and files'''
def __init__(self, context=None, resource_path=None):
zodbhandler = ERP5ZODBHandler(context)
self.opener = urllib2.build_opener(zodbhandler)
class ERP5ZODBHandler(ZODBHandler):
def zodb_open(self, req):
path = req.get_selector()
path = path.split('/')
if boboAwareZopeTraverse is None:
obj = restrictedTraverse(self.context,path,getSecurityManager())
else:
# XXX only the request should be required, but this looks ad-hoc..
econtext = dict(request=self.context.REQUEST)
obj = boboAwareZopeTraverse(self.context, path, econtext)
# check type and e.g. call object if script ...
if callable(obj):
try:
obj = obj()
except (ConflictError, RuntimeError):
raise
except:
pass
## for OFS.Image-like objects
if hasattr(obj,'_original'):
obj = obj._original._data()
elif hasattr(obj,'_data'):
obj = obj._data
elif hasattr(obj,'data'):
obj = obj.data
return StringIO(str(obj))
def ReportTool_renderPDF(self, templatename, document_xml, *args, **kwargs):
"""
Render document using template
"""
context = kwargs.get('context',None)
if context is None:
context = self
encoding = kwargs.get('encoding') or 'UTF-8'
#LOG('ReportTool_renderPDF', 0, 'encoding = %r' % encoding)
rhandler = ERP5ResourceHandler(context, getattr(self, 'resourcePath', None))
# if zope gives us the xml in unicode
# we need to encode it before it can be parsed
template_xml = getattr(context, templatename)(*args, **kwargs)
if type(template_xml) is type(u''):
template_xml = self._encode(template_xml, encoding)
if type(document_xml) is type(u''):
document_xml = self._encode(document_xml, encoding)
#LOG('ReportTool_renderPDF', 0, 'template_xml = %r, document_xml = %r' % (template_xml, document_xml))
# XXXXX Because reportlab does not support UTF-8, use Latin-1. What a mess.
template_xml = unicode(template_xml,encoding).encode('iso-8859-1')
document_xml = unicode(document_xml,encoding).encode('iso-8859-1','replace')
encoding = 'iso-8859-1'
# create the PDFTemplate from xml
template_dom = xml.dom.minidom.parseString(template_xml)
template_dom.encoding = encoding
template = TemplateParser(template_dom,encoding,resourceHandler=rhandler)()
# create the PDFDocment from xml
document_dom = xml.dom.minidom.parseString(document_xml)
document_dom.encoding = encoding
document = DocumentParser(document_dom,encoding,resourceHandler=rhandler)
# create the PDF itself using the document and the template
buf = StringIO()
document(template,buf)
buf.seek(0)
return buf.read()
ReportTool.renderPDF = ReportTool_renderPDF
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