Validation of InvenTree style barcodes

This commit is contained in:
Oliver Walters 2020-04-14 22:00:58 +10:00
parent 4a615e05ae
commit 5de85defa7
3 changed files with 62 additions and 4 deletions

View File

@ -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')

View File

@ -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,
}

View File

@ -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