From 4fcafb2275f1f4fe366fb4ac5e162e938ff65a75 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 28 Sep 2018 17:42:02 +0200 Subject: Catch template syntax error on documet generation --- archaeological_operations/views.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'archaeological_operations/views.py') 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( -- cgit v1.2.3