mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Implements custom naming for generated labels
This commit is contained in:
parent
6de44a5921
commit
82a3d9ca96
@ -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'
|
||||
)
|
||||
|
||||
|
23
InvenTree/label/migrations/0007_auto_20210513_1327.py
Normal file
23
InvenTree/label/migrations/0007_auto_20210513_1327.py
Normal file
@ -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'),
|
||||
),
|
||||
]
|
@ -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
|
||||
)
|
||||
|
||||
|
@ -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(
|
||||
|
Loading…
Reference in New Issue
Block a user