summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2019-05-23 17:52:22 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2019-06-17 13:21:28 +0200
commit20dcdaa6996a2f109765c2f2ae00a3aea6683b49 (patch)
tree36cbe78806ca99e2dc0c6fa258bb1cec06223b3e
parent7d80c4bb2fae21c4b962913480ae84df5955fcb1 (diff)
downloadIshtar-20dcdaa6996a2f109765c2f2ae00a3aea6683b49.tar.bz2
Ishtar-20dcdaa6996a2f109765c2f2ae00a3aea6683b49.zip
Document generation: display an error page for template error
-rw-r--r--ishtar_common/models.py4
-rw-r--r--ishtar_common/templates/500.html2
-rw-r--r--ishtar_common/templates/error.html6
-rw-r--r--ishtar_common/views.py9
4 files changed, 20 insertions, 1 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 8e5983a45..032004e2f 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -25,7 +25,7 @@ from collections import OrderedDict
import datetime
import inspect
from importlib import import_module
-from jinja2 import TemplateSyntaxError
+from jinja2 import TemplateSyntaxError, UndefinedError
import json
import logging
import os
@@ -3201,6 +3201,8 @@ class DocumentTemplate(models.Model):
result = engine.render(self.template, **values)
except TemplateSyntaxError as e:
raise TemplateSyntaxError(str(e), e.lineno)
+ except UndefinedError as e:
+ raise TemplateSyntaxError(str(e), 0)
output = open(output_name, 'wb')
output.write(result)
return output_name
diff --git a/ishtar_common/templates/500.html b/ishtar_common/templates/500.html
index f48c2393d..9815a17f4 100644
--- a/ishtar_common/templates/500.html
+++ b/ishtar_common/templates/500.html
@@ -19,8 +19,10 @@
</header>
<div class="container">
{% block content %}
+ {% block error %}
<h3>{% trans "An error has occured. The support team has been warned." %}</h3>
{% endblock %}
+ {% endblock %}
<p><a href='/'>{% trans "Back to main page" %}</a></p>
</div>
<div id="footer">
diff --git a/ishtar_common/templates/error.html b/ishtar_common/templates/error.html
new file mode 100644
index 000000000..069796184
--- /dev/null
+++ b/ishtar_common/templates/error.html
@@ -0,0 +1,6 @@
+{% extends "500.html" %}
+{% load i18n %}
+{% block error %}
+<h3>{{error_title}}</h3>
+<p>{{error}}</p>
+{% endblock %}
diff --git a/ishtar_common/views.py b/ishtar_common/views.py
index a10c44c77..98c538c66 100644
--- a/ishtar_common/views.py
+++ b/ishtar_common/views.py
@@ -20,6 +20,7 @@
import csv
import datetime
import importlib
+from jinja2 import TemplateSyntaxError
import json
import logging
import os
@@ -34,6 +35,7 @@ from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse, NoReverseMatch
from django.db.models import Q
+from django.template import loader
from django.forms.models import modelformset_factory
from django.http import HttpResponse, Http404, HttpResponseRedirect, \
HttpResponseBadRequest, JsonResponse
@@ -1892,6 +1894,13 @@ def gen_generate_doc(model):
doc = item.publish(template_pk)
except model.DoesNotExist:
doc = None
+ except TemplateSyntaxError as e:
+ dct = {
+ "error_title": _("Error on your template"),
+ "error": str(e)
+ }
+ template = loader.get_template("error.html")
+ return HttpResponse(template.render(dct, request))
if doc:
MIMES = {'odt': 'application/vnd.oasis.opendocument.text',
'ods': 'application/vnd.oasis.opendocument.spreadsheet'}