Commit 2bed525f authored by Tatuya Kamada's avatar Tatuya Kamada

Append a feature to enable mapping ERP5Report ReportSection and ODF sections


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@26670 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 88d6e018
......@@ -104,7 +104,8 @@ class FormPrintout(Implicit, Persistent, RoleManager, Item):
Fields -> Paragraphs: supported
ListBox -> Table: supported
Report Section -> Frames: experimentally supported
Report Section
-> Frames or Sections: supported
FormBox -> Frame: experimentally supported
ImageField -> Photo: supported
styles.xml: supported
......@@ -453,45 +454,76 @@ class ODFStrategy(Implicit):
REQUEST = get_request()
request = extra_context.get('REQUEST', REQUEST)
report_section_frame_xpath = '//draw:frame[@draw:name="%s"]' % report_method.__name__
frame_list = element_tree.xpath(report_section_frame_xpath, namespaces=element_tree.nsmap)
if len(frame_list) is 0:
target_tuple = self._pickUpTargetSection(report_method_name=report_method.__name__,
report_section_list=report_section_list,
element_tree=element_tree)
if target_tuple is None:
return element_tree
frame = frame_list[0]
frame_paragraph = frame.getparent()
office_body = frame_paragraph.getparent()
# remove if no report section
if len(report_section_list) is 0:
office_body.remove(frame_paragraph)
return element_tree
frame_paragraph_index = office_body.index(frame_paragraph)
temporary_element_tree = deepcopy(frame_paragraph)
target_xpath, original_target = target_tuple
office_body = original_target.getparent()
target_index = office_body.index(original_target)
temporary_element_tree = deepcopy(original_target)
for (index, report_item) in enumerate(report_section_list):
report_item.pushReport(portal_object, render_prefix=None)
here = report_item.getObject(portal_object)
form_id = report_item.getFormId()
form = getattr(here, form_id)
frame_paragraph_element_tree = deepcopy(temporary_element_tree)
target_element_tree = deepcopy(temporary_element_tree)
# remove original target in the ODF template
if index is 0:
office_body.remove(frame_paragraph)
office_body.remove(original_target)
else:
self._setUniqueElementName(base_name=report_method.__name__,
iteration_index=index,
xpath=report_section_frame_xpath,
element_tree=frame_paragraph_element_tree)
frame_paragraph_element_tree = self._replaceXmlByForm(element_tree=frame_paragraph_element_tree,
form=form,
here=here,
extra_context=extra_context,
ooo_builder=ooo_builder,
iteration_index=index)
office_body.insert(frame_paragraph_index, frame_paragraph_element_tree)
frame_paragraph_index += 1
xpath=target_xpath,
element_tree=target_element_tree)
target_element_tree = self._replaceXmlByForm(element_tree=target_element_tree,
form=form,
here=here,
extra_context=extra_context,
ooo_builder=ooo_builder,
iteration_index=index)
office_body.insert(target_index, target_element_tree)
target_index += 1
report_item.popReport(portal_object, render_prefix=None)
return element_tree
def _pickUpTargetSection(self, report_method_name='', report_section_list=[], element_tree=None):
"""pick up a ODF target object to iterate ReportSection
report_method_name -- report method name
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_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_list = element_tree.xpath(section_xpath, namespaces=element_tree.nsmap)
if len(frame_list) is 0 and len(section_list) is 0:
return None
office_body = None
original_target = None
target_xpath = ''
if len(frame_list) > 0:
frame = frame_list[0]
original_target = frame.getparent()
target_xpath = frame_xpath
elif len(section_list) > 0:
original_target = section_list[0]
target_xpath = section_xpath
office_body = original_target.getparent()
# remove if no report section
if len(report_section_list) is 0:
office_body.remove(original_target)
return None
return (target_xpath, original_target)
def _setUniqueElementName(self, base_name='', iteration_index=0, xpath='', element_tree=None):
"""create a unique element name and set it to the element tree
......
......@@ -56,9 +56,13 @@ class TestFormPrintout(ERP5TypeTestCase):
'Foo_001.odt')
foo2_file_path = os.path.join(os.path.dirname(__file__),
'test_document',
'Foo_002.odt')
'Foo_002.odt')
foo3_file_path = os.path.join(os.path.dirname(__file__),
'test_document',
'Foo_003.odt')
foo_file = open(foo_file_path, 'rb')
foo2_file = open(foo2_file_path, 'rb')
foo3_file = open(foo3_file_path, 'rb')
custom = self.portal.portal_skins.custom
addStyleSheet = custom.manage_addProduct['OFSP'].manage_addFile
if custom._getOb('Foo_getODTStyleSheet', None) is None:
......@@ -66,7 +70,10 @@ class TestFormPrintout(ERP5TypeTestCase):
precondition='', content_type = 'application/vnd.oasis.opendocument.text')
if custom._getOb('Foo2_getODTStyleSheet', None) is None:
addStyleSheet(id='Foo2_getODTStyleSheet', file=foo2_file, title='',
precondition='', content_type = 'application/vnd.oasis.opendocument.text')
precondition='', content_type = 'application/vnd.oasis.opendocument.text')
if custom._getOb('Foo3_getODTStyleSheet', None) is None:
addStyleSheet(id='Foo3_getODTStyleSheet', file=foo3_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:
......@@ -652,7 +659,8 @@ class TestFormPrintout(ERP5TypeTestCase):
"""
Iteration using ERP5Report ReportSection test
"""
if not run: return
if not run: return
# create test target
custom = self.portal.portal_skins.custom
erp5form = custom.manage_addProduct['ERP5Form']
erp5form.addERP5Report(id='FooReport_view', title='Foo Report')
......@@ -699,7 +707,9 @@ r2 = ReportSection(path=context.getPhysicalPath(),
report_section_list = [r1, r2]
return report_section_list
"""
)
)
# 01. normal case using Frame
test1 = self.portal.foo_module.test1
foo_report_printout = test1.FooReport_viewAsPrintout
foo_report_printout.doSettings(REQUEST=None,
......@@ -724,7 +734,7 @@ return report_section_list
self._validate(odf_document)
# 02. no report section
# 02. no report section using frame
custom.manage_delObjects(['FooReport_getReportSectionList'])
createZODBPythonScript(
self.portal.portal_skins.custom,
......@@ -747,6 +757,111 @@ return []
# the frame was removed
self.assertEqual(len(frame_list), 0)
self._validate(odf_document)
def test_04_Iteration_02_Section(self, run=run_all_test):
"""
Iteration using ERP5Report ReportSection and ODF Section test
"""
if not run: return
# create test target
custom = self.portal.portal_skins.custom
erp5form = custom.manage_addProduct['ERP5Form']
erp5form.addERP5Report(id='FooReport_view', title='Foo Report')
foo_report_view = custom.FooReport_view
foo_report_view.report_method = 'FooReport_getReportSectionList'
erp5form.addERP5Form(id='Foo2_view', title='Foo2')
foo2_view = custom.Foo2_view
foo2_view.manage_addField('listbox_report', 'listbox report', 'ListBox')
listbox = foo2_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)
createZODBPythonScript(
self.portal.portal_skins.custom,
'FooReport_getReportSectionList',
'',
r"""
from Products.ERP5Form.Report import ReportSection
r1 = ReportSection(path=context.getPhysicalPath(),
form_id='Foo2_view',
selection_name='listbox_report_selection',
selection_params={'title':'foo_04_Iteration_1'})
r2 = ReportSection(path=context.getPhysicalPath(),
form_id='Foo2_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
foo_report_printout = test1.FooReport_viewAsPrintout
foo_report_printout.doSettings(REQUEST=None,
title='',
form_name='FooReport_view',
template='Foo3_getODTStyleSheet')
odf_document = foo_report_printout()
test_output = open("/tmp/test_04_Iteratoin_02_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="FooReport_getReportSectionList"]'
section_list = content.xpath(section_xpath, namespaces=content.nsmap)
self.assertEqual(len(section_list), 1)
section1_xpath = '//text:section[@text:name="FooReport_getReportSectionList_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="FooReport_getReportSectionList"]'
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):
"""
......
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