diff --git a/InvenTree/label/apps.py b/InvenTree/label/apps.py index 321047a551..8ec2be24f4 100644 --- a/InvenTree/label/apps.py +++ b/InvenTree/label/apps.py @@ -1,6 +1,7 @@ import os import shutil import logging +import hashlib from django.apps import AppConfig from django.conf import settings @@ -9,6 +10,20 @@ from django.conf import settings logger = logging.getLogger(__name__) +def hashFile(filename): + """ + Calculate the MD5 hash of a file + """ + + md5 = hashlib.md5() + + with open(filename, 'rb') as f: + data = f.read() + md5.update(data) + + return md5.hexdigest() + + class LabelConfig(AppConfig): name = 'label' @@ -35,6 +50,7 @@ class LabelConfig(AppConfig): src_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'templates', + 'label', 'stockitem', ) @@ -70,7 +86,21 @@ class LabelConfig(AppConfig): src_file = os.path.join(src_dir, label['file']) dst_file = os.path.join(settings.MEDIA_ROOT, filename) - if not os.path.exists(dst_file): + to_copy = False + + if os.path.exists(dst_file): + # File already exists - let's see if it is the "same", + # or if we need to overwrite it with a newer copy! + + 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) @@ -106,6 +136,7 @@ class LabelConfig(AppConfig): src_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'templates', + 'label', 'stocklocation', ) @@ -146,7 +177,21 @@ class LabelConfig(AppConfig): src_file = os.path.join(src_dir, label['file']) dst_file = os.path.join(settings.MEDIA_ROOT, filename) - if not os.path.exists(dst_file): + to_copy = False + + if os.path.exists(dst_file): + # File already exists - let's see if it is the "same", + # or if we need to overwrite it with a newer copy! + + 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) diff --git a/InvenTree/label/templates/label/stockitem/qr.html b/InvenTree/label/templates/label/stockitem/qr.html new file mode 100644 index 0000000000..8f489f81b8 --- /dev/null +++ b/InvenTree/label/templates/label/stockitem/qr.html @@ -0,0 +1,20 @@ +{% extends "label/label_base.html" %} + +{% load barcode %} + +{% block style %} + +.qr { + position: fixed; + left: 0mm; + top: 0mm; + height: {{ height }}mm; + width: {{ height }}mm; +} + +{% endblock %} + +{% block content %} + + +{% endblock %} diff --git a/InvenTree/label/templates/label/stocklocation/qr.html b/InvenTree/label/templates/label/stocklocation/qr.html new file mode 100644 index 0000000000..8f489f81b8 --- /dev/null +++ b/InvenTree/label/templates/label/stocklocation/qr.html @@ -0,0 +1,20 @@ +{% extends "label/label_base.html" %} + +{% load barcode %} + +{% block style %} + +.qr { + position: fixed; + left: 0mm; + top: 0mm; + height: {{ height }}mm; + width: {{ height }}mm; +} + +{% endblock %} + +{% block content %} + + +{% endblock %} diff --git a/InvenTree/label/templates/label/stocklocation/qr_and_text.html b/InvenTree/label/templates/label/stocklocation/qr_and_text.html new file mode 100644 index 0000000000..c5756b96f2 --- /dev/null +++ b/InvenTree/label/templates/label/stocklocation/qr_and_text.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; +} + +.loc { + font-family: Arial, Helvetica, sans-serif; + display: inline; + position: absolute; + left: {{ width }}, + top: 2mm; +} + +{% endblock %} + +{% block content %} + + + +
+ {{ location.name }} +
+ +{% endblock %} diff --git a/InvenTree/label/templates/stockitem/qr.html b/InvenTree/label/templates/stockitem/qr.html deleted file mode 100644 index 9cd9d20769..0000000000 --- a/InvenTree/label/templates/stockitem/qr.html +++ /dev/null @@ -1,16 +0,0 @@ - - - diff --git a/InvenTree/label/templates/stocklocation/qr.html b/InvenTree/label/templates/stocklocation/qr.html deleted file mode 100644 index 8fb22fbb40..0000000000 --- a/InvenTree/label/templates/stocklocation/qr.html +++ /dev/null @@ -1,16 +0,0 @@ - - - diff --git a/InvenTree/label/templates/stocklocation/qr_and_text.html b/InvenTree/label/templates/stocklocation/qr_and_text.html deleted file mode 100644 index 9a555aca15..0000000000 --- a/InvenTree/label/templates/stocklocation/qr_and_text.html +++ /dev/null @@ -1,43 +0,0 @@ - - - - -
- {{ location.name }} -
-
-
- Location ID: {{ location.pk }} -
- - \ No newline at end of file