mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 05:05:42 +00:00 
			
		
		
		
	Push even more barcode decoding to the individual plugin
- DigiKey barcode is NOT json formatted, for example...
This commit is contained in:
		| @@ -5,8 +5,6 @@ Main JSON interface views | ||||
| # -*- coding: utf-8 -*- | ||||
| from __future__ import unicode_literals | ||||
|  | ||||
| import json | ||||
|  | ||||
| from django.utils.translation import ugettext as _ | ||||
| from django.http import JsonResponse | ||||
|  | ||||
| @@ -61,49 +59,32 @@ class BarcodeScanView(APIView): | ||||
|         print("Barcode data:") | ||||
|         print(barcode_data) | ||||
|  | ||||
|         valid_data = False | ||||
|  | ||||
|         if barcode_data is None: | ||||
|             response['error'] = _('No barcode data provided') | ||||
|  | ||||
|         elif type(barcode_data) is dict: | ||||
|             valid_data = True | ||||
|         # Look for a barcode plugin that knows how to handle the data | ||||
|         for plugin_class in barcode_plugins: | ||||
|  | ||||
|         elif type(barcode_data) is str: | ||||
|             # Attempt to decode the barcode into a JSON object | ||||
|             try: | ||||
|                 barcode_data = json.loads(barcode_data) | ||||
|                 valid_data = True | ||||
|             except json.JSONDecodeError: | ||||
|                 response['error'] = _('Barcode is not a JSON object') | ||||
|             # Instantiate the plugin with the provided plugin data | ||||
|             plugin = plugin_class(barcode_data) | ||||
|  | ||||
|         else: | ||||
|             response['error'] = _('Barcode data is unknown format') | ||||
|             if plugin.validate(): | ||||
|                  | ||||
|         if valid_data: | ||||
|             # Look for a barcode plugin that knows how to handle the data | ||||
|             for plugin_class in barcode_plugins: | ||||
|                 # Plugin should return a dict response | ||||
|                 response = plugin.decode() | ||||
|                  | ||||
|                 # Instantiate the plugin with the provided plugin data | ||||
|                 plugin = plugin_class(barcode_data) | ||||
|                 if type(response) is dict: | ||||
|                     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') | ||||
|                     } | ||||
|  | ||||
|                 if plugin.validate(): | ||||
|                 response['plugin'] = plugin.get_name() | ||||
|                 response['hash'] = plugin.hash() | ||||
|  | ||||
|                     # Plugin should return a dict response | ||||
|                     response = plugin.decode() | ||||
|                      | ||||
|                     if type(response) is dict: | ||||
|                         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') | ||||
|                         } | ||||
|  | ||||
|                     response['plugin'] = plugin.get_name() | ||||
|                     response['hash'] = plugin.hash() | ||||
|  | ||||
|                     break | ||||
|                 break | ||||
|  | ||||
|         if 'error' not in response and 'success' not in response: | ||||
|             response = { | ||||
|   | ||||
| @@ -11,6 +11,8 @@ references model objects actually exist in the database. | ||||
|  | ||||
| # -*- coding: utf-8 -*- | ||||
|  | ||||
| import json | ||||
|  | ||||
| from . import barcode | ||||
|  | ||||
| from stock.models import StockItem, StockLocation | ||||
| @@ -34,6 +36,17 @@ class InvenTreeBarcodePlugin(barcode.BarcodePlugin): | ||||
|  | ||||
|         """ | ||||
|  | ||||
|         # The data must either be dict or be able to dictified | ||||
|         if type(self.data) is dict: | ||||
|             pass | ||||
|         elif type(self.data) is str: | ||||
|             try: | ||||
|                 self.data = json.loads(self.data) | ||||
|             except json.JSONDecodeError: | ||||
|                 return False | ||||
|         else: | ||||
|             return False | ||||
|  | ||||
|         for key in ['tool', 'version']: | ||||
|             if key not in self.data.keys(): | ||||
|                 return False | ||||
|   | ||||
		Reference in New Issue
	
	Block a user