From d8b09e63092822dc917c09bad5a3724e7e85ee08 Mon Sep 17 00:00:00 2001 From: Nicolas Delaby <nicolas@nexedi.com> Date: Tue, 14 Dec 2010 16:22:28 +0000 Subject: [PATCH] Implement render_odt_variable for TextWidget, FloatWidget, IntegerWidget and DateTimeWidget. render_odt_variable is able to render text:variable-set nodes like: <text:variable-set text:name="my_title" office:value-type="string">Title</text:variable-set> or <text:variable-set text:name="my_float" office:value-type="float" office:value="0.23" style:data-style-name="N2">0.23</text:variable-set> git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@41427 20353a03-c40f-0410-a6d1-a30d3c3de9de --- product/Formulator/Field.py | 19 ++++++++ product/Formulator/Widget.py | 89 ++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/product/Formulator/Field.py b/product/Formulator/Field.py index 0bb6c3a4b6..d554b97bb2 100644 --- a/product/Formulator/Field.py +++ b/product/Formulator/Field.py @@ -221,6 +221,16 @@ class Field: REQUEST, render_prefix, attr_dict, local_name) + security.declarePrivate('_render_odt_variable_helper') + def _render_odt_variable_helper(self, key, value, as_string, ooo_builder, + REQUEST, render_prefix, attr_dict, local_name): + value = self._get_default(key, value, REQUEST) + __traceback_info__ = ('key=%s value=%r' % (key, value)) + return self.widget.render_odt_variable(self, value, as_string, + ooo_builder, REQUEST, + render_prefix, attr_dict, + local_name) + security.declarePrivate('_get_default') def _get_default(self, key, value, REQUEST): if value is not None: @@ -315,6 +325,15 @@ class Field: ooo_builder, REQUEST, render_prefix, attr_dict, local_name) + security.declareProtected('View', 'render_odt_variable') + def render_odt_variable(self, key=None, value=None, as_string=True, + ooo_builder=None, REQUEST=None, render_prefix=None, attr_dict=None, + local_name='variable-set', key_prefix=None): + field_key = self.generate_field_key(key=key, key_prefix=key_prefix) + return self._render_odt_variable_helper(field_key, value, as_string, + ooo_builder, REQUEST, render_prefix, + attr_dict, local_name) + security.declareProtected('View', 'render_odt_view') def render_odt_view(self, value=None, as_string=True, ooo_builder=None, REQUEST=None, render_prefix=None, attr_dict=None, local_name='p'): diff --git a/product/Formulator/Widget.py b/product/Formulator/Widget.py index 36bb135127..9dc4475a9e 100644 --- a/product/Formulator/Widget.py +++ b/product/Formulator/Widget.py @@ -224,6 +224,28 @@ class Widget: return etree.tostring(text_node) return text_node + def render_odt_variable(self, field, value, as_string, ooo_builder, REQUEST, + render_prefix, attr_dict, local_name): + """ + Return a field value rendered in odt format as read-only mode. + - as_string return value as string or as xml object + - attr_dict can be used for additional attributes (like style). + - ooo_builder wrapper of ODF zipped archive usefull to insert images + - local_name local-name of the node returned by this render + """ + if attr_dict is None: + attr_dict = {} + attr_dict['{%s}value-type' % OFFICE_URI] = 'string' + if isinstance(value, str): + #required by lxml + value = value.decode('utf-8') + text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP) + text_node.text = value + text_node.attrib.update(attr_dict) + if as_string: + return etree.tostring(text_node) + return text_node + def render_odg(self, field, value, as_string, ooo_builder, REQUEST, render_prefix, attr_dict, local_name): """This render dedicated to render fields inside OOo document @@ -1496,6 +1518,27 @@ class DateTimeWidget(Widget): return etree.tostring(text_node) return text_node + def render_odt_variable(self, field, value, as_string, ooo_builder, REQUEST, + render_prefix, attr_dict, local_name): + """ + Return a field value rendered in odt format as read-only mode. + - as_string return value as string or as xml object + - attr_dict can be used for additional attributes (like style). + - ooo_builder wrapper of ODF zipped archive usefull to insert images + - local_name local-name of the node returned by this render + """ + if attr_dict is None: + attr_dict = {} + attr_dict['{%s}value-type' % OFFICE_URI] = 'date' + if not value and field.get_value('default_now'): + value = DateTime() + text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP) + attr_dict['{%s}date-value' % OFFICE_URI] = value.strftime('%Y-%m-%d %H:%M:%S') + text_node.attrib.update(attr_dict) + if as_string: + return etree.tostring(text_node) + return text_node + def render_odg_view(self, field, value, as_string, ooo_builder, REQUEST, render_prefix, attr_dict, local_name): """Transform DateTime into string then call default renderer @@ -1644,6 +1687,29 @@ class IntegerWidget(TextWidget) : ooo_builder, REQUEST, render_prefix, attr_dict, local_name) + def render_odt_variable(self, field, value, as_string, ooo_builder, REQUEST, + render_prefix, attr_dict, local_name): + """ + Return a field value rendered in odt format as read-only mode. + - as_string return value as string or as xml object + - attr_dict can be used for additional attributes (like style). + - ooo_builder wrapper of ODF zipped archive usefull to insert images + - local_name local-name of the node returned by this render + """ + if attr_dict is None: + attr_dict = {} + attr_dict['{%s}value-type' % OFFICE_URI] = 'float' + if isinstance(value, str): + #required by lxml + value = value.decode('utf-8') + text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP) + text_node.text = str(value) + attr_dict['{%s}value' % OFFICE_URI] = str(value) + text_node.attrib.update(attr_dict) + if as_string: + return etree.tostring(text_node) + return text_node + IntegerWidgetInstance = IntegerWidget() class FloatWidget(TextWidget): @@ -1837,6 +1903,29 @@ class FloatWidget(TextWidget): return etree.tostring(text_node) return text_node + def render_odt_variable(self, field, value, as_string, ooo_builder, REQUEST, + render_prefix, attr_dict, local_name): + """ + Return a field value rendered in odt format as read-only mode. + - as_string return value as string or as xml object + - attr_dict can be used for additional attributes (like style). + - ooo_builder wrapper of ODF zipped archive usefull to insert images + - local_name local-name of the node returned by this render + """ + if attr_dict is None: + attr_dict = {} + attr_dict['{%s}value-type' % OFFICE_URI] = 'float' + if isinstance(value, str): + #required by lxml + value = value.decode('utf-8') + text_node = Element('{%s}%s' % (TEXT_URI, local_name), nsmap=NSMAP) + text_node.text = str(value) + attr_dict['{%s}value' % OFFICE_URI] = str(value) + text_node.attrib.update(attr_dict) + if as_string: + return etree.tostring(text_node) + return text_node + def render_odg(self, field, value, as_string, ooo_builder, REQUEST, render_prefix, attr_dict, local_name): if attr_dict is None: -- 2.30.9