From 3b0c9930e35fd7ee3a11c01d74986f47b3df1201 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 28 Jul 2018 12:49:02 +0200 Subject: [PATCH 1/3] misc: move RTF escape code to ezt processor (#25521) --- wcs/qommon/ezt.py | 15 +++++++++++++++ wcs/wf/export_to_model.py | 22 ++-------------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/wcs/qommon/ezt.py b/wcs/qommon/ezt.py index 5cfcf91b..d41e12f4 100644 --- a/wcs/qommon/ezt.py +++ b/wcs/qommon/ezt.py @@ -240,6 +240,7 @@ except ImportError: FORMAT_RAW = 'raw' FORMAT_HTML = 'html' FORMAT_XML = 'xml' +FORMAT_RTF = 'rtf' # # This regular expression matches three alternatives: @@ -283,6 +284,7 @@ class Template: FORMAT_RAW : '_cmd_print', FORMAT_HTML : '_cmd_print_html', FORMAT_XML : '_cmd_print_xml', + FORMAT_RTF : '_cmd_print_rtf', } def __init__(self, fname=None, compress_whitespace=1, @@ -473,6 +475,19 @@ class Template: def _cmd_print_html(self, valref, fp, ctx): _write_value(valref, fp, ctx, cgi.escape) + def _cmd_print_rtf(self, valref, fp, ctx): + def char2rtf(c): + if ord(c) < 128: + return c + else: + return '\\u%d?' % ord(c) + + def rtf_escape(s): + s = ''.join([char2rtf(c) for c in s]) + return '{\\uc1{%s}}' % s + + _write_value(valref, fp, ctx, rtf_escape) + def _cmd_print_xml(self, valref, fp, ctx): ### use the same quoting as HTML for now self._cmd_print_html(valref, fp, ctx) diff --git a/wcs/wf/export_to_model.py b/wcs/wf/export_to_model.py index 08f7d4d0..e30e52a6 100644 --- a/wcs/wf/export_to_model.py +++ b/wcs/wf/export_to_model.py @@ -28,7 +28,7 @@ from quixote import get_response, get_request, get_publisher from quixote.directory import Directory from quixote.html import htmltext -from qommon import _ +from qommon import _, ezt from qommon import get_logger from qommon.form import (SingleSelectWidget, WidgetList, CheckboxWidget, StringWidget, UploadWidget, WysiwygTextWidget, Upload, @@ -175,24 +175,6 @@ class ExportToModelDirectory(Directory): return self.wfstatusitem.apply_template_to_formdata(self.formdata).read() -def char2rtf(c): - if ord(c) < 128: - return c - else: - return '\\u%d?' % ord(c) - - -def str2rtf(s): - s = ''.join([char2rtf(c) for c in s]) - return '{\\uc1{%s}}' % s - - -def rtf_process(value): - if value is None: - return None - return str2rtf(unicode(str(value), get_publisher().site_charset)) - - class ExportToModel(WorkflowStatusItem): description = N_('Document Creation') key = 'export_to_model' @@ -421,7 +403,7 @@ class ExportToModel(WorkflowStatusItem): # force ezt_only=True because an RTF file may contain {{ characters # and would be seen as a Django template return StringIO(template_on_formdata(formdata, self.model_file.get_file().read(), - process=rtf_process, ezt_only=True)) + ezt_format=ezt.FORMAT_RTF, ezt_only=True)) except TemplateError as e: url = formdata.get_url() get_logger().error('error in template for export to model [%s]: %s' % (url, str(e))) -- 2.18.0