feat(barcode): Add barcode support

This commit is contained in:
Andreas Mieke 2024-04-20 22:42:17 -04:00
parent fbfb35634e
commit bfaa0b7f89
Signed by: zenermerps
SSH key fingerprint: SHA256:Ne+hwc5QIgYlqCuLZ0LV3301Wo/p8UoGOrGC+T6S0t8

View file

@ -3,18 +3,21 @@ from django.utils.translation import gettext_lazy as _
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.core.validators import MaxValueValidator from django.core.validators import MaxValueValidator
# printer supportt # printer support
import socket import socket
import io
import json import json
# barcode helpers
from InvenTree.helpers_model import getModelsWithMixin
from InvenTree.models import InvenTreeBarcodeMixin
# InvenTree plugin libs # InvenTree plugin libs
from plugin import InvenTreePlugin from plugin import InvenTreePlugin
from plugin.mixins import LabelPrintingMixin, SettingsMixin from plugin.mixins import LabelPrintingMixin, SettingsMixin, BarcodeMixin
from inventree_phomemo.version import PHOMEMO_PLUGIN_VERSION from inventree_phomemo.version import PHOMEMO_PLUGIN_VERSION
class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin): class PhomemoLabelPlugin(LabelPrintingMixin, BarcodeMixin, SettingsMixin, InvenTreePlugin):
AUTHOR = "Sergal.engineering" AUTHOR = "Sergal.engineering"
DESCRIPTION = "Label printing plugin for Phomemo printers" DESCRIPTION = "Label printing plugin for Phomemo printers"
VERSION = PHOMEMO_PLUGIN_VERSION VERSION = PHOMEMO_PLUGIN_VERSION
@ -68,8 +71,6 @@ class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
'params': tpart.parameters_map() if hasattr(tpart, 'parameters_map') else None, 'params': tpart.parameters_map() if hasattr(tpart, 'parameters_map') else None,
'category': tpart.category.name if hasattr(tpart, 'category') else None, 'category': tpart.category.name if hasattr(tpart, 'category') else None,
'category_path': tpart.category.pathstring if hasattr(tpart, 'category') else None, 'category_path': tpart.category.pathstring if hasattr(tpart, 'category') else None,
'location': tpart.location.name if hasattr(tpart, 'location') else None,
'location_path': tpart.location.pathstring if hasattr(tpart, 'location') else None,
'barcode': barcode, 'barcode': barcode,
'type': kwargs['label_instance'].SUBDIR 'type': kwargs['label_instance'].SUBDIR
} }
@ -85,3 +86,45 @@ class PhomemoLabelPlugin(LabelPrintingMixin, SettingsMixin, InvenTreePlugin):
print_socket.close() print_socket.close()
except Exception as error: except Exception as error:
raise ConnectionError('Error connecting to printer server: ' + str(error)) raise ConnectionError('Error connecting to printer server: ' + str(error))
@staticmethod
def get_supported_barcode_models():
"""Returns a list of database models which support barcode functionality."""
return getModelsWithMixin(InvenTreeBarcodeMixin)
def format_matched_response(self, label, model, instance):
"""Format a response for the scanned data."""
return {label: instance.format_matched_response()}
def scan(self, barcode_data):
# Our barcode should be a string
if type(barcode_data) is not str:
barcode_data = str(barcode_data)
# Our barcodes are at least two symbols long
if len(barcode_data) < 2:
return
match barcode_data[0]:
case '1':
model_type = 'part'
case '2':
model_type = 'stockitem'
case '3':
model_type = 'stocklocation'
case '4':
model_type = 'build'
supported_models = self.get_supported_barcode_models()
for model in supported_models:
label = model.barcode_model_type()
if label == model_type:
try:
pk = int(barcode_data[1:])
instance = model.objects.get(pk=pk)
return self.format_matched_response(label, model, instance)
except (ValueError, model.DoesNotExist):
pass