mirror of
https://github.com/inventree/InvenTree.git
synced 2025-06-18 21:15:41 +00:00
Copy default part label templates
This commit is contained in:
@ -37,6 +37,7 @@ class LabelConfig(AppConfig):
|
||||
if canAppAccessDatabase():
|
||||
self.create_stock_item_labels()
|
||||
self.create_stock_location_labels()
|
||||
self.create_part_labels()
|
||||
|
||||
def create_stock_item_labels(self):
|
||||
"""
|
||||
@ -65,7 +66,7 @@ class LabelConfig(AppConfig):
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
labels = [
|
||||
@ -109,24 +110,21 @@ class LabelConfig(AppConfig):
|
||||
logger.info(f"Copying label template '{dst_file}'")
|
||||
shutil.copyfile(src_file, dst_file)
|
||||
|
||||
try:
|
||||
# Check if a label matching the template already exists
|
||||
if StockItemLabel.objects.filter(label=filename).exists():
|
||||
continue
|
||||
# Check if a label matching the template already exists
|
||||
if StockItemLabel.objects.filter(label=filename).exists():
|
||||
continue
|
||||
|
||||
logger.info(f"Creating entry for StockItemLabel '{label['name']}'")
|
||||
logger.info(f"Creating entry for StockItemLabel '{label['name']}'")
|
||||
|
||||
StockItemLabel.objects.create(
|
||||
name=label['name'],
|
||||
description=label['description'],
|
||||
label=filename,
|
||||
filters='',
|
||||
enabled=True,
|
||||
width=label['width'],
|
||||
height=label['height'],
|
||||
)
|
||||
except:
|
||||
pass
|
||||
StockItemLabel.objects.create(
|
||||
name=label['name'],
|
||||
description=label['description'],
|
||||
label=filename,
|
||||
filters='',
|
||||
enabled=True,
|
||||
width=label['width'],
|
||||
height=label['height'],
|
||||
)
|
||||
|
||||
def create_stock_location_labels(self):
|
||||
"""
|
||||
@ -155,7 +153,7 @@ class LabelConfig(AppConfig):
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
labels = [
|
||||
@ -206,21 +204,103 @@ class LabelConfig(AppConfig):
|
||||
logger.info(f"Copying label template '{dst_file}'")
|
||||
shutil.copyfile(src_file, dst_file)
|
||||
|
||||
try:
|
||||
# Check if a label matching the template already exists
|
||||
if StockLocationLabel.objects.filter(label=filename).exists():
|
||||
continue
|
||||
# Check if a label matching the template already exists
|
||||
if StockLocationLabel.objects.filter(label=filename).exists():
|
||||
continue
|
||||
|
||||
logger.info(f"Creating entry for StockLocationLabel '{label['name']}'")
|
||||
logger.info(f"Creating entry for StockLocationLabel '{label['name']}'")
|
||||
|
||||
StockLocationLabel.objects.create(
|
||||
name=label['name'],
|
||||
description=label['description'],
|
||||
label=filename,
|
||||
filters='',
|
||||
enabled=True,
|
||||
width=label['width'],
|
||||
height=label['height'],
|
||||
)
|
||||
except:
|
||||
pass
|
||||
StockLocationLabel.objects.create(
|
||||
name=label['name'],
|
||||
description=label['description'],
|
||||
label=filename,
|
||||
filters='',
|
||||
enabled=True,
|
||||
width=label['width'],
|
||||
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:
|
||||
# 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'],
|
||||
)
|
||||
|
Reference in New Issue
Block a user