Commit c843c5a2 authored by Tatuya Kamada's avatar Tatuya Kamada

Support ReportBox to display repeating pages.

TODO: write more tests for ReportBox 


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@29421 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 680310d9
......@@ -30,6 +30,7 @@ from Products.CMFCore.utils import _checkPermission
from Products.ERP5Type import PropertySheet, Permissions
from Products.ERP5Form.ListBox import ListBox
from Products.ERP5Form.FormBox import FormBox
from Products.ERP5Form.ReportBox import ReportBox
from Products.ERP5Form.ImageField import ImageField
from Products.ERP5OOo.OOoUtils import OOoBuilder
from Products.CMFCore.exceptions import AccessControl_Unauthorized
......@@ -277,8 +278,12 @@ class ODFStrategy(Implicit):
extra_context=extra_context,
ooo_builder=ooo_builder)
# mapping ERP5Report report method to ODF
report_method=extra_context.get('report_method')
base_name = (report_method is not None) and report_method.__name__ or None
content_element_tree = self._replaceXmlByReportSection(element_tree=content_element_tree,
extra_context=extra_context,
report_method=report_method,
base_name=base_name,
ooo_builder=ooo_builder)
content_xml = etree.tostring(content_element_tree, encoding='utf-8')
......@@ -332,6 +337,13 @@ class ODFStrategy(Implicit):
extra_context=extra_context,
ooo_builder=ooo_builder,
iteration_index=iteration_index)
elif isinstance(field, ReportBox):
report_method = getattr(field, field.report_method, None)
element_tree = self._replaceXmlByReportSection(element_tree=element_tree,
extra_context=extra_context,
report_method=report_method,
base_name=field.id,
ooo_builder=ooo_builder)
elif isinstance(field, ImageField):
element_tree = self._replaceXmlByImageField(element_tree=element_tree,
image_field=field,
......@@ -446,16 +458,15 @@ class ODFStrategy(Implicit):
parent_node.insert(paragraph_node_index, paragraph)
paragraph_node_index = paragraph_node_index + 1
def _replaceXmlByReportSection(self, element_tree=None, extra_context=None, ooo_builder=None):
if not extra_context.has_key('report_method') or extra_context['report_method'] is None:
def _replaceXmlByReportSection(self, element_tree=None, extra_context=None,
report_method=None, base_name=None,
ooo_builder=None):
if report_method is None:
return element_tree
report_method = extra_context['report_method']
report_section_list = report_method()
portal_object = self.getPortalObject()
REQUEST = get_request()
request = extra_context.get('REQUEST', REQUEST)
target_tuple = self._pickUpTargetSection(report_method_name=report_method.__name__,
target_tuple = self._pickUpTargetSection(base_name=base_name,
report_section_list=report_section_list,
element_tree=element_tree)
if target_tuple is None:
......@@ -475,7 +486,7 @@ class ODFStrategy(Implicit):
if index is 0:
office_body.remove(original_target)
else:
self._setUniqueElementName(base_name=report_method.__name__,
self._setUniqueElementName(base_name=base_name,
iteration_index=index,
xpath=target_xpath,
element_tree=target_element_tree)
......@@ -491,19 +502,17 @@ class ODFStrategy(Implicit):
report_item.popReport(portal_object, render_prefix=None)
return element_tree
def _pickUpTargetSection(self, report_method_name='', report_section_list=[], element_tree=None):
def _pickUpTargetSection(self, base_name='', report_section_list=[], element_tree=None):
"""pick up a ODF target object to iterate ReportSection
report_method_name -- report method name
base_name -- the target name to replace in an ODF document
report_section_list -- ERP5Form ReportSection List which was created by a report method
element_tree -- XML ElementTree object
"""
frame_xpath = '//draw:frame[@draw:name="%s"]' % report_method_name
frame_xpath = '//draw:frame[@draw:name="%s"]' % base_name
frame_list = element_tree.xpath(frame_xpath, namespaces=element_tree.nsmap)
# <text:section text:style-name="Sect2" text:name="Section2">
section_xpath = '//text:section[@text:name="%s"]' % report_method_name
section_xpath = '//text:section[@text:name="%s"]' % base_name
section_list = element_tree.xpath(section_xpath, namespaces=element_tree.nsmap)
if len(frame_list) is 0 and len(section_list) is 0:
return None
......@@ -705,7 +714,6 @@ class ODFStrategy(Implicit):
render_format='list',
REQUEST=REQUEST,
render_prefix=None)
# if ODF table has header rows, does not update the header rows
# if does not have header rows, insert the listbox title line
is_top = True
......
......@@ -62,9 +62,13 @@ class TestFormPrintout(ERP5TypeTestCase):
foo3_file_path = os.path.join(os.path.dirname(__file__),
'test_document',
'Foo_003.odt')
foo4_file_path = os.path.join(os.path.dirname(__file__),
'test_document',
'Foo_004.odt')
foo_file = open(foo_file_path, 'rb')
foo2_file = open(foo2_file_path, 'rb')
foo3_file = open(foo3_file_path, 'rb')
foo4_file = open(foo4_file_path, 'rb')
custom = self.portal.portal_skins.custom
addStyleSheet = custom.manage_addProduct['OFSP'].manage_addFile
if custom._getOb('Foo_getODTStyleSheet', None) is None:
......@@ -76,6 +80,9 @@ class TestFormPrintout(ERP5TypeTestCase):
if custom._getOb('Foo3_getODTStyleSheet', None) is None:
addStyleSheet(id='Foo3_getODTStyleSheet', file=foo3_file, title='',
precondition='', content_type = 'application/vnd.oasis.opendocument.text')
if custom._getOb('Foo4_getODTStyleSheet', None) is None:
addStyleSheet(id='Foo4_getODTStyleSheet', file=foo4_file, title='',
precondition='', content_type = 'application/vnd.oasis.opendocument.text')
erp5OOo = custom.manage_addProduct['ERP5OOo']
addOOoTemplate = erp5OOo.addOOoTemplate
if custom._getOb('Foo_viewAsOdt', None) is None:
......@@ -955,6 +962,117 @@ return []
self._validate(odf_document)
def test_04_Iteration_03_ReportBox_and_Section(self, run=run_all_test):
"""
Iteration using ReportBox and ODF Section test
"""
if not run: return
# create test target
custom = self.portal.portal_skins.custom
erp5form = custom.manage_addProduct['ERP5Form']
erp5form.addERP5Form(id='Foo_Box_view', title='Foo Box')
foo_box_view = custom.Foo_Box_view
foo_box_view.manage_addField('listbox_report', 'listbox report', 'ListBox')
listbox = foo_box_view.listbox_report
createZODBPythonScript(
self.portal.portal_skins.custom,
'FooReport_getFooList',
'title,**kw',
r"""
foo_list = context.objectValues(portal_type='Foo Line')
for foo in foo_list:
foo.setTitle(title)
return foo_list
"""
)
message = listbox.ListBox_setPropertyList(
field_list_method = 'FooReport_getFooList',
field_selection_name = 'listbox_report_selection',
field_portal_types = 'Foo Line | Foo Line',
field_columns = 'id|ID\ntitle|Title\nquantity|Quantity\nstart_date|Date',)
self.failUnless('Set Successfully' in message)
# report box
foo2_view = erp5form.addERP5Form(id='Foo2_view', title='Foo2 View')
foo2_view = custom.Foo2_view
foo2_view.manage_addField('your_report_box1', 'Your Report Box', 'ReportBox')
your_report_box1 = foo2_view.your_report_box1
your_report_box1.report_method = 'FooReport_getReportSectionList'
createZODBPythonScript(
self.portal.portal_skins.custom,
'FooReport_getReportSectionList',
'',
r"""
from Products.ERP5Form.Report import ReportSection
r1 = ReportSection(path=context.getPhysicalPath(),
form_id='Foo_Box_view',
selection_name='listbox_report_selection',
selection_params={'title':'foo_04_Iteration_1'})
r2 = ReportSection(path=context.getPhysicalPath(),
form_id='Foo_Box_view',
selection_name='listbox_report_selection',
selection_params={'title':'foo_04_Iteration_2'})
report_section_list = [r1, r2]
return report_section_list
"""
)
# 01. normal case using ODF Section
test1 = self.portal.foo_module.test1
request = self.app.REQUEST
request['here'] = test1
foo_report_printout = test1.FooReport_viewAsPrintout
foo_report_printout.doSettings(REQUEST=request,
title='',
form_name='Foo2_view',
template='Foo4_getODTStyleSheet')
odf_document = foo_report_printout()
# test_output = open("/tmp/test_04_Iteratoin_03_Section_01.odf", "w")
# test_output.write(odf_document)
self.assertTrue(odf_document is not None)
builder = OOoBuilder(odf_document)
content_xml = builder.extract("content.xml")
self.assertTrue(content_xml.find("foo_04_Iteration_1") > 0)
content = etree.XML(content_xml)
section_xpath = '//text:section[@text:name="your_report_box1"]'
section_list = content.xpath(section_xpath, namespaces=content.nsmap)
self.assertEqual(len(section_list), 1)
section1_xpath = '//text:section[@text:name="your_report_box1_1"]'
section1_list = content.xpath(section1_xpath, namespaces=content.nsmap)
self.assertEqual(len(section1_list), 1)
self._validate(odf_document)
# 02. no report section and using ODF Section
custom.manage_delObjects(['FooReport_getReportSectionList'])
createZODBPythonScript(
self.portal.portal_skins.custom,
'FooReport_getReportSectionList',
'',
r"""
return []
"""
)
odf_document = foo_report_printout()
#test_output = open("/tmp/test_04_Iteratoin_02_Section_02.odf", "w")
#test_output.write(odf_document)
self.assertTrue(odf_document is not None)
builder = OOoBuilder(odf_document)
content_xml = builder.extract("content.xml")
self.assertFalse(content_xml.find("foo_04_Iteration") > 0)
content = etree.XML(content_xml)
section_xpath = '//text:section[@text:name="your_report_box1"]'
section_list = content.xpath(section_xpath, namespaces=content.nsmap)
# the section was removed
self.assertEqual(len(section_list), 0)
self._validate(odf_document)
def _test_05_Styles(self, run=run_all_test):
"""
styles.xml not tested yet
......
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