diff --git a/InvenTree/label/apps.py b/InvenTree/label/apps.py index e51767d5f0..0293c3d18c 100644 --- a/InvenTree/label/apps.py +++ b/InvenTree/label/apps.py @@ -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'], + ) diff --git a/InvenTree/label/templates/label/part/part_label.html b/InvenTree/label/templates/label/part/part_label.html new file mode 100644 index 0000000000..558e1bca5b --- /dev/null +++ b/InvenTree/label/templates/label/part/part_label.html @@ -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 %} + + + +
+ {{ part.full_name }} +
+ +{% endblock %} \ No newline at end of file