Commit 6d9002ec authored by Nicolas Delaby's avatar Nicolas Delaby

Add batch_mode parameter for FormPrintout

  * if batch_mode is True disable the Reponse Header overriding
  * Change test
This change reflect OOoTemplate API
approved by Jerome


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@32071 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 4154e390
...@@ -173,8 +173,14 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item): ...@@ -173,8 +173,14 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
self.template = template self.template = template
security.declareProtected('View', 'index_html') security.declareProtected('View', 'index_html')
def index_html(self, REQUEST, icon=0, preview=0, width=None, height=None, RESPONSE=None): def index_html(self, REQUEST, icon=0, preview=0, width=None, height=None,
"""Render and view a printout document.""" RESPONSE=None, format=None, batch_mode=False):
"""Render and view a printout document.
format: conversion format requested by User.
take precedence of format in REQUEST
batch_mode: if True then avoid overriding response headers.
"""
obj = getattr(self, 'aq_parent', None) obj = getattr(self, 'aq_parent', None)
if obj is not None: if obj is not None:
...@@ -201,7 +207,8 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item): ...@@ -201,7 +207,8 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
self.strategy = self._createStrategy(content_type) self.strategy = self._createStrategy(content_type)
printout = self.strategy.render(extra_context=extra_context) printout = self.strategy.render(extra_context=extra_context)
return self._oooConvertByFormat(printout, content_type, return self._oooConvertByFormat(printout, content_type,
extra_context, REQUEST) extra_context, REQUEST,
format, batch_mode)
security.declareProtected('View', '__call__') security.declareProtected('View', '__call__')
__call__ = index_html __call__ = index_html
...@@ -227,7 +234,8 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item): ...@@ -227,7 +234,8 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
return ODGStrategy() return ODGStrategy()
raise ValueError, 'Template type: %s is not supported' % content_type raise ValueError, 'Template type: %s is not supported' % content_type
def _oooConvertByFormat(self, printout, content_type, extra_context, REQUEST): def _oooConvertByFormat(self, printout, content_type, extra_context,
REQUEST, format, batch_mode):
""" """
Convert the ODF document into the given format. Convert the ODF document into the given format.
...@@ -236,12 +244,13 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item): ...@@ -236,12 +244,13 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
content_type -- the content type of the printout content_type -- the content type of the printout
extra_context -- extra_context including a format extra_context -- extra_context including a format
REQUEST -- Request object REQUEST -- Request object
format -- requested output format
batch_mode -- Disable headers overriding
""" """
format = None if REQUEST is not None and not format:
if REQUEST is not None:
format = REQUEST.get('format', None) format = REQUEST.get('format', None)
if format is None: if not format:
if REQUEST is not None: if REQUEST is not None and not batch_mode:
REQUEST.RESPONSE.setHeader('Content-Type','%s' % content_type) REQUEST.RESPONSE.setHeader('Content-Type','%s' % content_type)
REQUEST.RESPONSE.setHeader('Content-disposition', REQUEST.RESPONSE.setHeader('Content-disposition',
'inline;filename="%s%s"' % (self.title_or_id(), guess_extension(content_type))) 'inline;filename="%s%s"' % (self.title_or_id(), guess_extension(content_type)))
...@@ -254,7 +263,7 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item): ...@@ -254,7 +263,7 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
base_content_type=content_type) base_content_type=content_type)
tmp_ooo.oo_data = printout tmp_ooo.oo_data = printout
mime, data = tmp_ooo.convert(format) mime, data = tmp_ooo.convert(format)
if REQUEST is not None: if REQUEST is not None and not batch_mode:
REQUEST.RESPONSE.setHeader('Content-type', mime) REQUEST.RESPONSE.setHeader('Content-type', mime)
REQUEST.RESPONSE.setHeader('Content-disposition', REQUEST.RESPONSE.setHeader('Content-disposition',
'attachment;filename="%s.%s"' % (self.title_or_id(), format)) 'attachment;filename="%s.%s"' % (self.title_or_id(), format))
......
...@@ -160,7 +160,7 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin): ...@@ -160,7 +160,7 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin):
self.assertTrue(content_xml.find("Foo title!") > 0) self.assertTrue(content_xml.find("Foo title!") > 0)
self.assertEqual(request.RESPONSE.getHeader('content-type'), self.assertEqual(request.RESPONSE.getHeader('content-type'),
'application/vnd.oasis.opendocument.graphics; charset=utf-8') 'application/vnd.oasis.opendocument.graphics')
self.assertEqual(request.RESPONSE.getHeader('content-disposition'), self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
'inline;filename="Foo_viewAsODGPrintout.odg"') 'inline;filename="Foo_viewAsODGPrintout.odg"')
self._validate(odf_document) self._validate(odf_document)
...@@ -206,7 +206,7 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin): ...@@ -206,7 +206,7 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin):
# 5. Normal case: just call a FormPrintout object # 5. Normal case: just call a FormPrintout object
request.RESPONSE.setHeader('Content-Type', 'text/html') request.RESPONSE.setHeader('Content-Type', 'text/html')
test1.setTitle("call!") test1.setTitle("call!")
odf_document = foo_printout(request) # call odf_document = foo_printout(request, batch_mode=True) # call
self.assertTrue(odf_document is not None) self.assertTrue(odf_document is not None)
builder = OOoBuilder(odf_document) builder = OOoBuilder(odf_document)
content_xml = builder.extract("content.xml") content_xml = builder.extract("content.xml")
...@@ -411,7 +411,7 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin): ...@@ -411,7 +411,7 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin):
self.assertTrue(content_xml.find("Foo title!") > 0) self.assertTrue(content_xml.find("Foo title!") > 0)
self.assertEqual(request.RESPONSE.getHeader('content-type'), self.assertEqual(request.RESPONSE.getHeader('content-type'),
'application/vnd.oasis.opendocument.graphics; charset=utf-8') 'application/vnd.oasis.opendocument.graphics')
self.assertEqual(request.RESPONSE.getHeader('content-disposition'), self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
'inline;filename="Foo_viewProxyFieldAsODGPrintout.odg"') 'inline;filename="Foo_viewProxyFieldAsODGPrintout.odg"')
self._validate(odf_document) self._validate(odf_document)
...@@ -462,8 +462,8 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin): ...@@ -462,8 +462,8 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin):
builder = OOoBuilder(odf_document) builder = OOoBuilder(odf_document)
content_xml = builder.extract("content.xml") content_xml = builder.extract("content.xml")
self.assertTrue(content_xml.find("call!") > 0) self.assertTrue(content_xml.find("call!") > 0)
# when just call FormPrintout, it does not change content-type self.assertEqual(request.RESPONSE.getHeader('content-type'),
self.assertEqual(request.RESPONSE.getHeader('content-type'), 'text/html') 'application/vnd.oasis.opendocument.graphics')
self._validate(odf_document) self._validate(odf_document)
# 5. Normal case: utf-8 string # 5. Normal case: utf-8 string
......
...@@ -202,12 +202,12 @@ class TestFormPrintoutAsODT(TestFormPrintoutMixin): ...@@ -202,12 +202,12 @@ class TestFormPrintoutAsODT(TestFormPrintoutMixin):
# 5. Normal case: just call a FormPrintout object # 5. Normal case: just call a FormPrintout object
request.RESPONSE.setHeader('Content-Type', 'text/html') request.RESPONSE.setHeader('Content-Type', 'text/html')
test1.setTitle("call!") test1.setTitle("call!")
odf_document = foo_printout(request) # call odf_document = foo_printout(request, batch_mode=True) # call
self.assertTrue(odf_document is not None) self.assertTrue(odf_document is not None)
builder = OOoBuilder(odf_document) builder = OOoBuilder(odf_document)
content_xml = builder.extract("content.xml") content_xml = builder.extract("content.xml")
self.assertTrue(content_xml.find("call!") > 0) self.assertTrue(content_xml.find("call!") > 0)
self.assertEqual(request.RESPONSE.getHeader('content-type'), 'application/vnd.oasis.opendocument.text') self.assertEqual(request.RESPONSE.getHeader('content-type'), 'text/html')
self._validate(odf_document) self._validate(odf_document)
# 5. Normal case: utf-8 string # 5. Normal case: utf-8 string
......
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