[Erp5-report] r26509 - in /erp5/trunk/products/ERP5OOo: ./ tests/ tests/test_document/

nobody at svn.erp5.org nobody at svn.erp5.org
Mon Apr 20 10:18:40 CEST 2009


Author: tatuya
Date: Mon Apr 20 10:18:38 2009
New Revision: 26509

URL: http://svn.erp5.org?rev=26509&view=rev
Log:
- when using ReportSection recreate a element name
- fix the address in the GPL 
- append XML validation

Modified:
    erp5/trunk/products/ERP5OOo/FormPrintout.py
    erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py
    erp5/trunk/products/ERP5OOo/tests/test_document/Foo_001.odt

Modified: erp5/trunk/products/ERP5OOo/FormPrintout.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5OOo/FormPrintout.py?rev=26509&r1=26508&r2=26509&view=diff
==============================================================================
--- erp5/trunk/products/ERP5OOo/FormPrintout.py [utf8] (original)
+++ erp5/trunk/products/ERP5OOo/FormPrintout.py [utf8] Mon Apr 20 10:18:38 2009
@@ -22,12 +22,12 @@
 #
 # 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.
-#
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
+# USA.
 ##############################################################################
 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
 from Products.PageTemplates.PageTemplateFile import PageTemplateFile
-from Products.CMFCore.utils import _checkPermission, getToolByName
+from Products.CMFCore.utils import _checkPermission
 from Products.PythonScripts.Utility import allow_class
 from Products.ERP5Type import PropertySheet, Permissions
 from Products.ERP5Form.ListBox import ListBox
@@ -316,14 +316,15 @@
     return ooo_builder
 
   def _replaceXmlByForm(self, element_tree=None, form=None, here=None,
-                           extra_context=None, ooo_builder=None):
+                           extra_context=None, ooo_builder=None, iteration_index=0):
     field_list = form.get_fields(include_disabled=1) 
     REQUEST = get_request()
     for (count, field) in enumerate(field_list):
       if isinstance(field, ListBox):
         element_tree = self._appendTableByListbox(element_tree=element_tree,
                                                   listbox=field,
-                                                  REQUEST=REQUEST)
+                                                  REQUEST=REQUEST,
+                                                  iteration_index=iteration_index)
       elif isinstance(field, FormBox):
         if not hasattr(here, field.get_value('formbox_target_id')):
           continue
@@ -332,23 +333,25 @@
                                             field=field,
                                             form=sub_form,
                                             extra_context=extra_context,
-                                            ooo_builder=ooo_builder)
+                                            ooo_builder=ooo_builder,
+                                            iteration_index=iteration_index)
       elif isinstance(field, ImageField):
         element_tree = self._replaceXmlByImageField(element_tree=element_tree,
                                                     image_field=field,
-                                                    ooo_builder=ooo_builder)
+                                                    ooo_builder=ooo_builder,
+                                                    iteration_index=iteration_index)
       else:
         element_tree = self._replaceNodeViaReference(element_tree=element_tree,
-                                                     field=field)
+                                                     field=field, iteration_index=iteration_index)
     return element_tree
 
-  def _replaceNodeViaReference(self, element_tree=None, field=None):
+  def _replaceNodeViaReference(self, element_tree=None, field=None, iteration_index=0):
     """replace nodes (e.g. paragraphs) via ODF reference"""
     element_tree = self._replaceNodeViaRangeReference(element_tree=element_tree, field=field)
     element_tree = self._replaceNodeViaPointReference(element_tree=element_tree, field=field)
     return element_tree
   
-  def _replaceNodeViaPointReference(self, element_tree=None, field=None):
+  def _replaceNodeViaPointReference(self, element_tree=None, field=None, iteration_index=0):
     """replace via ODF point reference
     
     point reference example:
@@ -370,9 +373,14 @@
           paragraph_node.text = value
       else:
         self._appendParagraphsWithLineList(target_node=target_node, line_list=field_value)
+    # set when using report section
+    self._setUniqueElementName(base_name=field.id,
+                               iteration_index=iteration_index,
+                               xpath=reference_xpath,
+                               element_tree=element_tree)
     return element_tree
   
-  def _replaceNodeViaRangeReference(self, element_tree=None, field=None):
+  def _replaceNodeViaRangeReference(self, element_tree=None, field=None, iteration_index=0):
     """replace via ODF range reference
 
     range reference example:
@@ -395,6 +403,12 @@
         node.tail = ''
     else:
       self._appendParagraphsWithLineList(target_node=target_node, line_list=field_value)
+
+    # set when using report section
+    self._setUniqueElementName(base_name=field.id,
+                               iteration_index=iteration_index,
+                               xpath=range_reference_xpath,
+                               element_tree=element_tree)
     return element_tree
 
   def _appendParagraphsWithLineList(self, target_node=None, line_list=None):
@@ -459,39 +473,57 @@
       if index is 0:
         office_body.remove(frame_paragraph)
       else:
-        self._setReportSectionFrameName(report_method_name=report_method.__name__,
-                                        frame_paragraph_index=index,
-                                        frame_paragraph_element_tree=frame_paragraph_element_tree)
+        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)
+                                                            ooo_builder=ooo_builder,
+                                                            iteration_index=index)
       office_body.insert(frame_paragraph_index, frame_paragraph_element_tree)
       frame_paragraph_index += 1
       report_item.popReport(portal_object, render_prefix=None)
     return element_tree
 
-  def _setReportSectionFrameName(self,
-                                 report_method_name='',
-                                 frame_paragraph_index=0,
-                                 frame_paragraph_element_tree=None):
-    report_section_frame_name =  "%s_%s" % (report_method_name, frame_paragraph_index)
-    draw_name_attribute = '{%s}name' % frame_paragraph_element_tree.nsmap['draw']
-    report_section_frame = frame_paragraph_element_tree.xpath(
-      'draw:frame[@draw:name="%s"]' % report_method_name,
-      namespaces=frame_paragraph_element_tree.nsmap)
-    if len(report_section_frame) is 0:
+  def _setUniqueElementName(self, base_name='', iteration_index=0, xpath='', element_tree=None):
+    """create a unique element name and set it to the element tree
+
+    Keyword arguments:
+    base_name -- the base name of the element
+    iteration_index -- iteration index
+    xpath -- xpath expression which was used to search the element
+    element_tree -- element tree
+    """
+    if iteration_index is 0:
       return
-    report_section_frame[0].set(draw_name_attribute, report_section_frame_name)
-  
+    def getNameAttribute(target_element=None):
+      if target_element is None:
+        return None
+      attrib = target_element.attrib
+      for key in attrib.keys():
+        if key.endswith("}name"):
+          return key
+      return None
+    odf_element_name =  "%s_%s" % (base_name, iteration_index)
+    result_list = element_tree.xpath(xpath, namespaces=element_tree.nsmap)
+    if len(result_list) is 0:
+      return
+    target_element = result_list[0]
+    name_attribute = getNameAttribute(target_element)
+    if name_attribute is not None:
+      target_element.set(name_attribute, odf_element_name)
+ 
   def _replaceXmlByFormbox(self,
                            element_tree=None,
                            field=None,
                            form=None,
                            extra_context=None,
-                           ooo_builder=None):
+                           ooo_builder=None,
+                           iteration_index=0):
     field_id = field.id
     enabled = field.get_value('enabled')
     draw_xpath = '//draw:frame[@draw:name="%s"]/draw:text-box/*' % field_id
@@ -504,14 +536,24 @@
     if not enabled:
       office_body.remove(frame_paragraph)
       return element_tree
+    # set when using report section
+    self._setUniqueElementName(base_name=field_id,
+                               iteration_index=iteration_index,
+                               xpath=draw_xpath,
+                               element_tree=element_tree)
     self._replaceXmlByForm(element_tree=frame_paragraph,
                            form=form,
                            here=extra_context['here'],
                            extra_context=extra_context,
-                           ooo_builder=ooo_builder)
+                           ooo_builder=ooo_builder,
+                           iteration_index=iteration_index)
     return element_tree
 
-  def _replaceXmlByImageField(self, element_tree=None, image_field=None, ooo_builder=None):
+  def _replaceXmlByImageField(self,
+                              element_tree=None,
+                              image_field=None,
+                              ooo_builder=None,
+                              iteration_index=0):
     alt = image_field.get_value('description') or image_field.get_value('title')
     image_xpath = '//draw:frame[@draw:name="%s"]/*' % image_field.id
     image_list = element_tree.xpath(image_xpath, namespaces=element_tree.nsmap)
@@ -531,6 +573,11 @@
     image_frame = image_node.getparent()
     image_frame.set('{%s}width' % element_tree.nsmap['svg'], picture_size[0])
     image_frame.set('{%s}height' % element_tree.nsmap['svg'], picture_size[1])
+    # set when using report section
+    self._setUniqueElementName(base_name=image_field.id,
+                               iteration_index=iteration_index,
+                               xpath=image_xpath,
+                               element_tree=element_tree)
     return element_tree
 
   def _createOdfUniqueFileName(self, path='', picture_type=''):
@@ -583,7 +630,8 @@
   def _appendTableByListbox(self,
                             element_tree=None, 
                             listbox=None,
-                            REQUEST=None):
+                            REQUEST=None,
+                            iteration_index=0):
     table_id = listbox.id
     table_xpath = '//table:table[@table:name="%s"]' % table_id
     # this list should be one item list
@@ -642,11 +690,21 @@
         row = self._updateColumnValue(row, listbox_column_list)
         newtable.append(row)
 
+    self._setUniqueElementName(base_name=table_id,
+                               iteration_index=iteration_index,
+                               xpath=table_xpath,
+                               element_tree=newtable)
     parent_paragraph.insert(target_index, newtable)
  
     return element_tree
 
   def _copyRowStyle(self, table_row_list=[], has_header_rows=False):
+    def removeOfficeAttribute(row):
+      if row is None or has_header_rows: return
+      odf_cell_list = row.findall("{%s}table-cell" % row.nsmap['table'])
+      for odf_cell in odf_cell_list:
+        self._removeColumnValue(odf_cell)
+          
     row_top = None
     row_middle = None
     row_bottom = None
@@ -667,6 +725,9 @@
         row_top = deepcopy(table_row_list[0])
         row_middle = deepcopy(table_row_list[1])
         row_bottom = deepcopy(table_row_list[-1])
+
+    # remove office attribute if create a new header row 
+    removeOfficeAttribute(row_top)
     return (row_top, row_middle, row_bottom)
 
   def _updateColumnValue(self, row=None, listbox_column_list=[]):

Modified: erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py
URL: http://svn.erp5.org/erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py?rev=26509&r1=26508&r2=26509&view=diff
==============================================================================
--- erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py [utf8] (original)
+++ erp5/trunk/products/ERP5OOo/tests/testFormPrintout.py [utf8] Mon Apr 20 10:18:38 2009
@@ -22,7 +22,8 @@
 #
 # 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.
+# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301,
+# USA.
 #
 ##############################################################################
 
@@ -144,6 +145,7 @@
                      'application/vnd.oasis.opendocument.text; charset=utf-8')
     self.assertEqual(request.RESPONSE.getHeader('content-disposition'),
                      'inline;filename="Foo_viewAsPrintout.odt"')
+    self._validate(odf_document)
     
     # 2. Normal case: change the field value and check again the ODF document
     test1.setTitle("Changed Title!")
@@ -153,7 +155,8 @@
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
     self.assertTrue(content_xml.find("Changed Title!") > 0)
-
+    self._validate(odf_document)
+    
     # 3. False case: change the field name 
     test1.setTitle("you cannot find")
     # rename id 'my_title' to 'xxx_title', then does not match in the ODF document
@@ -164,6 +167,7 @@
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
     self.assertFalse(content_xml.find("you cannot find") > 0)
+    self._validate(odf_document)
     # put back
     foo_form.manage_renameObject('xxx_title', 'my_title', REQUEST=request)
 
@@ -191,7 +195,8 @@
     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._validate(odf_document)
+    
     # 5. Normal case: utf-8 string
     test1.setTitle("Français")
     odf_document = foo_printout() 
@@ -199,7 +204,8 @@
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
     self.assertTrue(content_xml.find("Français") > 0)
-
+    self._validate(odf_document)
+    
     # 6. Normal case: unicode string
     test1.setTitle(u'Français test2')
     odf_document = foo_printout() 
@@ -207,7 +213,8 @@
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
     self.assertTrue(content_xml.find("Français test2") > 0)
-    
+    self._validate(odf_document)
+        
   def test_02_Table_01_Normal(self, run=run_all_test):
     """To test listbox and ODF table mapping
     
@@ -258,7 +265,8 @@
     builder = OOoBuilder(odf_document)
     content_xml = builder.extract("content.xml")
     self.assertTrue(content_xml.find("foo_title_1") > 0)
-
+    self._validate(odf_document)
+    
   def test_02_Table_02_SmallerThanListboxColumns(self, run=run_all_test):
     """2. Irregular case: listbox columns count smaller than table columns count"""
     if not run: return 
@@ -300,6 +308,7 @@
     content_xml = builder.extract("content.xml")
     self.assertFalse(content_xml.find("foo_title_1") > 0)
     self.assertTrue(content_xml.find("foo_title_2") > 0)
+    self._validate(odf_document)
 
   def test_02_Table_03_ListboxColumnsLargerThanTable(self, run=run_all_test):
     """3. Irregular case: listbox columns count larger than table columns count"""
@@ -336,7 +345,8 @@
     content_xml = builder.extract("content.xml")
     self.assertFalse(content_xml.find("foo_title_2") > 0)
     self.assertTrue(content_xml.find("foo_title_3") > 0)
-
+    self._validate(odf_document)
+    
   def test_02_Table_04_ListboxHasNotStat(self, run=run_all_test):
     """4. Irregular case: listbox has not a stat line, but table has a stat line"""
     if not run: return 
@@ -385,7 +395,8 @@
     span_attribute = "{%s}number-columns-spanned" % content.nsmap['table']
     self.assertFalse(first_row_columns[0].attrib.has_key(span_attribute))
     self.assertEqual(int(last_row_columns[0].attrib[span_attribute]), 2)
-
+    self._validate(odf_document)
+    
   def test_02_Table_05_NormalSameLayout(self, run=run_all_test):
     """5. Normal case: the listobx and the ODF table are same layout
 
@@ -431,6 +442,7 @@
     content_xml = builder.extract("content.xml")
     self.assertFalse(content_xml.find("foo_title_4") > 0)
     self.assertTrue(content_xml.find("foo_title_5") > 0)
+    self._validate(odf_document)
     
     # put back the field name
     foo_form.manage_renameObject('listbox2', 'listbox', REQUEST=request)
@@ -474,14 +486,15 @@
     self.assertTrue(listboxline_list[1].getColumnProperty('title') == "foo_title_6")
     
     odf_document = foo_printout.index_html(REQUEST=request)
-    #test_output = open("/tmp/test_02_06_Table.odf", "w")
-    #test_output.write(odf_document)
+    test_output = open("/tmp/test_02_06_Table.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_title_5") > 0)
     self.assertTrue(content_xml.find("foo_title_6") > 0)
-
+    self._validate(odf_document)
+    
     # put back the field name
     foo_form.manage_renameObject('listbox3', 'listbox', REQUEST=request)
 
@@ -530,7 +543,7 @@
     date_value_attrib = "{%s}date-value" % content.nsmap['office']
     self.assertTrue(date_column.attrib.has_key(date_value_attrib))
     self.assertEqual(date_column.attrib[date_value_attrib], '2009-04-20')
-
+    self._validate(odf_document)
 
   def test_02_Table_08_Nodata(self, run=run_all_test):
     """7. Normal case: list box has no data"""
@@ -564,7 +577,8 @@
     odf_table_rows = content.xpath(table_row_xpath, namespaces=content.nsmap)
     # no rows
     self.assertEqual(len(odf_table_rows), 0)
-
+    self._validate(odf_document)
+    
   def _test_03_Frame(self, run=run_all_test):
     """
     Frame not supported yet
@@ -667,7 +681,8 @@
     frame_list = content.xpath(frame_xpath, namespaces=content.nsmap)
     # the frame was removed
     self.assertEqual(len(frame_list), 0)
-     
+    self._validate(odf_document)
+    
   def _test_05_Styles(self, run=run_all_test):
     """
     styles.xml not tested yet

Modified: erp5/trunk/products/ERP5OOo/tests/test_document/Foo_001.odt
URL: http://svn.erp5.org/erp5/trunk/products/ERP5OOo/tests/test_document/Foo_001.odt?rev=26509&r1=26508&r2=26509&view=diff
==============================================================================
Binary files - no diff available.




More information about the Erp5-report mailing list