Commit a42a9edb authored by Nicolas Delaby's avatar Nicolas Delaby

- Add local_name parameter to overload name of odf nodes 'text:p' or 'text:span'

- Customise render_odt for DateTimeWidget and TextAreaWidget


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@31033 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 8653e043
......@@ -280,8 +280,8 @@ class Field:
render_prefix)
security.declareProtected('View', 'render_odt')
def render_odt(self, as_string=False, attr_dict=None):
return self.widget.render_odt(self, as_string, attr_dict=attr_dict)
def render_odt(self, as_string=False, local_name='p', attr_dict=None):
return self.widget.render_odt(self, as_string, local_name, attr_dict=attr_dict)
security.declareProtected('View', 'render_css')
def render_css(self, REQUEST=None):
......
......@@ -8,8 +8,8 @@ import types
from DocumentTemplate.ustr import ustr
from urlparse import urljoin
from lxml import etree
from lxml.etree import Element
from lxml.etree import Element, SubElement
import re
DRAW_URI = 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0'
......@@ -167,7 +167,7 @@ class Widget:
"""
return None
def render_odt(self, field, as_string, attr_dict=None):
def render_odt(self, field, as_string, local_name, attr_dict=None):
"""
Return a field value rendered in odt format.
- as_string return value as string or as xml object
......@@ -175,7 +175,7 @@ class Widget:
"""
if attr_dict is None:
attr_dict = {}
text_node = Element('{%s}%s' % (TEXT_URI, 'p'), nsmap=NSMAP)
text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP)
# get the field value
text_node.text = field.get_value('default').decode('utf-8')
text_node.attrib.update(attr_dict)
......@@ -429,6 +429,28 @@ class TextAreaWidget(Widget):
return ''
return value
def render_odt(self, field, as_string, local_name, attr_dict=None):
if attr_dict is None:
attr_dict = {}
text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP)
value = field.get_value('default').decode('utf-8')
value.replace('\r', '')
def replaceCharsByNode(match_object):
#global text_node
if match_object.group(1) is None:
text_node.text = match_object.group(2)
if match_object.group(1) == '\n':
line_break = SubElement(text_node, '{%s}%s' % (TEXT_URI, 'line-break'))
line_break.tail = match_object.group(2)
if match_object.group(1) == '\t':
line_break = SubElement(text_node, '{%s}%s' % (TEXT_URI, 'tab'))
line_break.tail = match_object.group(2)
re.sub('([\n\t])?([^\n\t]*)', replaceCharsByNode, value)
text_node.attrib.update(attr_dict)
if as_string:
return etree.tostring(text_node)
return text_node
TextAreaWidgetInstance = TextAreaWidget()
class LinesTextAreaWidget(TextAreaWidget):
......@@ -1199,7 +1221,7 @@ class DateTimeWidget(Widget):
def render_pdf(self, field, value, render_prefix=None):
return self.format_value(field, value, mode='pdf')
def render_odt(self, field, as_string, attr_dict=None):
def render_odt(self, field, as_string, local_name, attr_dict=None):
"""
Return a field value rendered in odt format.
- as_string return value as string or as xml object
......@@ -1207,7 +1229,7 @@ class DateTimeWidget(Widget):
"""
if attr_dict is None:
attr_dict = {}
text_node = Element('{%s}%s' % (TEXT_URI, 'p'), nsmap=NSMAP)
text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP)
# get the field value
text_node.text = self.format_value(field, field.get_value('default'), mode='pdf').decode('utf-8')
text_node.attrib.update(attr_dict)
......@@ -1518,6 +1540,17 @@ class FloatWidget(TextWidget):
'format': format,
'type': 'float'}
def render_odt(self, field, as_string, local_name, attr_dict=None):
if attr_dict is None:
attr_dict = {}
text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP)
# get the field value
text_node.text = self.format_value(field, field.get_value('default')).decode('utf-8')
text_node.attrib.update(attr_dict)
if as_string:
return etree.tostring(text_node)
return text_node
FloatWidgetInstance = FloatWidget()
class LinkWidget(TextWidget):
......
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