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
|
2021-02-10 09:40:15 +00:00
|
|
|
from django.core.exceptions import ValidationError, FieldError
|
2020-08-15 23:28:12 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-01-09 10:50:42 +00:00
|
|
|
import stock.models
|
2020-08-15 23:28:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2021-01-13 21:15:05 +00:00
|
|
|
def validate_stock_item_filters(filters):
|
|
|
|
|
|
|
|
filters = validateFilterString(filters, model=stock.models.StockItem)
|
|
|
|
|
|
|
|
return filters
|
|
|
|
|
|
|
|
|
|
|
|
def validate_stock_location_filters(filters):
|
|
|
|
|
|
|
|
filters = validateFilterString(filters, model=stock.models.StockLocation)
|
|
|
|
|
|
|
|
return filters
|
|
|
|
|
|
|
|
|
2020-08-15 23:28:12 +00:00
|
|
|
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(
|
|
|
|
blank=False, max_length=100,
|
2021-01-11 12:44:11 +00:00
|
|
|
verbose_name=_('Name'),
|
2020-08-15 23:28:12 +00:00
|
|
|
help_text=_('Label name'),
|
|
|
|
)
|
|
|
|
|
2021-01-08 11:47:47 +00:00
|
|
|
description = models.CharField(
|
|
|
|
max_length=250,
|
|
|
|
blank=True, null=True,
|
2021-01-11 12:44:11 +00:00
|
|
|
verbose_name=_('Description'),
|
2021-01-08 11:47:47 +00:00
|
|
|
help_text=_('Label description'),
|
|
|
|
)
|
2020-08-15 23:28:12 +00:00
|
|
|
|
|
|
|
label = models.FileField(
|
|
|
|
upload_to=rename_label,
|
2021-01-11 12:44:11 +00:00
|
|
|
unique=True,
|
2020-08-15 23:28:12 +00:00
|
|
|
blank=False, null=False,
|
2021-01-11 12:44:11 +00:00
|
|
|
verbose_name=_('Label'),
|
2020-08-15 23:28:12 +00:00
|
|
|
help_text=_('Label template file'),
|
|
|
|
validators=[FileExtensionValidator(allowed_extensions=['html'])],
|
|
|
|
)
|
|
|
|
|
2020-08-22 23:07:15 +00:00
|
|
|
enabled = models.BooleanField(
|
|
|
|
default=True,
|
2021-01-11 12:44:11 +00:00
|
|
|
verbose_name=_('Enabled'),
|
2020-08-22 23:07:15 +00:00
|
|
|
help_text=_('Label template is enabled'),
|
|
|
|
)
|
|
|
|
|
2020-08-15 23:28:12 +00:00
|
|
|
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"
|
|
|
|
|
2021-01-13 21:15:05 +00:00
|
|
|
filters = models.CharField(
|
|
|
|
blank=True, max_length=250,
|
|
|
|
help_text=_('Query filters (comma-separated list of key=value pairs'),
|
|
|
|
verbose_name=_('Filters'),
|
|
|
|
validators=[
|
|
|
|
validate_stock_item_filters]
|
|
|
|
)
|
|
|
|
|
2020-08-15 23:28:12 +00:00
|
|
|
def matches_stock_item(self, item):
|
|
|
|
"""
|
|
|
|
Test if this label template matches a given StockItem object
|
|
|
|
"""
|
|
|
|
|
2021-02-10 09:40:15 +00:00
|
|
|
try:
|
|
|
|
filters = validateFilterString(self.filters)
|
|
|
|
items = stock.models.StockItem.objects.filter(**filters)
|
|
|
|
except (ValidationError, FieldError):
|
|
|
|
# If an error exists with the "filters" field, return False
|
|
|
|
return False
|
2020-08-15 23:28:12 +00:00
|
|
|
|
|
|
|
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
|
2021-01-08 12:08:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class StockLocationLabel(LabelTemplate):
|
|
|
|
"""
|
|
|
|
Template for printing StockLocation labels
|
|
|
|
"""
|
|
|
|
|
|
|
|
SUBDIR = "stocklocation"
|
|
|
|
|
2021-01-13 21:15:05 +00:00
|
|
|
filters = models.CharField(
|
|
|
|
blank=True, max_length=250,
|
|
|
|
help_text=_('Query filters (comma-separated list of key=value pairs'),
|
|
|
|
verbose_name=_('Filters'),
|
|
|
|
validators=[
|
|
|
|
validate_stock_location_filters]
|
|
|
|
)
|
|
|
|
|
2021-01-08 12:08:00 +00:00
|
|
|
def matches_stock_location(self, location):
|
|
|
|
"""
|
|
|
|
Test if this label template matches a given StockLocation object
|
|
|
|
"""
|
|
|
|
|
2021-02-10 09:40:15 +00:00
|
|
|
try:
|
|
|
|
filters = validateFilterString(self.filters)
|
|
|
|
locs = stock.models.StockLocation.objects.filter(**filters)
|
|
|
|
except (ValidationError, FieldError):
|
|
|
|
return False
|
2021-01-08 12:08:00 +00:00
|
|
|
|
|
|
|
locs = locs.filter(pk=location.pk)
|
|
|
|
|
|
|
|
return locs.exists()
|
|
|
|
|
|
|
|
def get_record_data(self, locations):
|
|
|
|
"""
|
|
|
|
Generate context data for each provided StockLocation
|
|
|
|
"""
|
|
|
|
|
|
|
|
records = []
|
|
|
|
|
|
|
|
for loc in locations:
|
|
|
|
|
|
|
|
records.append({
|
2021-01-09 10:55:05 +00:00
|
|
|
'location': loc,
|
2021-01-08 12:08:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return records
|