Commit 57048f13 authored by Rie Kozai's avatar Rie Kozai

implement batch_mode correctly and test it.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@15372 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 091ae64d
...@@ -373,7 +373,7 @@ class OOoDocument(File, ConversionCacheMixin): ...@@ -373,7 +373,7 @@ class OOoDocument(File, ConversionCacheMixin):
if display is None or original_format not in STANDARD_IMAGE_FORMAT_LIST: if display is None or original_format not in STANDARD_IMAGE_FORMAT_LIST:
self.setConversion(data, mime, format=format) self.setConversion(data, mime, format=format)
else: else:
temp_image = self.portal_contributions.newContent( self.portal_contributions.newContent(
portal_type='Image', portal_type='Image',
temp_object=1) temp_object=1)
temp_image._setData(data) temp_image._setData(data)
......
...@@ -51,6 +51,8 @@ try: ...@@ -51,6 +51,8 @@ try:
except ImportError: except ImportError:
SUPPORTS_WEBDAV_LOCKS = 0 SUPPORTS_WEBDAV_LOCKS = 0
from Products.ERP5.Document.Document import ConversionError
# Constructors # Constructors
manage_addOOoTemplate = DTMLFile("dtml/OOoTemplate_add", globals()) manage_addOOoTemplate = DTMLFile("dtml/OOoTemplate_add", globals())
...@@ -63,6 +65,7 @@ def addOOoTemplate(self, id, title="", REQUEST=None): ...@@ -63,6 +65,7 @@ def addOOoTemplate(self, id, title="", REQUEST=None):
""" """
# add actual object # add actual object
id = self._setObject(id, OOoTemplate(id, title)) id = self._setObject(id, OOoTemplate(id, title))
if REQUEST is not None:
file = REQUEST.form.get('file') file = REQUEST.form.get('file')
if file.filename: if file.filename:
# Get the template in the associated context and upload the file # Get the template in the associated context and upload the file
...@@ -394,9 +397,8 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -394,9 +397,8 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
def pt_render(self, source=0, extra_context={}): def pt_render(self, source=0, extra_context={}):
# Get request # Get request
request = extra_context.get('REQUEST', None) request = extra_context.get('REQUEST', None)
if not request: if request is None:
request = get_request() request = self.REQUEST
# Get parent object (the one to render this template on) # Get parent object (the one to render this template on)
here = getattr(self, 'aq_parent', None) here = getattr(self, 'aq_parent', None)
if here is None: if here is None:
...@@ -413,6 +415,7 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -413,6 +415,7 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
ooo_builder = OOoBuilder(ooo_document) ooo_builder = OOoBuilder(ooo_document)
# Pass builder instance as extra_context # Pass builder instance as extra_context
extra_context['ooo_builder'] = ooo_builder extra_context['ooo_builder'] = ooo_builder
# And render page template # And render page template
doc_xml = ZopePageTemplate.pt_render(self, source=source, extra_context=extra_context) doc_xml = ZopePageTemplate.pt_render(self, source=source, extra_context=extra_context)
...@@ -423,6 +426,7 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -423,6 +426,7 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
default_styles_text = ooo_builder.extract('styles.xml') default_styles_text = ooo_builder.extract('styles.xml')
except AttributeError: except AttributeError:
default_styles_text = None default_styles_text = None
# Add the associated files # Add the associated files
for dir_name, document_dict in attachments_dict.iteritems(): for dir_name, document_dict in attachments_dict.iteritems():
# Special case : the document is an OOo one # Special case : the document is an OOo one
...@@ -441,9 +445,6 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -441,9 +445,6 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
ooo_builder.addFileEntry(full_path=dir_name, ooo_builder.addFileEntry(full_path=dir_name,
media_type=document_dict['doc_type'], content = document_dict['document'] ) media_type=document_dict['doc_type'], content = document_dict['document'] )
# Get request and batch_mode
batch_mode = extra_context.get('batch_mode', 0)
# Debug mode # Debug mode
if request.get('debug',0): if request.get('debug',0):
return doc_xml return doc_xml
...@@ -457,6 +458,12 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -457,6 +458,12 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
except AttributeError: except AttributeError:
self.OLE_documents_zipstring = None self.OLE_documents_zipstring = None
# Convert if necessary
opts = extra_context.get("options", None)
# Get request and batch_mode
batch_mode = opts.get('batch_mode', None)
# If the file has embedded OLE documents, restore it # If the file has embedded OLE documents, restore it
if self.OLE_documents_zipstring: if self.OLE_documents_zipstring:
additional_builder = OOoBuilder( self.OLE_documents_zipstring ) additional_builder = OOoBuilder( self.OLE_documents_zipstring )
...@@ -467,21 +474,19 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -467,21 +474,19 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
ooo_builder.updateManifest() ooo_builder.updateManifest()
# Produce final result # Produce final result
ooo = ooo_builder.render(self.title or self.id) ooo = ooo_builder.render(name=self.title or self.id)
# Convert if necessary
opts = extra_context.get("options", None)
if opts is not None: if opts is not None:
format = opts.get('format', request.get('format', None)) format = opts.get('format', request.get('format', None))
if format: if format:
return self._asFormat(ooo, format, request) return self._asFormat(ooo, format, request, batch_mode)
# Do not send a RESPONSE if in batch_mode if format is None 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-disposition', 'inline;filename=%s' % self.title_or_id())
request.RESPONSE.setHeader('Content-Disposition','inline;filename=%s' % else:
self.title_or_id()) request.RESPONSE.setHeader('Content-Type','%s;; charset=utf-8' % 'text/html')
request.RESPONSE.setHeader('Content-disposition', 'inline;filename=%s' % self.title_or_id())
return ooo return ooo
...@@ -503,8 +508,8 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -503,8 +508,8 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
""" """
return self._asFormat(ooo, 'pdf', REQUEST) return self._asFormat(ooo, 'pdf', REQUEST)
def _asFormat(self, ooo, format, REQUEST=None): def _asFormat(self, ooo, format, REQUEST=None, batch_mode=0):
# now create a temp OOoDocument to convert data to pdf # Now create a temp OOoDocument to convert data to pdf
from Products.ERP5Type.Document import newTempOOoDocument from Products.ERP5Type.Document import newTempOOoDocument
tmp_ooo = newTempOOoDocument(self, self.title_or_id()) tmp_ooo = newTempOOoDocument(self, self.title_or_id())
tmp_ooo.edit(base_data=ooo, tmp_ooo.edit(base_data=ooo,
...@@ -512,29 +517,29 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0"> ...@@ -512,29 +517,29 @@ xmlns:config="http://openoffice.org/2001/config" office:version="1.0">
source_reference=self.title_or_id(), source_reference=self.title_or_id(),
base_content_type=self.content_type,) base_content_type=self.content_type,)
tmp_ooo.oo_data = ooo tmp_ooo.oo_data = ooo
if format == 'pdf' and not batch_mode:
if format == 'pdf': # Slightly different implementation
# slightly different implementation
# now convert it to pdf # now convert it to pdf
tgts = [x[1] for x in tmp_ooo.getTargetFormatItemList() tgts = [x[1] for x in tmp_ooo.getTargetFormatItemList()
if x[1].endswith('pdf')] if x[1].endswith('pdf')]
if len(tgts) > 1: 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' raise ValueError, 'multiple pdf formats found - this shouldnt happen'
if len(tgts) == 0: 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' raise ValueError, 'no pdf format found'
fmt = tgts[0] fmt = tgts[0]
mime, data = tmp_ooo.convert(fmt) mime, data = tmp_ooo.convert(fmt)
if REQUEST is not None: if REQUEST is not None:
REQUEST.RESPONSE.setHeader('Content-type', 'application/pdf') REQUEST.RESPONSE.setHeader('Content-type', 'application/pdf')
REQUEST.RESPONSE.setHeader('Content-disposition', REQUEST.RESPONSE.setHeader('Content-disposition', 'attachment;filename=%s.pdf' % self.title_or_id())
'attachment;; filename="%s.pdf"' % self.title_or_id())
return data return data
mime , data = tmp_ooo.convert(format)
mime, data = tmp_ooo.convert(format)
if REQUEST is not None: if REQUEST is not None:
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))
# FIXME the above lines should return zip format when html was requested # FIXME the above lines should return zip format when html was requested
return data return data
......
...@@ -176,7 +176,6 @@ class OOoBuilder: ...@@ -176,7 +176,6 @@ class OOoBuilder:
""" Add a path to the manifest """ """ Add a path to the manifest """
MANIFEST_FILENAME = 'META-INF/manifest.xml' MANIFEST_FILENAME = 'META-INF/manifest.xml'
meta_infos = self.extract(MANIFEST_FILENAME) meta_infos = self.extract(MANIFEST_FILENAME)
# prevent some duplicates # prevent some duplicates
for meta_line in meta_infos.split('\n'): for meta_line in meta_infos.split('\n'):
for new_meta_line in self._manifest_additions_list: for new_meta_line in self._manifest_additions_list:
...@@ -207,9 +206,9 @@ class OOoBuilder: ...@@ -207,9 +206,9 @@ class OOoBuilder:
returns the OOo document returns the OOo document
""" """
request = get_request() request = get_request()
request.response.setHeader('Content-type', OOo_mimeType_dict.get(extension, 'application/vnd.sun.xml.writer'))
if name: if name:
request.response.setHeader('Content-Disposition', 'attachment; filename=%s.%s' % (name, extension)) request.response.setHeader('Content-Disposition', 'inline; filename=%s.%s' % (name, extension))
self._document.seek(0) self._document.seek(0)
return self._document.read() return self._document.read()
......
##############################################################################
#
# Copyright (c) 2004, 2005, 2006 Nexedi SARL and Contributors.
# All Rights Reserved.
##
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
##############################################################################
import os
import sys
import Zope
from AccessControl.SecurityManagement import newSecurityManager
from Testing import ZopeTestCase
from Products.PageTemplates.GlobalTranslationService import setGlobalTranslationService
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Testing.makerequest import makerequest
from Products.ERP5.Document.Document import ConversionError
from App.config import getConfiguration
class TestOoodResponse(ERP5TypeTestCase):
manager_username = 'rie'
manager_password = 'rie'
quiet = 1
run_all_test = 1
def getTitle(self):
return "TestOOoBatchMode"
def login(self, quiet=0, run=run_all_test):
uf = self.getPortal().acl_users
uf._doAddUser(self.manager_username, self.manager_password, ['Manager'], [])
user = uf.getUserById(self.manager_username).__of__(uf)
newSecurityManager(None, user)
def getBusinessTemplateList(self):
return ()
def afterSetUp(self):
self.login()
portal_skins = self.getSkinsTool()
portal_skins.custom.REQUEST.RESPONSE.setHeader('content-type', 'text/html')
try:
import_file_path = os.path.join(getConfiguration().instancehome,
'Products',
'ERP5OOo',
'tests',
'PersonSpreadsheetStylesheet' )
except IOError:
import_file_path = os.path.join(getConfiguration().softwarehome,
'Products',
'ERP5OOo',
'tests',
'PersonSpreadsheetStylesheet')
import_file = open(import_file_path)
addStyleSheet = portal_skins.custom.manage_addProduct['OFSP'].manage_addFile
addStyleSheet(id='Base_getODTStyleSheet', file=import_file, title='', precondition='', content_type='application/vnd.oasis.opendocument.text')
addOOoTemplate = portal_skins.custom.manage_addProduct['ERP5OOo'].addOOoTemplate
addOOoTemplate(id='ERP5Site_viewNothingAsOdt', title='')
portal_skins.changeSkin(skinname=None)
ERP5Site_viewNothingAsOdt = self.getPortal().ERP5Site_viewNothingAsOdt
text = "<office:document-content xmlns:draw='urn:oasis:names:tc:opendocument:xmlns:drawing:1.0' xmlns:office='urn:oasis:names:tc:opendocument:xmlns:office:1.0' xmlns:text='urn:oasis:names:tc:opendocument:xmlns:text:1.0' xmlns:ooo='http://openoffice.org/2004/office' xmlns:number='urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0' xmlns:dc='http://purl.org/dc/elements/1.1/' xmlns:meta='urn:oasis:names:tc:opendocument:xmlns:meta:1.0' xmlns:table='urn:oasis:names:tc:opendocument:xmlns:table:1.0' xmlns:dr3d='urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0' xmlns:fo='urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0' xmlns:style='urn:oasis:names:tc:opendocument:xmlns:style:1.0' xmlns:xforms='http://www.w3.org/2002/xforms' xmlns:form='urn:oasis:names:tc:opendocument:xmlns:form:1.0' xmlns:script='urn:oasis:names:tc:opendocument:xmlns:script:1.0' xmlns:ooow='http://openoffice.org/2004/writer' xmlns:svg='urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0' xmlns:chart='urn:oasis:names:tc:opendocument:xmlns:chart:1.0' xmlns:dom='http://www.w3.org/2001/xml-events' xmlns:xlink='http://www.w3.org/1999/xlink' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:oooc='http://openoffice.org/2004/calc' xmlns:math='http://www.w3.org/1998/Math/MathML' xmlns:tal='http://xml.zope.org/namespaces/tal'></office:document-content>"
content_type = 'application/vnd.oasis.opendocument.text'
ERP5Site_viewNothingAsOdt.pt_edit(text, content_type)
def test_01_noExcNoFormatNoBatchMode(self):
portal_skins = self.getSkinsTool()
portal_skins.custom.REQUEST.RESPONSE.setHeader('content-type', 'text/html')
ERP5Site_viewNothingAsOdt = self.getPortal().ERP5Site_viewNothingAsOdt
ERP5Site_viewNothingAsOdt(batch_mode=0)
self.assertEqual('application/vnd.oasis.opendocument.text', portal_skins.custom.REQUEST.RESPONSE.getHeader('content-type').split(';')[0])
self.assertEqual('inline;filename=ERP5Site_viewNothingAsOdt', portal_skins.custom.REQUEST.RESPONSE.getHeader('content-disposition'))
def test_02_noExcNoFormatBatchMode(self):
portal_skins = self.getSkinsTool()
portal_skins.custom.REQUEST.RESPONSE.setHeader('content-type', 'text/html')
ERP5Site_viewNothingAsOdt = self.getPortal().ERP5Site_viewNothingAsOdt
ERP5Site_viewNothingAsOdt(batch_mode=1)
self.assertEqual('text/html', portal_skins.custom.REQUEST.RESPONSE.getHeader('content-type').split(';')[0])
def test_03_excPdfFormatNoBatchMode(self):
portal_skins = self.getSkinsTool()
portal_skins.custom.REQUEST.RESPONSE.setHeader('content-type', 'text/html')
ERP5Site_viewNothingAsOdt = self.getPortal().ERP5Site_viewNothingAsOdt
self.assertRaises(ConversionError, ERP5Site_viewNothingAsOdt, batch_mode=0, format='pdf')
self.assertEqual('text/html', portal_skins.custom.REQUEST.RESPONSE.getHeader('content-type').split(';')[0])
def test_04_excPdfFormatBatchMode(self):
portal_skins = self.getSkinsTool()
portal_skins.custom.REQUEST.RESPONSE.setHeader('content-type', 'text/html')
ERP5Site_viewNothingAsOdt = self.getPortal().ERP5Site_viewNothingAsOdt
self.assertRaises(ConversionError, ERP5Site_viewNothingAsOdt, batch_mode=1, format='pdf')
self.assertEqual('text/html', portal_skins.custom.REQUEST.RESPONSE.getHeader('content-type').split(';')[0])
if __name__ == '__main__':
framework()
else:
import unittest
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestOoodResponse))
return suite
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