diff options
-rw-r--r-- | archaeological_files/templates/ishtar/administrativeact_document.html | 3 | ||||
-rw-r--r-- | archaeological_operations/views.py | 28 | ||||
-rw-r--r-- | ishtar_common/models.py | 6 |
3 files changed, 30 insertions, 7 deletions
diff --git a/archaeological_files/templates/ishtar/administrativeact_document.html b/archaeological_files/templates/ishtar/administrativeact_document.html index f8ab213fe..c040ca300 100644 --- a/archaeological_files/templates/ishtar/administrativeact_document.html +++ b/archaeological_files/templates/ishtar/administrativeact_document.html @@ -6,9 +6,10 @@ {% endblock %} {% block content %} -{% if template_form.non_field_errors %} +{% if template_form.non_field_errors or template_error %} <div class="alert alert-warning alert-dismissible fade show" role="alert"> {{template_form.non_field_errors}} + {% if template_error %}{{template_error}}{% endif %} <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> </button> diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py index 858043c82..48d7c4a4f 100644 --- a/archaeological_operations/views.py +++ b/archaeological_operations/views.py @@ -18,6 +18,7 @@ # See the file COPYING for details. import json +from jinja2 import TemplateSyntaxError from django.conf import settings from django.core.urlresolvers import reverse @@ -48,7 +49,8 @@ from archaeological_operations.wizards import has_associated_file, \ SiteDeletionWizard, OperationSearch from ishtar_common.forms import ClosingDateFormSelection, FinalForm, \ FinalDeleteForm -from ishtar_common.models import get_current_profile, IshtarSiteProfile +from ishtar_common.models import get_current_profile, IshtarSiteProfile, \ + DocumentTemplate from ishtar_common.utils import put_session_message, check_rights_condition from ishtar_common.views_item import get_item, show_item, revert_item, new_item from ishtar_common.wizards import SearchWizard @@ -530,10 +532,26 @@ def administrativeactfile_document( dct['template_form'] = DocumentGenerationAdminActForm( request.POST, document_type=document_type, obj=c_object) if dct['template_form'].is_valid(): - return generatedoc_administrativeactop( - request, - dct['search_form'].cleaned_data.get('pk'), - dct['template_form'].cleaned_data.get('document_template')) + try: + return generatedoc_administrativeactop( + request, + dct['search_form'].cleaned_data.get('pk'), + dct['template_form'].cleaned_data.get( + 'document_template')) + except TemplateSyntaxError: + dct['search_form'] = search_form() + try: + template = DocumentTemplate.objects.get(pk=dct[ + 'template_form'].cleaned_data.get( + 'document_template')).name + except DocumentTemplate.DoesNotExist: + template = "" + dct['template_form'] = DocumentGenerationAdminActForm( + document_type=document_type) + dct['template_error'] = unicode(_( + u"Syntax error on the source template \"{}\" - " + u"contact your administrator and ask him to check " + u"the syntax of this document.")).format(template) else: dct['search_form'] = search_form() dct['template_form'] = DocumentGenerationAdminActForm( diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 21216163a..2f637cabe 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -23,6 +23,7 @@ Models description import copy import datetime import inspect +from jinja2 import TemplateSyntaxError import logging import os import re @@ -2318,7 +2319,10 @@ class DocumentTemplate(models.Model): u"." + self.template.name.split('.')[-1] values = c_object.get_values() engine = SecretaryRenderer() - result = engine.render(self.template, **values) + try: + result = engine.render(self.template, **values) + except TemplateSyntaxError as e: + raise TemplateSyntaxError(e.message, e.lineno) output = open(output_name, 'wb') output.write(result) return output_name |