From 5de85defa7762c69fce56b4da9d789f6f5bfb14e Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 14 Apr 2020 22:00:58 +1000 Subject: [PATCH] Validation of InvenTree style barcodes --- InvenTree/InvenTree/api.py | 3 +- InvenTree/plugins/barcode/barcode.py | 21 +++++++++++-- InvenTree/plugins/barcode/inventree.py | 42 +++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 4 deletions(-) diff --git a/InvenTree/InvenTree/api.py b/InvenTree/InvenTree/api.py index af8290b236..69d7695974 100644 --- a/InvenTree/InvenTree/api.py +++ b/InvenTree/InvenTree/api.py @@ -76,7 +76,8 @@ class BarcodeScanView(APIView): response = plugin.decode_barcode(barcode_data) if type(response) is dict: - response['success'] = _('Barcode successfully decoded') + if 'success' not in response.keys() and 'error' not in response.keys(): + response['success'] = _('Barcode successfully decoded') else: response = { 'error': _('Barcode plugin returned incorrect response') diff --git a/InvenTree/plugins/barcode/barcode.py b/InvenTree/plugins/barcode/barcode.py index 447a8d4edc..90d9cf848d 100644 --- a/InvenTree/plugins/barcode/barcode.py +++ b/InvenTree/plugins/barcode/barcode.py @@ -8,6 +8,9 @@ class BarcodePlugin(plugin.InvenTreePlugin): The BarcodePlugin class is the base class for any barcode plugin. """ + def __init__(self): + plugin.InvenTreePlugin.__init__(self) + def validate_barcode(self, barcode_data): """ Default implementation returns False @@ -21,5 +24,19 @@ class BarcodePlugin(plugin.InvenTreePlugin): return None - def __init__(self): - plugin.InvenTreePlugin.__init__(self) + def render_part(self, part): + return { + 'id': part.id, + 'name': part.full_name, + } + + def render_stock_location(self, loc): + return { + "id": loc.id + } + + def render_stock_item(self, item): + + return { + "id": item.id, + } diff --git a/InvenTree/plugins/barcode/inventree.py b/InvenTree/plugins/barcode/inventree.py index b13e0643a3..ba2b7e737d 100644 --- a/InvenTree/plugins/barcode/inventree.py +++ b/InvenTree/plugins/barcode/inventree.py @@ -2,6 +2,11 @@ from . import barcode +from stock.models import StockItem, StockLocation +from part.models import Part + +from django.utils.translation import ugettext as _ + class InvenTreeBarcodePlugin(barcode.BarcodePlugin): @@ -28,4 +33,39 @@ class InvenTreeBarcodePlugin(barcode.BarcodePlugin): return True def decode_barcode(self, barcode_data): - pass + + response = {} + + if 'part' in barcode_data.keys(): + id = barcode_data['part'].get('id', None) + + try: + part = Part.objects.get(id=id) + response['part'] = self.render_part(part) + except (ValueError, Part.DoesNotExist): + response['error'] = _('Part does not exist') + + elif 'stocklocation' in barcode_data.keys(): + id = barcode_data['stocklocation'].get('id', None) + + try: + loc = StockLocation.objects.get(id=id) + response['stocklocation'] = self.render_stock_location(loc) + except (ValueError, StockLocation.DoesNotExist): + response['error'] = _('StockLocation does not exist') + + elif 'stockitem' in barcode_data.keys(): + + id = barcode_data['stockitem'].get('id', None) + + try: + item = StockItem.objects.get(id=id) + response['stockitem'] = self.render_stock_item(item) + except (ValueError, StockItem.DoesNotExist): + response['error'] = _('StockItem does not exist') + + else: + response['error'] = _('No matching data') + + return response +