use new loading mechanism for barcodes too

This commit is contained in:
Matthias 2022-01-10 01:06:05 +01:00
parent 13ff94b6b2
commit 4fc3e85a53
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
3 changed files with 3 additions and 50 deletions

View File

@ -13,7 +13,7 @@ from stock.models import StockItem
from stock.serializers import StockItemSerializer from stock.serializers import StockItemSerializer
from barcodes.barcode import hash_barcode from barcodes.barcode import hash_barcode
from plugin.plugins import load_barcode_plugins from plugin import plugin_registry
class BarcodeScan(APIView): class BarcodeScan(APIView):
@ -53,7 +53,7 @@ class BarcodeScan(APIView):
if 'barcode' not in data: if 'barcode' not in data:
raise ValidationError({'barcode': _('Must provide barcode_data parameter')}) raise ValidationError({'barcode': _('Must provide barcode_data parameter')})
plugins = load_barcode_plugins() plugins = plugin_registry.with_mixin('barcode')
barcode_data = data.get('barcode') barcode_data = data.get('barcode')
@ -160,7 +160,7 @@ class BarcodeAssign(APIView):
except (ValueError, StockItem.DoesNotExist): except (ValueError, StockItem.DoesNotExist):
raise ValidationError({'stockitem': _('No matching stock item found')}) raise ValidationError({'stockitem': _('No matching stock item found')})
plugins = load_barcode_plugins() plugins = plugin_registry.with_mixin('barcode')
plugin = None plugin = None

View File

@ -4,14 +4,10 @@
import inspect import inspect
import importlib import importlib
import pkgutil import pkgutil
import logging
from django.core.exceptions import AppRegistryNotReady from django.core.exceptions import AppRegistryNotReady
logger = logging.getLogger("inventree")
def iter_namespace(pkg): def iter_namespace(pkg):
"""get all modules in a package""" """get all modules in a package"""
return pkgutil.iter_modules(pkg.__path__, pkg.__name__ + ".") return pkgutil.iter_modules(pkg.__path__, pkg.__name__ + ".")
@ -70,34 +66,3 @@ def get_plugins(pkg, baseclass, recursive: bool = False):
plugins.append(plugin) plugins.append(plugin)
return plugins return plugins
def load_plugins(name: str, cls, module):
"""general function to load a plugin class
:param name: name of the plugin for logs
:type name: str
:param module: module from which the plugins should be loaded
:return: class of the to-be-loaded plugin
"""
logger.debug("Loading %s plugins", name)
plugins = get_plugins(module, cls)
if len(plugins) > 0:
logger.info("Discovered %i %s plugins:", len(plugins), name)
for plugin in plugins:
logger.debug(" - %s", plugin.PLUGIN_NAME)
return plugins
def load_barcode_plugins():
"""
Return a list of all registered barcode plugins
"""
from barcodes import plugins as BarcodePlugins
from barcodes.barcode import BarcodePlugin
return load_plugins('barcode', BarcodePlugin, BarcodePlugins)

View File

@ -34,18 +34,6 @@ class InvenTreePluginTests(TestCase):
self.assertEqual(self.named_plugin.plugin_name(), 'abc123') self.assertEqual(self.named_plugin.plugin_name(), 'abc123')
class PluginIntegrationTests(TestCase):
""" Tests for general plugin functions """
def test_plugin_loading(self):
"""check if plugins load as expected"""
# plugin_names_barcode = [a().plugin_name() for a in load_barcode_plugins()] # TODO refactor barcode plugin to support standard loading
# self.assertEqual(plugin_names_barcode, '')
# TODO remove test once loading is moved
class PluginTagTests(TestCase): class PluginTagTests(TestCase):
""" Tests for the plugin extras """ """ Tests for the plugin extras """