diff --git a/product/ERP5OOo/FormPrintout.py b/product/ERP5OOo/FormPrintout.py index e0f7637c8101a75a378df1a278bc0912e8f60071..3e9cac21cffd90f18c8230dcc871cd0d77c5fbeb 100644 --- a/product/ERP5OOo/FormPrintout.py +++ b/product/ERP5OOo/FormPrintout.py @@ -186,11 +186,10 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item): content_type = printout_template.content_type self.strategy = self._createStrategy(content_type) printout = self.strategy.render(extra_context=extra_context) - if REQUEST is not None: - REQUEST.RESPONSE.setHeader('Content-Type','%s; charset=utf-8' % content_type) - REQUEST.RESPONSE.setHeader('Content-disposition', - 'inline;filename="%s%s"' % (self.title_or_id(), guess_extension(content_type))) - return printout + return self._oooConvertByFormat(printout, + content_type=content_type, + extra_context=extra_context, + REQUEST=REQUEST) security.declareProtected('View', '__call__') __call__ = index_html @@ -214,6 +213,40 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item): return ODTStrategy() raise ValueError, 'Do not support the template type:%s' % content_type + def _oooConvertByFormat(self, printout, content_type=None, extra_context={}, REQUEST=None): + """ + Convert the ODF document into the given format. + + Keyword arguments: + printout -- ODF document + content_type -- the content type of the printout + extra_context -- extra_context including a format + REQUEST -- Request object + """ + options = extra_context.get('options', {}) + format = None + if REQUEST is not None: + format = options.get('format', REQUEST.get('format', None)) + if format is None: + if REQUEST is not None: + REQUEST.RESPONSE.setHeader('Content-Type','%s; charset=utf-8' % content_type) + REQUEST.RESPONSE.setHeader('Content-disposition', + 'inline;filename="%s%s"' % (self.title_or_id(), guess_extension(content_type))) + return printout + from Products.ERP5Type.Document import newTempOOoDocument + tmp_ooo = newTempOOoDocument(self, self.title_or_id()) + tmp_ooo.edit(base_data=printout, + fname=self.title_or_id(), + source_reference=self.title_or_id(), + base_content_type=content_type) + tmp_ooo.oo_data = printout + mime, data = tmp_ooo.convert(format) + if REQUEST is not None: + REQUEST.RESPONSE.setHeader('Content-type', mime) + REQUEST.RESPONSE.setHeader('Content-disposition', + 'attachment;filename="%s.%s"' % (self.title_or_id(), format)) + return data + InitializeClass(FormPrintout) class ODFStrategy(Implicit): diff --git a/product/ERP5OOo/tests/testFormPrintout.py b/product/ERP5OOo/tests/testFormPrintout.py index 5453ca96ec78a7367db62bf5f5af60302ecd42da..2628766e8713b0f07baa82e8c2bde7e3b714000a 100644 --- a/product/ERP5OOo/tests/testFormPrintout.py +++ b/product/ERP5OOo/tests/testFormPrintout.py @@ -31,6 +31,7 @@ import unittest import transaction from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase from Products.ERP5Type.tests.utils import createZODBPythonScript +from Products.MimetypesRegistry.mime_types.magic import guessMime from AccessControl.SecurityManagement import newSecurityManager from Products.ERP5OOo.OOoUtils import OOoBuilder from Products.ERP5OOo.tests.utils import Validator @@ -53,6 +54,7 @@ class TestFormPrintout(ERP5TypeTestCase): def afterSetUp(self): self.login() + self.setSystemPreference() foo_file_path = os.path.join(os.path.dirname(__file__), 'test_document', 'Foo_001.odt') @@ -126,6 +128,15 @@ class TestFormPrintout(ERP5TypeTestCase): user = uf.getUserById('tatuya').__of__(uf) newSecurityManager(None, user) + def setSystemPreference(self): + default_pref = self.portal.portal_preferences.default_site_preference + default_pref.setPreferredOoodocServerAddress('127.0.0.1') + default_pref.setPreferredOoodocServerPortNumber('8008') + #default_pref.setPreferredConversionCacheFactory('document_cache_factory') + if default_pref.getPreferenceState() != 'global': + default_pref.enable() + + def _validate(self, odf_file_data): error_list = self.validator.validate(odf_file_data) if error_list: @@ -315,7 +326,6 @@ class TestFormPrintout(ERP5TypeTestCase): self.assertTrue(content_xml.find("test title") < 0) self._validate(odf_document) - def test_02_Table_01_Normal(self, run=run_all_test): """To test listbox and ODF table mapping @@ -1159,6 +1169,28 @@ return [] self.assertTrue(content_xml.find('<draw:image xlink:href') < 0) self._validate(odf_document) + def test_08_OOoConversion(self, run=run_all_test): + """test ooo conversion""" + if not run: return + foo_printout = self.portal.foo_module.test1.Foo_viewAsPrintout + foo_form = self.portal.foo_module.test1.Foo_view + if foo_form._getOb("my_test_title", None) is None: + foo_form.manage_addField('my_test_title', 'test title', 'StringField') + test_title = foo_form.my_test_title + test_title.values['default'] = 'ZZZ test here ZZZ' + + self.portal.REQUEST.set('format', 'pdf') + printout = foo_printout(REQUEST=self.portal.REQUEST) + #test_output = open("/tmp/test_99_OOoConversion.pdf", "w") + #test_output.write(printout.data) + self.assertEqual('application/pdf', guessMime(printout.data)) + + self.portal.REQUEST.set('format', 'doc') + printout = foo_printout(REQUEST=self.portal.REQUEST) + #test_output = open("/tmp/test_99_OOoConversion.doc", "w") + #test_output.write(printout.data) + self.assertEqual('application/msword', guessMime(printout.data)) + def test_suite(): suite = unittest.TestSuite()