From 70c5b27d2243c912b22dd00c04e87446fe037e5c Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 21 May 2020 14:05:25 +1000 Subject: [PATCH] Add ReportAsset model - Files which can be embedded into a report --- InvenTree/InvenTree/settings.py | 52 +++++++++---------- InvenTree/report/admin.py | 8 ++- .../report/migrations/0003_reportasset.py | 22 ++++++++ InvenTree/report/models.py | 28 +++++++++- .../stock/templates/stock/item_base.html | 2 + 5 files changed, 84 insertions(+), 28 deletions(-) create mode 100644 InvenTree/report/migrations/0003_reportasset.py diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 3d87527216..6cadb09150 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -72,6 +72,27 @@ if DEBUG: format='%(asctime)s %(levelname)s %(message)s', ) +# Web URL endpoint for served static files +STATIC_URL = '/static/' + +# The filesystem location for served static files +STATIC_ROOT = os.path.abspath(CONFIG.get('static_root', os.path.join(BASE_DIR, 'static'))) + +STATICFILES_DIRS = [ + os.path.join(BASE_DIR, 'InvenTree', 'static'), +] + +# Web URL endpoint for served media files +MEDIA_URL = '/media/' + +# The filesystem location for served static files +MEDIA_ROOT = os.path.abspath(CONFIG.get('media_root', os.path.join(BASE_DIR, 'media'))) + +if DEBUG: + print("InvenTree running in DEBUG mode") + print("MEDIA_ROOT:", MEDIA_ROOT) + print("STATIC_ROOT:", STATIC_ROOT) + # Does the user wish to use the sentry.io integration? sentry_opts = CONFIG.get('sentry', {}) @@ -161,7 +182,11 @@ ROOT_URLCONF = 'InvenTree.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [os.path.join(BASE_DIR, 'templates')], + 'DIRS': [ + os.path.join(BASE_DIR, 'templates'), + # Allow templates in the reporting directory to be accessed + os.path.join(MEDIA_ROOT, 'report'), + ], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ @@ -316,31 +341,6 @@ DATE_INPUT_FORMATS = [ "%Y-%m-%d", ] - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/1.10/howto/static-files/ - -# Web URL endpoint for served static files -STATIC_URL = '/static/' - -# The filesystem location for served static files -STATIC_ROOT = os.path.abspath(CONFIG.get('static_root', os.path.join(BASE_DIR, 'static'))) - -STATICFILES_DIRS = [ - os.path.join(BASE_DIR, 'InvenTree', 'static'), -] - -# Web URL endpoint for served media files -MEDIA_URL = '/media/' - -# The filesystem location for served static files -MEDIA_ROOT = os.path.abspath(CONFIG.get('media_root', os.path.join(BASE_DIR, 'media'))) - -if DEBUG: - print("InvenTree running in DEBUG mode") - print("MEDIA_ROOT:", MEDIA_ROOT) - print("STATIC_ROOT:", STATIC_ROOT) - # crispy forms use the bootstrap templates CRISPY_TEMPLATE_PACK = 'bootstrap3' diff --git a/InvenTree/report/admin.py b/InvenTree/report/admin.py index 1d5d1fee01..fc4c914358 100644 --- a/InvenTree/report/admin.py +++ b/InvenTree/report/admin.py @@ -3,7 +3,7 @@ from __future__ import unicode_literals from django.contrib import admin -from .models import ReportTemplate +from .models import ReportTemplate, ReportAsset class ReportTemplateAdmin(admin.ModelAdmin): @@ -11,4 +11,10 @@ class ReportTemplateAdmin(admin.ModelAdmin): list_display = ('template', 'description') +class ReportAssetAdmin(admin.ModelAdmin): + + list_display = ('asset', 'description') + + admin.site.register(ReportTemplate, ReportTemplateAdmin) +admin.site.register(ReportAsset, ReportAssetAdmin) diff --git a/InvenTree/report/migrations/0003_reportasset.py b/InvenTree/report/migrations/0003_reportasset.py new file mode 100644 index 0000000000..6f9c8efd03 --- /dev/null +++ b/InvenTree/report/migrations/0003_reportasset.py @@ -0,0 +1,22 @@ +# Generated by Django 3.0.5 on 2020-05-21 04:04 + +from django.db import migrations, models +import report.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('report', '0002_auto_20200521_0350'), + ] + + operations = [ + migrations.CreateModel( + name='ReportAsset', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('asset', models.FileField(help_text='Report asset file', upload_to=report.models.rename_asset)), + ('description', models.CharField(help_text='Asset file description', max_length=250)), + ], + ), + ] diff --git a/InvenTree/report/models.py b/InvenTree/report/models.py index 2658a28f4d..eeebbfbee9 100644 --- a/InvenTree/report/models.py +++ b/InvenTree/report/models.py @@ -17,7 +17,7 @@ def rename_template(instance, filename): filename = os.path.basename(filename) - return os.path.join('report', 'template', filename) + return os.path.join('report', 'report_template', filename) class ReportTemplate(models.Model): @@ -35,3 +35,29 @@ class ReportTemplate(models.Model): ) description = models.CharField(max_length=250, help_text=_("Report template description")) + + +def rename_asset(instance, filename): + + filename = os.path.basename(filename) + + return os.path.join('report', 'assets', filename) + + +class ReportAsset(models.Model): + """ + Asset file for use in report templates. + For example, an image to use in a header file. + Uploaded asset files appear in MEDIA_ROOT/report/assets, + and can be loaded in a template using the {% report_asset %} tag. + """ + + def __str__(self): + return os.path.basename(self.asset.name) + + asset = models.FileField( + upload_to=rename_asset, + help_text=_("Report asset file"), + ) + + description = models.CharField(max_length=250, help_text=_("Asset file description")) diff --git a/InvenTree/stock/templates/stock/item_base.html b/InvenTree/stock/templates/stock/item_base.html index 782e51a2b8..4a12faff5e 100644 --- a/InvenTree/stock/templates/stock/item_base.html +++ b/InvenTree/stock/templates/stock/item_base.html @@ -15,6 +15,8 @@ InvenTree | {% trans "Stock Item" %} - {{ item }} {% block pre_content %} {% include 'stock/loc_link.html' with location=item.location %} +{% include "report_template/test.html" with item=item %} + {% if item.hasRequiredTests and not item.passedAllRequiredTests %}
{% trans "This stock item has not passed all required tests" %}