Handle missing report template

This commit is contained in:
Oliver 2021-10-30 13:28:08 +11:00
parent a689b77cfb
commit 79434bb4e0

View File

@ -1,11 +1,14 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
from django.core import exceptions
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.conf.urls import url, include from django.conf.urls import url, include
from django.core.exceptions import ValidationError, FieldError from django.core.exceptions import ValidationError, FieldError
from django.http import HttpResponse from django.http import HttpResponse
from django.template.exceptions import TemplateDoesNotExist
from django_filters.rest_framework import DjangoFilterBackend from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import generics, filters from rest_framework import generics, filters
@ -218,10 +221,21 @@ class ReportPrintMixin:
report_name = report.generate_filename(request) report_name = report.generate_filename(request)
if debug_mode: try:
outputs.append(report.render_as_string(request)) if debug_mode:
else: outputs.append(report.render_as_string(request))
outputs.append(report.render(request)) else:
outputs.append(report.render(request))
except TemplateDoesNotExist:
filename = report.template
return Response(
{
'error': _(f"Template file '{filename}' is missing or does not exist"),
},
status=400,
)
if not report_name.endswith('.pdf'): if not report_name.endswith('.pdf'):
report_name += '.pdf' report_name += '.pdf'
@ -243,16 +257,28 @@ class ReportPrintMixin:
pages = [] pages = []
if len(outputs) > 1: try:
# If more than one output is generated, merge them into a single file
for output in outputs:
doc = output.get_document()
for page in doc.pages:
pages.append(page)
pdf = outputs[0].get_document().copy(pages).write_pdf() pdf = outputs[0].get_document().copy(pages).write_pdf()
else:
pdf = outputs[0].get_document().write_pdf() if len(outputs) > 1:
# If more than one output is generated, merge them into a single file
for output in outputs:
doc = output.get_document()
for page in doc.pages:
pages.append(page)
else:
pdf = outputs[0].get_document().write_pdf()
except TemplateDoesNotExist:
filename = report.template
return Response(
{
'error': _(f"Template file '{filename}' is missing or does not exist"),
},
status=400,
)
inline = common.models.InvenTreeUserSetting.get_setting('REPORT_INLINE', user=request.user) inline = common.models.InvenTreeUserSetting.get_setting('REPORT_INLINE', user=request.user)