Copy default test report across

This commit is contained in:
Oliver Walters 2021-02-04 20:25:01 +11:00
parent 98d291c2f8
commit bc36775270
3 changed files with 99 additions and 11 deletions

View File

@ -1,5 +1,92 @@
import os
import shutil
import logging
from django.apps import AppConfig
from django.conf import settings
logger = logging.getLogger(__name__)
class ReportConfig(AppConfig):
name = 'report'
def ready(self):
"""
This function is called whenever the report app is loaded
"""
self.create_default_test_reports()
def create_default_test_reports(self):
"""
Create database entries for the default TestReport templates,
if they do not already exist
"""
try:
from .models import TestReport
except:
# Database is not ready yet
return
src_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'templates',
'report',
)
dst_dir = os.path.join(
settings.MEDIA_ROOT,
'report',
'inventree', # Stored in secret directory!
'test',
)
if not os.path.exists(dst_dir):
logger.info(f"Creating missing directory: '{dst_dir}'")
os.makedirs(dst_dir, exist_ok=True)
# List of test reports to copy across
reports = [
{
'file': 'inventree_test_report.html',
'name': 'InvenTree Test Report',
'description': 'Stock item test report',
},
]
for report in reports:
# Create destination file name
filename = os.path.join(
'report',
'inventree',
'test',
report['file']
)
src_file = os.path.join(src_dir, report['file'])
dst_file = os.path.join(settings.MEDIA_ROOT, filename)
if not os.path.exists(dst_file):
logger.info(f"Copying test report template '{dst_file}'")
shutil.copyfile(src_file, dst_file)
try:
# Check if a report matching the template already exists
if TestReport.objects.filter(template=filename).exists():
continue
logger.info(f"Creating new TestReport for '{report['name']}'")
TestReport.objects.create(
name=report['name'],
description=report['description'],
template=filename,
filters='',
enabled=True
)
except:
pass

View File

@ -31,13 +31,6 @@ except OSError as err:
from django.http import HttpResponse
class TexResponse(HttpResponse):
def __init__(self, content, filename=None):
super().__init__(content_type="application/txt")
self["Content-Disposition"] = 'filename="{}"'.format(filename)
self.write(content)
def rename_template(instance, filename):
return instance.rename_file(filename)
@ -95,10 +88,12 @@ class ReportBase(models.Model):
Required for passing the file to an external process
"""
template = os.path.join('report_template', self.getSubdir(), os.path.basename(self.template.name))
template = self.template.name
template = template.replace('/', os.path.sep)
template = template.replace('\\', os.path.sep)
template = os.path.join(settings.MEDIA_ROOT, template)
return template
name = models.CharField(

View File

@ -57,15 +57,21 @@ content: "{% trans 'Stock Item Test Report' %}";
<div class='container'>
<div class='text-left'>
<h2>
{{ stock_item.part.full_name }}
{{ part.full_name }}
</h2>
<p>{{ part.description }}</p>
<p><i>{{ stock_item.location }}</i></p>
</div>
<div class='img-right'>
<img src="{% part_image part %}">
<hr>
<h3>
<h4>
{% if stock_item.is_serialized %}
{% trans "Serial Number" %}: {{ stock_item.serial }}
</h3>
{% else %}
{% trans "Quantity" %}: {% decimal stock_item.quantity %}
{% endif %}
</h4>
</div>
</div>