From 6d9002ecbe11ad2893e685aaee199c98364f3953 Mon Sep 17 00:00:00 2001
From: Nicolas Delaby <nicolas@nexedi.com>
Date: Thu, 28 Jan 2010 16:52:13 +0000
Subject: [PATCH] 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
---
 product/ERP5OOo/FormPrintout.py               | 27 ++++++++++++-------
 .../ERP5OOo/tests/testFormPrintoutAsODG.py    | 10 +++----
 .../ERP5OOo/tests/testFormPrintoutAsODT.py    |  4 +--
 3 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/product/ERP5OOo/FormPrintout.py b/product/ERP5OOo/FormPrintout.py
index f99ed8c617..d330de0f02 100644
--- a/product/ERP5OOo/FormPrintout.py
+++ b/product/ERP5OOo/FormPrintout.py
@@ -173,8 +173,14 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
     self.template = template
 
   security.declareProtected('View', 'index_html')
-  def index_html(self, REQUEST, icon=0, preview=0, width=None, height=None, RESPONSE=None):
-    """Render and view a printout document."""
+  def index_html(self, REQUEST, icon=0, preview=0, width=None, height=None,
+                 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)
     if obj is not None:
@@ -201,7 +207,8 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
     self.strategy = self._createStrategy(content_type)
     printout = self.strategy.render(extra_context=extra_context)
     return self._oooConvertByFormat(printout, content_type,
-                                    extra_context, REQUEST)
+                                    extra_context, REQUEST, 
+                                    format, batch_mode)
 
   security.declareProtected('View', '__call__')
   __call__ = index_html
@@ -227,7 +234,8 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
       return ODGStrategy()
     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.
 
@@ -236,12 +244,13 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
     content_type -- the content type of the printout
     extra_context -- extra_context including a format
     REQUEST -- Request object
+    format -- requested output format
+    batch_mode -- Disable headers overriding
     """
-    format = None
-    if REQUEST is not None:
+    if REQUEST is not None and not format:
       format = REQUEST.get('format', None)
-    if format is None:
-      if REQUEST is not None:
+    if not format:
+      if REQUEST is not None and not batch_mode:
         REQUEST.RESPONSE.setHeader('Content-Type','%s' % content_type)
         REQUEST.RESPONSE.setHeader('Content-disposition',
                                    'inline;filename="%s%s"' % (self.title_or_id(), guess_extension(content_type)))
@@ -254,7 +263,7 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
                  base_content_type=content_type)
     tmp_ooo.oo_data = printout
     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-disposition',
           'attachment;filename="%s.%s"' % (self.title_or_id(), format))
diff --git a/product/ERP5OOo/tests/testFormPrintoutAsODG.py b/product/ERP5OOo/tests/testFormPrintoutAsODG.py
index 37edc57c45..88c782f043 100644
--- a/product/ERP5OOo/tests/testFormPrintoutAsODG.py
+++ b/product/ERP5OOo/tests/testFormPrintoutAsODG.py
@@ -160,7 +160,7 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin):
 
     self.assertTrue(content_xml.find("Foo title!") > 0)
     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'),
                      'inline;filename="Foo_viewAsODGPrintout.odg"')
     self._validate(odf_document)
@@ -206,7 +206,7 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin):
     # 5. Normal case: just call a FormPrintout object
     request.RESPONSE.setHeader('Content-Type', 'text/html')
     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)
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
@@ -411,7 +411,7 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin):
 
     self.assertTrue(content_xml.find("Foo title!") > 0)
     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'),
                      'inline;filename="Foo_viewProxyFieldAsODGPrintout.odg"')
     self._validate(odf_document)
@@ -462,8 +462,8 @@ class TestFormPrintoutAsODG(TestFormPrintoutMixin):
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
     self.assertTrue(content_xml.find("call!") > 0)
-    # when just call FormPrintout, it does not change content-type
-    self.assertEqual(request.RESPONSE.getHeader('content-type'), 'text/html')
+    self.assertEqual(request.RESPONSE.getHeader('content-type'),
+                     'application/vnd.oasis.opendocument.graphics')
     self._validate(odf_document)
 
     # 5. Normal case: utf-8 string
diff --git a/product/ERP5OOo/tests/testFormPrintoutAsODT.py b/product/ERP5OOo/tests/testFormPrintoutAsODT.py
index 0ad74e7eec..4374811a88 100644
--- a/product/ERP5OOo/tests/testFormPrintoutAsODT.py
+++ b/product/ERP5OOo/tests/testFormPrintoutAsODT.py
@@ -202,12 +202,12 @@ class TestFormPrintoutAsODT(TestFormPrintoutMixin):
     # 5. Normal case: just call a FormPrintout object
     request.RESPONSE.setHeader('Content-Type', 'text/html')
     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)
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
     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)
 
     # 5. Normal case: utf-8 string
-- 
2.30.9