mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-29 12:06:44 +00:00
Copy default part label templates
This commit is contained in:
parent
1830467487
commit
c39f705ef7
@ -37,6 +37,7 @@ class LabelConfig(AppConfig):
|
|||||||
if canAppAccessDatabase():
|
if canAppAccessDatabase():
|
||||||
self.create_stock_item_labels()
|
self.create_stock_item_labels()
|
||||||
self.create_stock_location_labels()
|
self.create_stock_location_labels()
|
||||||
|
self.create_part_labels()
|
||||||
|
|
||||||
def create_stock_item_labels(self):
|
def create_stock_item_labels(self):
|
||||||
"""
|
"""
|
||||||
@ -65,7 +66,7 @@ class LabelConfig(AppConfig):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not os.path.exists(dst_dir):
|
if not os.path.exists(dst_dir):
|
||||||
logger.info(f"Creating missing directory: '{dst_dir}'")
|
logger.info(f"Creating required directory: '{dst_dir}'")
|
||||||
os.makedirs(dst_dir, exist_ok=True)
|
os.makedirs(dst_dir, exist_ok=True)
|
||||||
|
|
||||||
labels = [
|
labels = [
|
||||||
@ -109,7 +110,6 @@ class LabelConfig(AppConfig):
|
|||||||
logger.info(f"Copying label template '{dst_file}'")
|
logger.info(f"Copying label template '{dst_file}'")
|
||||||
shutil.copyfile(src_file, dst_file)
|
shutil.copyfile(src_file, dst_file)
|
||||||
|
|
||||||
try:
|
|
||||||
# Check if a label matching the template already exists
|
# Check if a label matching the template already exists
|
||||||
if StockItemLabel.objects.filter(label=filename).exists():
|
if StockItemLabel.objects.filter(label=filename).exists():
|
||||||
continue
|
continue
|
||||||
@ -125,8 +125,6 @@ class LabelConfig(AppConfig):
|
|||||||
width=label['width'],
|
width=label['width'],
|
||||||
height=label['height'],
|
height=label['height'],
|
||||||
)
|
)
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def create_stock_location_labels(self):
|
def create_stock_location_labels(self):
|
||||||
"""
|
"""
|
||||||
@ -155,7 +153,7 @@ class LabelConfig(AppConfig):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not os.path.exists(dst_dir):
|
if not os.path.exists(dst_dir):
|
||||||
logger.info(f"Creating missing directory: '{dst_dir}'")
|
logger.info(f"Creating required directory: '{dst_dir}'")
|
||||||
os.makedirs(dst_dir, exist_ok=True)
|
os.makedirs(dst_dir, exist_ok=True)
|
||||||
|
|
||||||
labels = [
|
labels = [
|
||||||
@ -206,7 +204,6 @@ class LabelConfig(AppConfig):
|
|||||||
logger.info(f"Copying label template '{dst_file}'")
|
logger.info(f"Copying label template '{dst_file}'")
|
||||||
shutil.copyfile(src_file, dst_file)
|
shutil.copyfile(src_file, dst_file)
|
||||||
|
|
||||||
try:
|
|
||||||
# Check if a label matching the template already exists
|
# Check if a label matching the template already exists
|
||||||
if StockLocationLabel.objects.filter(label=filename).exists():
|
if StockLocationLabel.objects.filter(label=filename).exists():
|
||||||
continue
|
continue
|
||||||
@ -222,5 +219,88 @@ class LabelConfig(AppConfig):
|
|||||||
width=label['width'],
|
width=label['width'],
|
||||||
height=label['height'],
|
height=label['height'],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def create_part_labels(self):
|
||||||
|
"""
|
||||||
|
Create database entries for the default PartLabel templates,
|
||||||
|
if they do not already exist.
|
||||||
|
"""
|
||||||
|
|
||||||
|
try:
|
||||||
|
from .models import PartLabel
|
||||||
except:
|
except:
|
||||||
pass
|
# Database might not yet be ready
|
||||||
|
return
|
||||||
|
|
||||||
|
src_dir = os.path.join(
|
||||||
|
os.path.dirname(os.path.realpath(__file__)),
|
||||||
|
'templates',
|
||||||
|
'label',
|
||||||
|
'part',
|
||||||
|
)
|
||||||
|
|
||||||
|
dst_dir = os.path.join(
|
||||||
|
settings.MEDIA_ROOT,
|
||||||
|
'label',
|
||||||
|
'inventree',
|
||||||
|
'part',
|
||||||
|
)
|
||||||
|
|
||||||
|
if not os.path.exists(dst_dir):
|
||||||
|
logger.info(f"Creating required directory: '{dst_dir}'")
|
||||||
|
os.makedirs(dst_dir, exist_ok=True)
|
||||||
|
|
||||||
|
labels = [
|
||||||
|
{
|
||||||
|
'file': 'part_label.html',
|
||||||
|
'name': 'Part Label',
|
||||||
|
'description': 'Simple part label',
|
||||||
|
'width': 50,
|
||||||
|
'height': 24,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
for label in labels:
|
||||||
|
|
||||||
|
filename = os.path.join(
|
||||||
|
'label',
|
||||||
|
'inventree',
|
||||||
|
'part',
|
||||||
|
label['file']
|
||||||
|
)
|
||||||
|
|
||||||
|
src_file = os.path.join(src_dir, label['file'])
|
||||||
|
dst_file = os.path.join(settings.MEDIA_ROOT, filename)
|
||||||
|
|
||||||
|
to_copy = False
|
||||||
|
|
||||||
|
if os.path.exists(dst_file):
|
||||||
|
# File already exists - let's see if it is the "same"
|
||||||
|
|
||||||
|
if not hashFile(dst_file) == hashFile(src_file):
|
||||||
|
logger.info(f"Hash differs for '{filename}'")
|
||||||
|
to_copy = True
|
||||||
|
|
||||||
|
else:
|
||||||
|
logger.info(f"Label template '{filename}' is not present")
|
||||||
|
to_copy = True
|
||||||
|
|
||||||
|
if to_copy:
|
||||||
|
logger.info(f"Copying label template '{dst_file}'")
|
||||||
|
shutil.copyfile(src_file, dst_file)
|
||||||
|
|
||||||
|
# Check if a label matching the template already exists
|
||||||
|
if PartLabel.objects.filter(label=filename).exists():
|
||||||
|
continue
|
||||||
|
|
||||||
|
logger.info(f"Creating entry for PartLabel '{label['name']}'")
|
||||||
|
|
||||||
|
PartLabel.objects.create(
|
||||||
|
name=label['name'],
|
||||||
|
description=label['description'],
|
||||||
|
label=filename,
|
||||||
|
filters='',
|
||||||
|
enabled=True,
|
||||||
|
width=label['width'],
|
||||||
|
height=label['height'],
|
||||||
|
)
|
||||||
|
33
InvenTree/label/templates/label/part/part_label.html
Normal file
33
InvenTree/label/templates/label/part/part_label.html
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
{% extends "label/label_base.html" %}
|
||||||
|
|
||||||
|
{% load barcode %}
|
||||||
|
|
||||||
|
{% block style %}
|
||||||
|
|
||||||
|
.qr {
|
||||||
|
position: fixed;
|
||||||
|
left: 0mm;
|
||||||
|
top: 0mm;
|
||||||
|
height: {{ height }}mm;
|
||||||
|
width: {{ height }}mm;
|
||||||
|
}
|
||||||
|
|
||||||
|
.part {
|
||||||
|
font-family: Arial, Helvetica, sans-serif;
|
||||||
|
display: inline;
|
||||||
|
position: absolute;
|
||||||
|
left: {{ height }}mm;
|
||||||
|
top: 2mm;
|
||||||
|
}
|
||||||
|
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
|
||||||
|
<img class='qr' src='{% qrcode qr_data %}'>
|
||||||
|
|
||||||
|
<div class='part'>
|
||||||
|
{{ part.full_name }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{% endblock %}
|
Loading…
x
Reference in New Issue
Block a user