Commit b32693fd authored by Tatuya Kamada's avatar Tatuya Kamada

* Fix

 - LinesField via point-reference did not work,
   because it looked wrong node
 - LinesField insert order was wrong, it was reversed

* Append
 replacing some control charactors such as 
 tabs and line-breaks
 


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@26637 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 61bcc04c
...@@ -45,6 +45,7 @@ from zLOG import LOG, DEBUG, INFO, WARNING ...@@ -45,6 +45,7 @@ from zLOG import LOG, DEBUG, INFO, WARNING
from mimetypes import guess_extension from mimetypes import guess_extension
from DateTime import DateTime from DateTime import DateTime
from decimal import Decimal from decimal import Decimal
from xml.sax.saxutils import escape
import re import re
try: try:
...@@ -363,15 +364,16 @@ class ODFStrategy(Implicit): ...@@ -363,15 +364,16 @@ class ODFStrategy(Implicit):
reference_xpath = '//text:reference-mark[@text:name="%s"]' % field_id reference_xpath = '//text:reference-mark[@text:name="%s"]' % field_id
reference_list = element_tree.xpath(reference_xpath, namespaces=element_tree.nsmap) reference_list = element_tree.xpath(reference_xpath, namespaces=element_tree.nsmap)
if len(reference_list) > 0: if len(reference_list) > 0:
target_node = reference_list[0]
paragraph_node = reference_list[0].getparent() paragraph_node = reference_list[0].getparent()
parent_node = paragraph_node.getparent() parent_node = paragraph_node.getparent()
# remove such a "bbb": <text:p>aaa<br/>bbb</text:p>
if not isinstance(field_value, list): if not isinstance(field_value, list):
# remove such a "bbb": <text:p>aaa<text:line-break/>bbb</text:p>
for child in paragraph_node.getchildren(): for child in paragraph_node.getchildren():
child.tail = '' child.tail = ''
paragraph_node.text = value paragraph_node.text = value
else: else:
self._appendParagraphsWithLineList(target_node=paragraph_node, line_list=field_value) self._appendParagraphsWithLineList(target_node=target_node, line_list=field_value)
# set when using report section # set when using report section
self._setUniqueElementName(base_name=field.id, self._setUniqueElementName(base_name=field.id,
iteration_index=iteration_index, iteration_index=iteration_index,
...@@ -394,6 +396,7 @@ class ODFStrategy(Implicit): ...@@ -394,6 +396,7 @@ class ODFStrategy(Implicit):
target_node = reference_list[0] target_node = reference_list[0]
if not isinstance(field_value, list): if not isinstance(field_value, list):
target_node.tail = value target_node.tail = value
# clear text until 'reference-mark-end'
for node in target_node.itersiblings(): for node in target_node.itersiblings():
end_tag_name = '{%s}reference-mark-end' % element_tree.nsmap['text'] end_tag_name = '{%s}reference-mark-end' % element_tree.nsmap['text']
name_attribute = '{%s}name' % element_tree.nsmap['text'] name_attribute = '{%s}name' % element_tree.nsmap['text']
...@@ -438,7 +441,8 @@ class ODFStrategy(Implicit): ...@@ -438,7 +441,8 @@ class ODFStrategy(Implicit):
paragraph_node_index = parent_node.index(paragraph_node) paragraph_node_index = parent_node.index(paragraph_node)
parent_node.remove(paragraph_node) parent_node.remove(paragraph_node)
for (index, paragraph) in enumerate(paragraph_list): for (index, paragraph) in enumerate(paragraph_list):
parent_node.insert(paragraph_node_index + 1, paragraph) 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): 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: if not extra_context.has_key('report_method') or extra_context['report_method'] is None:
...@@ -762,25 +766,58 @@ class ODFStrategy(Implicit): ...@@ -762,25 +766,58 @@ class ODFStrategy(Implicit):
def _setColumnValue(self, column, value): def _setColumnValue(self, column, value):
self._clearColumnValue(column) self._clearColumnValue(column)
if value is None: if value is None:
value = ''
self._removeColumnValue(column) self._removeColumnValue(column)
if isinstance(value, DateTime): column_value, table_content = self._translateValueIntoColumnContent(value, column)
value = value.strftime('%Y-%m-%d') for child in column.getchildren():
column_value = unicode(str(value),'utf-8')
column_children = column.getchildren()
first_child = None
if len(column_children) > 0:
first_child = deepcopy(column_children[0])
first_child.text = column_value
for child in column_children:
column.remove(child) column.remove(child)
if first_child is not None: if table_content is not None:
column.append(first_child) column.append(table_content)
if column_value != '':
value_attribute = self._getColumnValueAttribute(column) value_attribute = self._getColumnValueAttribute(column)
if value_attribute is not None: if value_attribute is not None and column_value is not None:
column.set(value_attribute, column_value) column.set(value_attribute, column_value)
def _translateValueIntoColumnContent(self, value, column):
"""translate a value as a table content"""
table_content = None
column_children = column.getchildren()
if len(column_children) > 0:
table_content = deepcopy(column_children[0])
# create a tempolaly etree object to generate a content paragraph
fragment = self._valueAsOdfXmlElement(value=value, element_tree=column)
column_value = None
if table_content is not None:
table_content.text = fragment.text
for element in fragment.getchildren():
table_content.append(element)
column_value = " ".join([x for x in table_content.itertext()])
return (column_value, table_content)
def _valueAsOdfXmlElement(self, value=None, element_tree=None):
"""values as ODF XML element
replacing:
\t -> tabs
\n -> line-breaks
DateTime -> Y-m-d
"""
if value is None:
value = ''
translated_value = str(value)
if isinstance(value, DateTime):
translated_value = value.strftime('%Y-%m-%d')
translated_value = escape(translated_value)
text_namespace = element_tree.nsmap['text']
tab_element_str = '<text:tab xmlns:text="%s"/>' % text_namespace
line_break_element_str ='<text:line-break xmlns:text="%s"/>' % text_namespace
translated_value = translated_value.replace('\t', tab_element_str)
translated_value = translated_value.replace('\r', '')
translated_value = translated_value.replace('\n', line_break_element_str)
translated_value = unicode(str(translated_value),'utf-8')
# create a paragraph
template = '<text:p xmlns:text="%s">%s</text:p>'
fragment_element_tree = etree.XML(template % (text_namespace, translated_value))
return fragment_element_tree
def _removeColumnValue(self, column): def _removeColumnValue(self, column):
# to eliminate a default value, remove "office:*" attributes. # to eliminate a default value, remove "office:*" attributes.
# if remaining these attribetes, the column shows its default value, # if remaining these attribetes, the column shows its default value,
......
...@@ -215,6 +215,28 @@ class TestFormPrintout(ERP5TypeTestCase): ...@@ -215,6 +215,28 @@ class TestFormPrintout(ERP5TypeTestCase):
self.assertTrue(content_xml.find("Français test2") > 0) self.assertTrue(content_xml.find("Français test2") > 0)
self._validate(odf_document) self._validate(odf_document)
def test_01_Paragraph_07_LinesField(self, run=run_all_test):
"""test LinesField into multi line"""
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("week", None) is None:
foo_form.manage_addField('week', 'week', 'LinesField')
week = foo_form.week
week.values['default'] = ['line1', 'line2']
odf_document = foo_printout()
self.assertTrue(odf_document is not None)
#test_output = open("/tmp/test_01_07_Paragraph.odf", "w")
#test_output.write(odf_document)
builder = OOoBuilder(odf_document)
content_xml = builder.extract("content.xml")
self.assertTrue(content_xml.find("line1") > 0)
self.assertTrue(content_xml.find("line2") > 0)
self.assertTrue(content_xml.find("line2") > content_xml.find("line1"))
self._validate(odf_document)
def test_02_Table_01_Normal(self, run=run_all_test): def test_02_Table_01_Normal(self, run=run_all_test):
"""To test listbox and ODF table mapping """To test listbox and ODF table mapping
......
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