From cf3d96a2655f149a0738e710539880fae820be8a Mon Sep 17 00:00:00 2001 From: Bobbe <34186858+30350n@users.noreply.github.com> Date: Wed, 15 Nov 2023 03:40:30 +0100 Subject: [PATCH] Fix some issues with supplier barcode plugins (#5919) * Remove unnecessary logging imports from supplier plugins * Fix MPN filtering in get_supplier_parts * Ensure digikey barcodes contain a SKU * Keep running other barcode plugins if an error occured in one of them * Fix typo (replace plugin with current_plugin) * Fix class name --- InvenTree/plugin/base/barcodes/api.py | 25 +++++++++++++++++-- InvenTree/plugin/base/barcodes/mixins.py | 2 +- InvenTree/plugin/builtin/suppliers/digikey.py | 8 +++--- InvenTree/plugin/builtin/suppliers/lcsc.py | 3 --- InvenTree/plugin/builtin/suppliers/mouser.py | 4 --- InvenTree/plugin/builtin/suppliers/tme.py | 3 --- 6 files changed, 28 insertions(+), 17 deletions(-) diff --git a/InvenTree/plugin/base/barcodes/api.py b/InvenTree/plugin/base/barcodes/api.py index 2f9332daa8..844a90da93 100644 --- a/InvenTree/plugin/base/barcodes/api.py +++ b/InvenTree/plugin/base/barcodes/api.py @@ -1,5 +1,6 @@ """API endpoints for barcode plugins.""" +import logging from django.urls import path, re_path from django.utils.translation import gettext_lazy as _ @@ -17,6 +18,8 @@ from plugin.builtin.barcodes.inventree_barcode import \ from stock.models import StockLocation from users.models import RuleSet +logger = logging.getLogger('inventree') + class BarcodeScan(APIView): """Endpoint for handling generic barcode scan requests. @@ -68,7 +71,16 @@ class BarcodeScan(APIView): result = current_plugin.scan(barcode_data) - if result is not None: + if result is None: + continue + + if "error" in result: + logger.info("%s.scan(...) returned an error: %s", + current_plugin.__class__.__name__, result["error"]) + if not response: + plugin = current_plugin + response = result + else: plugin = current_plugin response = result break @@ -300,7 +312,16 @@ class BarcodePOReceive(APIView): location=location, ) - if result is not None: + if result is None: + continue + + if "error" in result: + logger.info("%s.scan_receive_item(...) returned an error: %s", + current_plugin.__class__.__name__, result["error"]) + if not response: + plugin = current_plugin + response = result + else: plugin = current_plugin response = result break diff --git a/InvenTree/plugin/base/barcodes/mixins.py b/InvenTree/plugin/base/barcodes/mixins.py index bbe6d7d31d..cc0e9a379b 100644 --- a/InvenTree/plugin/base/barcodes/mixins.py +++ b/InvenTree/plugin/base/barcodes/mixins.py @@ -130,7 +130,7 @@ class BarcodeMixin: return supplier_parts if mpn: - supplier_parts = SupplierPart.objects.filter(manufacturer_part__MPN__iexact=mpn) + supplier_parts = supplier_parts.filter(manufacturer_part__MPN__iexact=mpn) if len(supplier_parts) == 1: return supplier_parts diff --git a/InvenTree/plugin/builtin/suppliers/digikey.py b/InvenTree/plugin/builtin/suppliers/digikey.py index 51caa353cd..67db4db723 100644 --- a/InvenTree/plugin/builtin/suppliers/digikey.py +++ b/InvenTree/plugin/builtin/suppliers/digikey.py @@ -3,16 +3,12 @@ This plugin can currently only match DigiKey barcodes to supplier parts. """ -import logging - from django.utils.translation import gettext_lazy as _ from plugin import InvenTreePlugin from plugin.base.barcodes.mixins import SupplierBarcodeData from plugin.mixins import SettingsMixin, SupplierBarcodeMixin -logger = logging.getLogger('inventree') - class DigiKeyPlugin(SupplierBarcodeMixin, SettingsMixin, InvenTreePlugin): """Plugin to integrate the DigiKey API into Inventree.""" @@ -41,6 +37,10 @@ class DigiKeyPlugin(SupplierBarcodeMixin, SettingsMixin, InvenTreePlugin): if not (barcode_fields := self.parse_ecia_barcode2d(barcode_data)): return None + # digikey barcodes should always contain a SKU + if "supplier_part_number" not in barcode_fields: + return None + return SupplierBarcodeData( SKU=barcode_fields.get("supplier_part_number"), MPN=barcode_fields.get("manufacturer_part_number"), diff --git a/InvenTree/plugin/builtin/suppliers/lcsc.py b/InvenTree/plugin/builtin/suppliers/lcsc.py index c4179767f1..2b9e245db2 100644 --- a/InvenTree/plugin/builtin/suppliers/lcsc.py +++ b/InvenTree/plugin/builtin/suppliers/lcsc.py @@ -3,7 +3,6 @@ This plugin can currently only match LCSC barcodes to supplier parts. """ -import logging import re from django.utils.translation import gettext_lazy as _ @@ -12,8 +11,6 @@ from plugin import InvenTreePlugin from plugin.base.barcodes.mixins import SupplierBarcodeData from plugin.mixins import SettingsMixin, SupplierBarcodeMixin -logger = logging.getLogger('inventree') - class LCSCPlugin(SupplierBarcodeMixin, SettingsMixin, InvenTreePlugin): """Plugin to integrate the LCSC API into Inventree.""" diff --git a/InvenTree/plugin/builtin/suppliers/mouser.py b/InvenTree/plugin/builtin/suppliers/mouser.py index 50ad2785c8..a2ab9e4f70 100644 --- a/InvenTree/plugin/builtin/suppliers/mouser.py +++ b/InvenTree/plugin/builtin/suppliers/mouser.py @@ -3,16 +3,12 @@ This plugin currently only match Mouser barcodes to supplier parts. """ -import logging - from django.utils.translation import gettext_lazy as _ from plugin import InvenTreePlugin from plugin.base.barcodes.mixins import SupplierBarcodeData from plugin.mixins import SettingsMixin, SupplierBarcodeMixin -logger = logging.getLogger('inventree') - class MouserPlugin(SupplierBarcodeMixin, SettingsMixin, InvenTreePlugin): """Plugin to integrate the Mouser API into Inventree.""" diff --git a/InvenTree/plugin/builtin/suppliers/tme.py b/InvenTree/plugin/builtin/suppliers/tme.py index bfcb4c33b5..de799a084f 100644 --- a/InvenTree/plugin/builtin/suppliers/tme.py +++ b/InvenTree/plugin/builtin/suppliers/tme.py @@ -3,7 +3,6 @@ This plugin can currently only match TME barcodes to supplier parts. """ -import logging import re from django.utils.translation import gettext_lazy as _ @@ -12,8 +11,6 @@ from plugin import InvenTreePlugin from plugin.base.barcodes.mixins import SupplierBarcodeData from plugin.mixins import SettingsMixin, SupplierBarcodeMixin -logger = logging.getLogger('inventree') - class TMEPlugin(SupplierBarcodeMixin, SettingsMixin, InvenTreePlugin): """Plugin to integrate the TME API into Inventree."""