mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add context data to StockItemLabel model
This commit is contained in:
parent
aefd70ce49
commit
18b3fd3256
@ -14,7 +14,7 @@ from django.core.validators import FileExtensionValidator
|
|||||||
|
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from InvenTree.helpers import validateFilterString
|
from InvenTree.helpers import validateFilterString, normalize
|
||||||
|
|
||||||
from stock.models import StockItem
|
from stock.models import StockItem
|
||||||
|
|
||||||
@ -38,6 +38,10 @@ class LabelTemplate(models.Model):
|
|||||||
# Each class of label files will be stored in a separate subdirectory
|
# Each class of label files will be stored in a separate subdirectory
|
||||||
SUBDIR = "label"
|
SUBDIR = "label"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def template(self):
|
||||||
|
return self.label.path
|
||||||
|
|
||||||
name = models.CharField(
|
name = models.CharField(
|
||||||
unique=True,
|
unique=True,
|
||||||
blank=False, max_length=100,
|
blank=False, max_length=100,
|
||||||
@ -60,6 +64,9 @@ class LabelTemplate(models.Model):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def get_record_data(self, items):
|
def get_record_data(self, items):
|
||||||
|
"""
|
||||||
|
Return a list of dict objects, one for each item.
|
||||||
|
"""
|
||||||
|
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@ -67,9 +74,9 @@ class LabelTemplate(models.Model):
|
|||||||
|
|
||||||
records = self.get_record_data(items)
|
records = self.get_record_data(items)
|
||||||
|
|
||||||
writer = LabelWriter(self.label.filename)
|
writer = LabelWriter(self.template)
|
||||||
|
|
||||||
writer.write_label(records, 'out.pdf')
|
writer.write_labels(records, 'out.html')
|
||||||
|
|
||||||
|
|
||||||
class StockItemLabel(LabelTemplate):
|
class StockItemLabel(LabelTemplate):
|
||||||
@ -91,3 +98,26 @@ class StockItemLabel(LabelTemplate):
|
|||||||
items = items.filter(pk=item.pk)
|
items = items.filter(pk=item.pk)
|
||||||
|
|
||||||
return items.exists()
|
return items.exists()
|
||||||
|
|
||||||
|
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,
|
||||||
|
'qr_data': item.format_short_barcode(),
|
||||||
|
})
|
||||||
|
|
||||||
|
return records
|
||||||
|
@ -302,6 +302,13 @@ class StockItem(MPTTModel):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def format_short_barcode(self):
|
||||||
|
"""
|
||||||
|
Return a short barcode
|
||||||
|
"""
|
||||||
|
|
||||||
|
return "stockid={pk}".format(pk=self.pk)
|
||||||
|
|
||||||
uid = models.CharField(blank=True, max_length=128, help_text=("Unique identifier field"))
|
uid = models.CharField(blank=True, max_length=128, help_text=("Unique identifier field"))
|
||||||
|
|
||||||
parent = TreeForeignKey(
|
parent = TreeForeignKey(
|
||||||
|
Loading…
Reference in New Issue
Block a user