diff --git a/InvenTree/common/models.py b/InvenTree/common/models.py index a81d06ed25..9e6c6e868d 100644 --- a/InvenTree/common/models.py +++ b/InvenTree/common/models.py @@ -174,6 +174,24 @@ class InvenTreeSetting(models.Model): 'validator': bool, }, + 'REPORT_DEBUG_MODE': { + 'name': _('Debug Mode'), + 'description': _('Generate reports in debug mode (HTML output)'), + 'default': False, + 'validator': bool, + }, + + 'REPORT_DEFAULT_PAGE_SIZE': { + 'name': _('Page Size'), + 'description': _('Default page size for PDF reports'), + 'default': 'A4', + 'choices': [ + ('A4', 'A4'), + ('Legal', 'Legal'), + ('Letter', 'Letter') + ], + }, + 'REPORT_ENABLE_TEST_REPORT': { 'name': _('Test Reports'), 'description': _('Enable generation of test reports'), diff --git a/InvenTree/report/api.py b/InvenTree/report/api.py index aaffc47a9c..560cf706de 100644 --- a/InvenTree/report/api.py +++ b/InvenTree/report/api.py @@ -3,12 +3,14 @@ from __future__ import unicode_literals from django.utils.translation import ugettext as _ from django.conf.urls import url, include +from django.http import HttpResponse from django_filters.rest_framework import DjangoFilterBackend from rest_framework import generics, filters from rest_framework.response import Response +import common.models import InvenTree.helpers from stock.models import StockItem @@ -165,31 +167,53 @@ class StockItemTestReportPrint(generics.RetrieveAPIView, StockItemReportMixin): outputs = [] + # In debug mode, generate single HTML output, rather than PDF + debug_mode = common.models.InvenTreeSetting.get_setting('REPORT_DEBUG_MODE') + # Merge one or more PDF files into a single download for item in items: report = self.get_object() report.stock_item = item - outputs.append(report.render(request)) + if debug_mode: + outputs.append(report.render_to_string(request)) + else: + outputs.append(report.render(request)) - pages = [] + if debug_mode: + """ + Contatenate all rendered templates into a single HTML string, + and return the string as a HTML response. + """ - 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) + html = "\n".join(outputs) + + return HttpResponse(html) - pdf = outputs[0].get_document().copy(pages).write_pdf() else: - pdf = outputs[0].get_document().write_pdf() + """ + Concatenate all rendered pages into a single PDF object, + and return the resulting document! + """ - return InvenTree.helpers.DownloadFile( - pdf, - 'test_report.pdf', - content_type='application/pdf' - ) + pages = [] + + 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) + + pdf = outputs[0].get_document().copy(pages).write_pdf() + else: + pdf = outputs[0].get_document().write_pdf() + + return InvenTree.helpers.DownloadFile( + pdf, + 'test_report.pdf', + content_type='application/pdf' + ) report_api_urls = [ diff --git a/InvenTree/report/models.py b/InvenTree/report/models.py index 0c77f4d19a..ddb7e9ea94 100644 --- a/InvenTree/report/models.py +++ b/InvenTree/report/models.py @@ -14,10 +14,13 @@ import datetime from django.db import models from django.conf import settings +from django.template.loader import render_to_string + from django.core.files.storage import FileSystemStorage from django.core.validators import FileExtensionValidator import stock.models +import common.models from InvenTree.helpers import validateFilterString @@ -174,6 +177,33 @@ class ReportTemplateBase(ReportBase): return {} + def context(self, request): + """ + All context to be passed to the renderer. + """ + + context = self.get_context_data(request) + + context['date'] = datetime.datetime.now().date() + context['datetime'] = datetime.datetime.now() + context['default_page_size'] = common.models.InvenTreeSetting.get_setting('REPORT_DEFAULT_PAGE_SIZE') + context['report_description'] = self.description + context['report_name'] = self.name + context['report_revision'] = self.revision + context['request'] = request + context['user'] = request.user + + return context + + def render_to_string(self, request, **kwargs): + """ + Render the report to a HTML stiring. + + Useful for debug mode (viewing generated code) + """ + + return render_to_string(self.template_name, self.context(request), request) + def render(self, request, **kwargs): """ Render the template to a PDF file. @@ -184,18 +214,6 @@ class ReportTemplateBase(ReportBase): # TODO: Support custom filename generation! # filename = kwargs.get('filename', 'report.pdf') - context = self.get_context_data(request) - - context['media'] = settings.MEDIA_ROOT - - context['report_name'] = self.name - context['report_description'] = self.description - context['report_revision'] = self.revision - context['request'] = request - context['user'] = request.user - context['date'] = datetime.datetime.now().date() - context['datetime'] = datetime.datetime.now() - # Render HTML template to PDF wp = WeasyprintReportMixin( request, @@ -205,7 +223,7 @@ class ReportTemplateBase(ReportBase): **kwargs) return wp.render_to_response( - context, + self.context(request), **kwargs) enabled = models.BooleanField( diff --git a/InvenTree/report/templates/report/inventree_report_base.html b/InvenTree/report/templates/report/inventree_report_base.html index 084b100cab..7c2cff8b4a 100644 --- a/InvenTree/report/templates/report/inventree_report_base.html +++ b/InvenTree/report/templates/report/inventree_report_base.html @@ -3,14 +3,12 @@