feat(label): Add print_labels for multiple labels

This commit is contained in:
Andreas Mieke 2024-04-21 21:10:51 -04:00
parent 7dc96c65ff
commit c5d55e79b2
Signed by: zenermerps
SSH key fingerprint: SHA256:Ne+hwc5QIgYlqCuLZ0LV3301Wo/p8UoGOrGC+T6S0t8

View file

@ -1,7 +1,5 @@
# translation
from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator
from django.core.validators import MaxValueValidator
# django
from django.http import JsonResponse
# printer support
import socket
@ -14,6 +12,7 @@ from InvenTree.models import InvenTreeBarcodeMixin
# InvenTree plugin libs
from plugin import InvenTreePlugin
from plugin.mixins import LabelPrintingMixin, SettingsMixin, BarcodeMixin
from label.models import LabelTemplate
from inventree_phomemo.version import PHOMEMO_PLUGIN_VERSION
@ -38,12 +37,7 @@ class PhomemoLabelPlugin(LabelPrintingMixin, BarcodeMixin, SettingsMixin, InvenT
},
}
def print_label(self, **kwargs):
# Read settings
ip_address = self.get_setting('IP_ADDRESS')
port = int(self.get_setting('PORT'))
def get_fields(self, **kwargs):
object_to_print = kwargs['label_instance'].object_to_print
match kwargs['label_instance'].SUBDIR:
@ -72,9 +66,52 @@ class PhomemoLabelPlugin(LabelPrintingMixin, BarcodeMixin, SettingsMixin, InvenT
'category': tpart.category.name if hasattr(tpart, 'category') else None,
'category_path': tpart.category.pathstring if hasattr(tpart, 'category') else None,
'barcode': barcode,
'type': kwargs['label_instance'].SUBDIR
'type': kwargs['label_instance'].SUBDIR,
'width': kwargs['width'],
'height': kwargs['height']
}
return fields
def print_labels(self, label: LabelTemplate, items: list, request, **kwargs):
outputs = []
# Get label content for every label
for item in items:
label.object_to_print = item
outputs.append(self.get_fields(label, request, **kwargs))
# Read settings
ip_address = self.get_setting('IP_ADDRESS')
port = int(self.get_setting('PORT'))
# Dump to json
data = json.dumps(outputs)
# Send the label to the printer
try:
print_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print_socket.settimeout(5)
print_socket.connect((ip_address, port))
print_socket.sendall(bytes(data,encoding="utf-8"))
print_socket.close()
except Exception as error:
raise ConnectionError('Error connecting to printer server: ' + str(error))
return JsonResponse({
'success': True,
'message': f'{len(items)} labels printed',
})
def print_label(self, **kwargs):
# Read settings
ip_address = self.get_setting('IP_ADDRESS')
port = int(self.get_setting('PORT'))
# Get label content
fields = self.get_fields(self, kwargs)
# Dump to json
data = json.dumps(fields)
# Send the label to the printer