From 82a3d9ca967f80d25e9bf09fe56d79acd81e779f Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Thu, 13 May 2021 13:40:40 +1000 Subject: [PATCH] Implements custom naming for generated labels --- InvenTree/label/api.py | 9 +++++++- .../migrations/0007_auto_20210513_1327.py | 23 +++++++++++++++++++ InvenTree/label/models.py | 22 ++++++++++++++++++ InvenTree/report/models.py | 1 + 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 InvenTree/label/migrations/0007_auto_20210513_1327.py diff --git a/InvenTree/label/api.py b/InvenTree/label/api.py index fc082e4a50..8522857e30 100644 --- a/InvenTree/label/api.py +++ b/InvenTree/label/api.py @@ -63,16 +63,23 @@ class LabelPrintMixin: # In debug mode, generate single HTML output, rather than PDF debug_mode = common.models.InvenTreeSetting.get_setting('REPORT_DEBUG_MODE') + label_name = "label.pdf" + # Merge one or more PDF files into a single download for item in items_to_print: label = self.get_object() label.object_to_print = item + label_name = label.generate_filename(request) + if debug_mode: outputs.append(label.render_as_string(request)) else: outputs.append(label.render(request)) + if not label_name.endswith(".pdf"): + label_name += ".pdf" + if debug_mode: """ Contatenate all rendered templates into a single HTML string, @@ -103,7 +110,7 @@ class LabelPrintMixin: return InvenTree.helpers.DownloadFile( pdf, - 'inventree_label.pdf', + label_name, content_type='application/pdf' ) diff --git a/InvenTree/label/migrations/0007_auto_20210513_1327.py b/InvenTree/label/migrations/0007_auto_20210513_1327.py new file mode 100644 index 0000000000..d49c83c92b --- /dev/null +++ b/InvenTree/label/migrations/0007_auto_20210513_1327.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2 on 2021-05-13 03:27 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('label', '0006_auto_20210222_1535'), + ] + + operations = [ + migrations.AddField( + model_name='stockitemlabel', + name='filename_pattern', + field=models.CharField(default='label.pdf', help_text='Pattern for generating label filenames', max_length=100, verbose_name='Filename Pattern'), + ), + migrations.AddField( + model_name='stocklocationlabel', + name='filename_pattern', + field=models.CharField(default='label.pdf', help_text='Pattern for generating label filenames', max_length=100, verbose_name='Filename Pattern'), + ), + ] diff --git a/InvenTree/label/models.py b/InvenTree/label/models.py index 71ccc73ac9..a5d8314193 100644 --- a/InvenTree/label/models.py +++ b/InvenTree/label/models.py @@ -15,6 +15,7 @@ from django.db import models from django.core.validators import FileExtensionValidator, MinValueValidator from django.core.exceptions import ValidationError, FieldError +from django.template import Template, Context from django.template.loader import render_to_string from django.utils.translation import gettext_lazy as _ @@ -138,6 +139,13 @@ class LabelTemplate(models.Model): validators=[MinValueValidator(2)] ) + filename_pattern = models.CharField( + default="label.pdf", + verbose_name=_('Filename Pattern'), + help_text=_('Pattern for generating label filenames'), + max_length=100, + ) + @property def template_name(self): """ @@ -162,6 +170,19 @@ class LabelTemplate(models.Model): return {} + def generate_filename(self, request, **kwargs): + """ + Generate a filename for this label + """ + + template_string = Template(self.filename_pattern) + + ctx = self.context(request) + + context = Context(ctx) + + return template_string.render(context) + def context(self, request): """ Provides context data to the template. @@ -201,6 +222,7 @@ class LabelTemplate(models.Model): self.template_name, base_url=request.build_absolute_uri("/"), presentational_hints=True, + filename=self.generate_filename(request), **kwargs ) diff --git a/InvenTree/report/models.py b/InvenTree/report/models.py index dfb7c09679..ecb4d91492 100644 --- a/InvenTree/report/models.py +++ b/InvenTree/report/models.py @@ -278,6 +278,7 @@ class ReportTemplateBase(ReportBase): self.template_name, base_url=request.build_absolute_uri("/"), presentational_hints=True, + filename=self.generate_filename(request), **kwargs) return wp.render_to_response(