Commit f74409c1 authored by Nicolas Delaby's avatar Nicolas Delaby

Use render_odt on fields

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@31034 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent a42a9edb
...@@ -43,6 +43,7 @@ from OFS.SimpleItem import Item ...@@ -43,6 +43,7 @@ from OFS.SimpleItem import Item
from urllib import quote, quote_plus from urllib import quote, quote_plus
from copy import deepcopy from copy import deepcopy
from lxml import etree from lxml import etree
from lxml.etree import _Element, _ElementStringResult
from zLOG import LOG, DEBUG, INFO, WARNING 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
...@@ -897,6 +898,9 @@ class ODFStrategy(Implicit): ...@@ -897,6 +898,9 @@ class ODFStrategy(Implicit):
class ODTStrategy(ODFStrategy): class ODTStrategy(ODFStrategy):
"""ODTStrategy create a ODT Document from a form and a ODT template""" """ODTStrategy create a ODT Document from a form and a ODT template"""
_style_attribute_name = '{urn:oasis:names:tc:opendocument:xmlns:text:1.0}style-name'
def _replaceXmlByForm(self, element_tree=None, form=None, here=None, def _replaceXmlByForm(self, element_tree=None, form=None, here=None,
extra_context=None, ooo_builder=None, iteration_index=0): extra_context=None, ooo_builder=None, iteration_index=0):
""" """
...@@ -963,21 +967,16 @@ class ODTStrategy(ODFStrategy): ...@@ -963,21 +967,16 @@ class ODTStrategy(ODFStrategy):
<text:reference-mark text:name="invoice-date"/> <text:reference-mark text:name="invoice-date"/>
""" """
field_id = field.id field_id = field.id
field_value = self._renderField(field)
value = self._toUnicodeString(field_value)
# text:reference-mark text:name="invoice-date"
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: for target_node in reference_list:
target_node = reference_list[0] node_to_replace = target_node.xpath('ancestor::text:p[1]', namespaces=element_tree.nsmap)[0]
paragraph_node = reference_list[0].getparent() attr_dict = {}
if not isinstance(field_value, list): style_value = node_to_replace.attrib.get(self._style_attribute_name)
# remove such a "bbb": <text:p>aaa<text:line-break/>bbb</text:p> if style_value:
for child in paragraph_node.getchildren(): attr_dict.update({self._style_attribute_name: style_value})
child.tail = '' new_node = field.render_odt(attr_dict=attr_dict)
paragraph_node.text = value node_to_replace.getparent().replace(node_to_replace, new_node)
else:
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,
...@@ -991,34 +990,28 @@ class ODTStrategy(ODFStrategy): ...@@ -991,34 +990,28 @@ class ODTStrategy(ODFStrategy):
range reference example: range reference example:
<text:reference-mark-start text:name="week"/>Monday<text:reference-mark-end text:name="week"/> <text:reference-mark-start text:name="week"/>Monday<text:reference-mark-end text:name="week"/>
or or
<text:reference-mark-start text:name="my_title"/><text:span text:style-name="T1">title</text:span> <text:reference-mark-start text:name="my_title"/>
<text:span text:style-name="T1">title</text:span>
<text:reference-mark-end text:name="my_title"/> <text:reference-mark-end text:name="my_title"/>
""" """
field_value = self._renderField(field) field_id = field.id
value = self._toUnicodeString(field_value) range_reference_xpath = '//text:reference-mark-start[@text:name="%s"]/'\
range_reference_xpath = '//text:reference-mark-start[@text:name="%s"]' % field.id 'following-sibling::*[node()/'\
'following::text:reference-mark-end[@text:name="%s"]]' % (field_id, field_id)
reference_list = element_tree.xpath(range_reference_xpath, namespaces=element_tree.nsmap) reference_list = element_tree.xpath(range_reference_xpath, namespaces=element_tree.nsmap)
if len(reference_list) is 0: if not reference_list:
return element_tree return element_tree
target_node = reference_list[0] parent_node_xpath = '//text:reference-mark-start[@text:name="%s"]/parent::*[1]' % field_id
if not isinstance(field_value, list): parent_node = element_tree.xpath(parent_node_xpath, namespaces=element_tree.nsmap)[0]
next_node = target_node.getnext() text_reference_position = int(parent_node.xpath('count(preceding-sibling::text:reference-mark-start)', namespaces=element_tree.nsmap))
span_tag_name = '{%s}span' % element_tree.nsmap['text']
if next_node is not None and next_node.tag == span_tag_name: #Delete all contents between <text:reference-mark-start/> and <text:reference-mark-end/>
next_node.text = value #Try to fetch style-name
else: attr_dict = {}
target_node.tail = value [(attr_dict.update(target_node.attrib), parent_node.remove(target_node)) for target_node in reference_list]
# clear text until 'reference-mark-end' new_node = field.render_odt(local_name='span', attr_dict=attr_dict)
for node in target_node.itersiblings(): parent_node.insert(text_reference_position+1, new_node)
end_tag_name = '{%s}reference-mark-end' % element_tree.nsmap['text']
name_attribute = '{%s}name' % element_tree.nsmap['text']
if node.tag == end_tag_name and node.get(name_attribute) == field.id:
break
node.tail = ''
else:
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,
...@@ -1026,40 +1019,12 @@ class ODTStrategy(ODFStrategy): ...@@ -1026,40 +1019,12 @@ class ODTStrategy(ODFStrategy):
element_tree=element_tree) element_tree=element_tree)
return element_tree return element_tree
def _appendParagraphsWithLineList(self, target_node=None, line_list=None): # set when using report section
"""Create paragraphs using an ERP5 Form line list. self._setUniqueElementName(base_name=field.id,
iteration_index=iteration_index,
Keyword arguments: xpath=range_reference_xpath,
target_node -- target text node which is marked by an ODF reference. element_tree=element_tree)
line_list -- an ERP5 Form line list return element_tree
example:
--
first line
second line
--
<p:text>
first line
</p:text>
<p:text>
second line
</p:text>
"""
paragraph_node = target_node.getparent()
parent_node = paragraph_node.getparent()
paragraph_list = []
for line in line_list:
p = deepcopy(paragraph_node)
for child in p.getchildren():
child.tail = ''
value = self._toUnicodeString(line)
p.text = value
paragraph_list.append(p)
paragraph_node_index = parent_node.index(paragraph_node)
parent_node.remove(paragraph_node)
for (index, paragraph) in enumerate(paragraph_list):
parent_node.insert(paragraph_node_index, paragraph)
paragraph_node_index = paragraph_node_index + 1
class ODGStrategy(ODFStrategy): class ODGStrategy(ODFStrategy):
"""ODGStrategy create a ODG Document from a form and a ODG template""" """ODGStrategy create a ODG Document from a form and a ODG template"""
......
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