From ccda637e3c67f462b35f9efee3b5813f90dc17e0 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Sun, 16 Aug 2020 13:42:27 +1000 Subject: [PATCH] Fixes for barcode decoding --- .../barcode/plugins/inventree_barcode.py | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/InvenTree/barcode/plugins/inventree_barcode.py b/InvenTree/barcode/plugins/inventree_barcode.py index 780b7e888a..6e4e6937a0 100644 --- a/InvenTree/barcode/plugins/inventree_barcode.py +++ b/InvenTree/barcode/plugins/inventree_barcode.py @@ -68,7 +68,7 @@ class InvenTreeBarcodePlugin(BarcodePlugin): # Initially try casting to an integer try: pk = int(data) - except (ValueError): + except (TypeError, ValueError): pk = None if pk is None: @@ -89,10 +89,21 @@ class InvenTreeBarcodePlugin(BarcodePlugin): for k in self.data.keys(): if k.lower() == 'stocklocation': + + pk = None + + # First try simple integer lookup try: - pk = self.data[k]['id'] - except (AttributeError, KeyError): - raise ValidationError({k: "id parameter not supplied"}) + pk = int(self.data[k]) + except (TypeError, ValueError): + pk = None + + if pk is None: + # Lookup by 'id' field + try: + pk = self.data[k]['id'] + except (AttributeError, KeyError): + raise ValidationError({k: "id parameter not supplied"}) try: loc = StockLocation.objects.get(pk=pk) @@ -106,10 +117,20 @@ class InvenTreeBarcodePlugin(BarcodePlugin): for k in self.data.keys(): if k.lower() == 'part': + + pk = None + + # Try integer lookup first try: - pk = self.data[k]['id'] - except (AttributeError, KeyError): - raise ValidationError({k, 'id parameter not supplied'}) + pk = int(self.data[k]) + except (TypeError, ValueError): + pk = None + + if pk is None: + try: + pk = self.data[k]['id'] + except (AttributeError, KeyError): + raise ValidationError({k, 'id parameter not supplied'}) try: part = Part.objects.get(pk=pk)