diff options
-rw-r--r-- | ishtar_common/models.py | 2 | ||||
-rw-r--r-- | ishtar_common/templates/error.html | 3 | ||||
-rw-r--r-- | ishtar_common/utils_secretary.py | 46 | ||||
-rw-r--r-- | ishtar_common/views.py | 3 |
4 files changed, 52 insertions, 2 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 176559934..35c35e97b 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -3378,6 +3378,8 @@ class DocumentTemplate(models.Model): raise TemplateSyntaxError(str(e), e.lineno) except UndefinedError as e: raise TemplateSyntaxError(str(e), 0) + except Exception as e: + raise TemplateSyntaxError(str(e), 0) output = open(output_name, 'wb') output.write(result) return output_name diff --git a/ishtar_common/templates/error.html b/ishtar_common/templates/error.html index 069796184..ba334da5a 100644 --- a/ishtar_common/templates/error.html +++ b/ishtar_common/templates/error.html @@ -3,4 +3,7 @@ {% block error %} <h3>{{error_title}}</h3> <p>{{error}}</p> +{% if back_url %} +<p><a href="{{back_url}}">{% trans "Back to your page" %}</a></p> +{% endif %} {% endblock %} diff --git a/ishtar_common/utils_secretary.py b/ishtar_common/utils_secretary.py index b66ae4760..2266c38c3 100644 --- a/ishtar_common/utils_secretary.py +++ b/ishtar_common/utils_secretary.py @@ -1,7 +1,11 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from secretary import Renderer +from secretary import Renderer, parseString + +from xml.parsers.expat import ExpatError, ErrorString + + from datetime import datetime import locale @@ -73,5 +77,45 @@ class IshtarSecretaryRenderer(Renderer): ) return image_file, mime + def _render_xml(self, xml_document, **kwargs): + # Prepare the xml object to be processed by jinja2 + self.log.debug('Rendering XML object') + template_string = "" + + try: + self.template_images = dict() + self._prepare_document_tags(xml_document) + xml_source = xml_document.toxml() + xml_source = xml_source.encode('ascii', 'xmlcharrefreplace') + jinja_template = self.environment.from_string( + self._unescape_entities(xml_source.decode('utf-8')) + ) + + result = jinja_template.render(**kwargs) + result = self._encode_escape_chars(result) + + final_xml = parseString(result.encode('ascii', 'xmlcharrefreplace')) + if self.template_images: + self.replace_images(final_xml) + + return final_xml + except ExpatError as e: + if not 'result' in locals(): + result = xml_source + ### changes + try: + near = result.split('\n')[e.lineno -1][e.offset-200:e.offset+200] + except IndexError: + near = "..." + ### endchanges + raise ExpatError('ExpatError "%s" at line %d, column %d\nNear of: "[...]%s[...]"' % \ + (ErrorString(e.code), e.lineno, e.offset, near)) + except: + self.log.error('Error rendering template:\n%s', + xml_document.toprettyxml(), exc_info=True) + self.log.error('Unescaped template was:\n{0}'.format(template_string)) + raise + finally: + self.log.debug('Rendering xml object finished') diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 5a3f90da6..2af082a0a 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -1950,7 +1950,8 @@ def gen_generate_doc(model): except TemplateSyntaxError as e: dct = { "error_title": _("Error on your template"), - "error": str(e) + "error": str(e), + "back_url": reverse("display-item", args=[item.SLUG, pk]) } template = loader.get_template("error.html") return HttpResponse(template.render(dct, request)) |