2020-08-15 23:28:12 +00:00
|
|
|
"""
|
|
|
|
Label printing models
|
|
|
|
"""
|
|
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
import os
|
2020-08-16 00:29:03 +00:00
|
|
|
import io
|
2020-08-15 23:28:12 +00:00
|
|
|
|
|
|
|
from blabel import LabelWriter
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.core.validators import FileExtensionValidator
|
|
|
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
2020-08-16 00:24:15 +00:00
|
|
|
from InvenTree.helpers import validateFilterString, normalize
|
2020-08-15 23:28:12 +00:00
|
|
|
|
|
|
|
from stock.models import StockItem
|
|
|
|
|
|
|
|
|
|
|
|
def rename_label(instance, filename):
|
|
|
|
""" Place the label file into the correct subdirectory """
|
|
|
|
|
|
|
|
filename = os.path.basename(filename)
|
|
|
|
|
|
|
|
return os.path.join('label', 'template', instance.SUBDIR, filename)
|
|
|
|
|
|
|
|
|
|
|
|
class LabelTemplate(models.Model):
|
|
|
|
"""
|
|
|
|
Base class for generic, filterable labels.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
abstract = True
|
|
|
|
|
|
|
|
# Each class of label files will be stored in a separate subdirectory
|
|
|
|
SUBDIR = "label"
|
|
|
|
|
2020-08-16 00:24:15 +00:00
|
|
|
@property
|
|
|
|
def template(self):
|
|
|
|
return self.label.path
|
|
|
|
|
2020-08-16 02:10:58 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "{n} - {d}".format(
|
|
|
|
n=self.name,
|
|
|
|
d=self.description
|
|
|
|
)
|
|
|
|
|
2020-08-15 23:28:12 +00:00
|
|
|
name = models.CharField(
|
|
|
|
unique=True,
|
|
|
|
blank=False, max_length=100,
|
|
|
|
help_text=_('Label name'),
|
|
|
|
)
|
|
|
|
|
|
|
|
description = models.CharField(max_length=250, help_text=_('Label description'), blank=True, null=True)
|
|
|
|
|
|
|
|
label = models.FileField(
|
|
|
|
upload_to=rename_label,
|
|
|
|
blank=False, null=False,
|
|
|
|
help_text=_('Label template file'),
|
|
|
|
validators=[FileExtensionValidator(allowed_extensions=['html'])],
|
|
|
|
)
|
|
|
|
|
|
|
|
filters = models.CharField(
|
|
|
|
blank=True, max_length=250,
|
|
|
|
help_text=_('Query filters (comma-separated list of key=value pairs'),
|
|
|
|
validators=[validateFilterString]
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_record_data(self, items):
|
2020-08-16 00:24:15 +00:00
|
|
|
"""
|
|
|
|
Return a list of dict objects, one for each item.
|
|
|
|
"""
|
2020-08-15 23:28:12 +00:00
|
|
|
|
|
|
|
return []
|
|
|
|
|
2020-08-16 00:29:03 +00:00
|
|
|
def render_to_file(self, filename, items, **kwargs):
|
|
|
|
"""
|
|
|
|
Render labels to a PDF file
|
|
|
|
"""
|
|
|
|
|
|
|
|
records = self.get_record_data(items)
|
|
|
|
|
|
|
|
writer = LabelWriter(self.template)
|
|
|
|
|
|
|
|
writer.write_labels(records, filename)
|
|
|
|
|
2020-08-15 23:28:12 +00:00
|
|
|
def render(self, items, **kwargs):
|
2020-08-16 00:29:03 +00:00
|
|
|
"""
|
|
|
|
Render labels to an in-memory PDF object, and return it
|
|
|
|
"""
|
2020-08-15 23:28:12 +00:00
|
|
|
|
|
|
|
records = self.get_record_data(items)
|
|
|
|
|
2020-08-16 00:24:15 +00:00
|
|
|
writer = LabelWriter(self.template)
|
2020-08-15 23:28:12 +00:00
|
|
|
|
2020-08-16 00:29:03 +00:00
|
|
|
buffer = io.BytesIO()
|
|
|
|
|
|
|
|
writer.write_labels(records, buffer)
|
|
|
|
|
|
|
|
return buffer
|
2020-08-15 23:28:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StockItemLabel(LabelTemplate):
|
|
|
|
"""
|
|
|
|
Template for printing StockItem labels
|
|
|
|
"""
|
|
|
|
|
|
|
|
SUBDIR = "stockitem"
|
|
|
|
|
|
|
|
def matches_stock_item(self, item):
|
|
|
|
"""
|
|
|
|
Test if this label template matches a given StockItem object
|
|
|
|
"""
|
|
|
|
|
|
|
|
filters = validateFilterString(self.filters)
|
|
|
|
|
|
|
|
items = StockItem.objects.filter(**filters)
|
|
|
|
|
|
|
|
items = items.filter(pk=item.pk)
|
|
|
|
|
|
|
|
return items.exists()
|
2020-08-16 00:24:15 +00:00
|
|
|
|
|
|
|
def get_record_data(self, items):
|
|
|
|
"""
|
|
|
|
Generate context data for each provided StockItem
|
|
|
|
"""
|
|
|
|
records = []
|
|
|
|
|
|
|
|
for item in items:
|
|
|
|
|
|
|
|
# Add some basic information
|
|
|
|
records.append({
|
|
|
|
'item': item,
|
|
|
|
'part': item.part,
|
|
|
|
'name': item.part.name,
|
|
|
|
'ipn': item.part.IPN,
|
|
|
|
'quantity': normalize(item.quantity),
|
|
|
|
'serial': item.serial,
|
|
|
|
'uid': item.uid,
|
|
|
|
'pk': item.pk,
|
2020-08-16 03:29:38 +00:00
|
|
|
'qr_data': item.format_barcode(brief=True),
|
2020-08-16 02:10:58 +00:00
|
|
|
'tests': item.testResultMap()
|
2020-08-16 00:24:15 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return records
|