mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add 'REPORT_DEBUG_MODE' setting
- If set, reports are printed in raw HTML - Not pretty, but useful for debugging output of rendered template
This commit is contained in:
parent
dbaa0fc300
commit
30d95e1511
@ -174,6 +174,13 @@ class InvenTreeSetting(models.Model):
|
|||||||
'validator': bool,
|
'validator': bool,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
'REPORT_DEBUG_MODE': {
|
||||||
|
'name': _('Debug Mode'),
|
||||||
|
'description': _('Generate reports in debug mode (HTML output)'),
|
||||||
|
'default': False,
|
||||||
|
'validator': bool,
|
||||||
|
},
|
||||||
|
|
||||||
'REPORT_DEFAULT_PAGE_SIZE': {
|
'REPORT_DEFAULT_PAGE_SIZE': {
|
||||||
'name': _('Page Size'),
|
'name': _('Page Size'),
|
||||||
'description': _('Default page size for PDF reports'),
|
'description': _('Default page size for PDF reports'),
|
||||||
|
@ -3,12 +3,14 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
from django.utils.translation import ugettext as _
|
from django.utils.translation import ugettext as _
|
||||||
from django.conf.urls import url, include
|
from django.conf.urls import url, include
|
||||||
|
from django.http import HttpResponse
|
||||||
|
|
||||||
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
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
|
||||||
|
import common.models
|
||||||
import InvenTree.helpers
|
import InvenTree.helpers
|
||||||
|
|
||||||
from stock.models import StockItem
|
from stock.models import StockItem
|
||||||
@ -165,13 +167,36 @@ class StockItemTestReportPrint(generics.RetrieveAPIView, StockItemReportMixin):
|
|||||||
|
|
||||||
outputs = []
|
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
|
# Merge one or more PDF files into a single download
|
||||||
for item in items:
|
for item in items:
|
||||||
report = self.get_object()
|
report = self.get_object()
|
||||||
report.stock_item = item
|
report.stock_item = item
|
||||||
|
|
||||||
|
if debug_mode:
|
||||||
|
outputs.append(report.render_to_string(request))
|
||||||
|
else:
|
||||||
outputs.append(report.render(request))
|
outputs.append(report.render(request))
|
||||||
|
|
||||||
|
|
||||||
|
if debug_mode:
|
||||||
|
"""
|
||||||
|
Contatenate all rendered templates into a single HTML string,
|
||||||
|
and return the string as a HTML response.
|
||||||
|
"""
|
||||||
|
|
||||||
|
html = "\n".join(outputs)
|
||||||
|
|
||||||
|
return HttpResponse(html)
|
||||||
|
|
||||||
|
else:
|
||||||
|
"""
|
||||||
|
Concatenate all rendered pages into a single PDF object,
|
||||||
|
and return the resulting document!
|
||||||
|
"""
|
||||||
|
|
||||||
pages = []
|
pages = []
|
||||||
|
|
||||||
if len(outputs) > 1:
|
if len(outputs) > 1:
|
||||||
|
@ -14,6 +14,8 @@ import datetime
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
|
from django.template.loader import render_to_string
|
||||||
|
|
||||||
from django.core.files.storage import FileSystemStorage
|
from django.core.files.storage import FileSystemStorage
|
||||||
from django.core.validators import FileExtensionValidator
|
from django.core.validators import FileExtensionValidator
|
||||||
|
|
||||||
@ -175,16 +177,11 @@ class ReportTemplateBase(ReportBase):
|
|||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
def render(self, request, **kwargs):
|
def context(self, request):
|
||||||
"""
|
"""
|
||||||
Render the template to a PDF file.
|
All context to be passed to the renderer.
|
||||||
|
|
||||||
Uses django-weasyprint plugin to render HTML template against Weasyprint
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# TODO: Support custom filename generation!
|
|
||||||
# filename = kwargs.get('filename', 'report.pdf')
|
|
||||||
|
|
||||||
context = self.get_context_data(request)
|
context = self.get_context_data(request)
|
||||||
|
|
||||||
context['date'] = datetime.datetime.now().date()
|
context['date'] = datetime.datetime.now().date()
|
||||||
@ -196,6 +193,27 @@ class ReportTemplateBase(ReportBase):
|
|||||||
context['request'] = request
|
context['request'] = request
|
||||||
context['user'] = request.user
|
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.
|
||||||
|
|
||||||
|
Uses django-weasyprint plugin to render HTML template against Weasyprint
|
||||||
|
"""
|
||||||
|
|
||||||
|
# TODO: Support custom filename generation!
|
||||||
|
# filename = kwargs.get('filename', 'report.pdf')
|
||||||
|
|
||||||
# Render HTML template to PDF
|
# Render HTML template to PDF
|
||||||
wp = WeasyprintReportMixin(
|
wp = WeasyprintReportMixin(
|
||||||
request,
|
request,
|
||||||
@ -205,7 +223,7 @@ class ReportTemplateBase(ReportBase):
|
|||||||
**kwargs)
|
**kwargs)
|
||||||
|
|
||||||
return wp.render_to_response(
|
return wp.render_to_response(
|
||||||
context,
|
self.context(request),
|
||||||
**kwargs)
|
**kwargs)
|
||||||
|
|
||||||
enabled = models.BooleanField(
|
enabled = models.BooleanField(
|
||||||
|
@ -50,6 +50,10 @@ content: "{% trans 'Stock Item Test Report' %}";
|
|||||||
width: 50%;
|
width: 50%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.part-img {
|
||||||
|
height: 4cm;
|
||||||
|
}
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block page_content %}
|
{% block page_content %}
|
||||||
@ -64,7 +68,7 @@ content: "{% trans 'Stock Item Test Report' %}";
|
|||||||
<p><i>Stock Item ID: {{ stock_item.pk }}</i></p>
|
<p><i>Stock Item ID: {{ stock_item.pk }}</i></p>
|
||||||
</div>
|
</div>
|
||||||
<div class='img-right'>
|
<div class='img-right'>
|
||||||
<img src="{% part_image part %}">
|
<img class='part-img' src="{% part_image part %}">
|
||||||
<hr>
|
<hr>
|
||||||
<h4>
|
<h4>
|
||||||
{% if stock_item.is_serialized %}
|
{% if stock_item.is_serialized %}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
{% include "InvenTree/settings/header.html" %}
|
{% include "InvenTree/settings/header.html" %}
|
||||||
<tbody>
|
<tbody>
|
||||||
{% include "InvenTree/settings/setting.html" with key="REPORT_DEFAULT_PAGE_SIZE" %}
|
{% include "InvenTree/settings/setting.html" with key="REPORT_DEFAULT_PAGE_SIZE" %}
|
||||||
|
{% include "InvenTree/settings/setting.html" with key="REPORT_DEBUG_MODE" %}
|
||||||
{% include "InvenTree/settings/setting.html" with key="REPORT_ENABLE_TEST_REPORT" %}
|
{% include "InvenTree/settings/setting.html" with key="REPORT_ENABLE_TEST_REPORT" %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
Loading…
Reference in New Issue
Block a user