From f1f07a1977f85378b86164077a641423593502c6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 24 Mar 2022 13:31:01 +1100 Subject: [PATCH] Extract plugin information from label printing request --- InvenTree/label/api.py | 36 ++++++++++++++++++++++ InvenTree/templates/js/translated/label.js | 3 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/InvenTree/label/api.py b/InvenTree/label/api.py index 475d4f8fea..99e51b48b4 100644 --- a/InvenTree/label/api.py +++ b/InvenTree/label/api.py @@ -2,6 +2,8 @@ from __future__ import unicode_literals from django.utils.translation import ugettext_lazy as _ + +from django.conf import settings from django.conf.urls import url, include from django.core.exceptions import ValidationError, FieldError from django.http import HttpResponse @@ -14,6 +16,8 @@ from rest_framework.response import Response import InvenTree.helpers import common.models +from plugin.registry import registry + from stock.models import StockItem, StockLocation from part.models import Part @@ -46,11 +50,43 @@ class LabelPrintMixin: Mixin for printing labels """ + def get_plugin(self, request): + """ + Return the label printing plugin associated with this request. + This is provided in the url, e.g. ?plugin=myprinter + + Requires: + - settings.PLUGINS_ENABLED is True + - matching plugin can be found + - matching plugin implements the 'labels' mixin + - matching plugin is enabled + """ + + if not settings.PLUGINS_ENABLED: + return None + + plugin_key = request.query_params.get('plugin', None) + + for slug, plugin in registry.plugins.items(): + + if slug == plugin_key and plugin.mixin_enabled('labels'): + + config = plugin.plugin_config() + + if config and config.active: + # Only return the plugin if it is enabled! + return plugin + + # No matches found + return None + def print(self, request, items_to_print): """ Print this label template against a number of pre-validated items """ + # Check the request to determine if the user has selected a label printing plugin + plugin = self.get_plugin(request) if len(items_to_print) == 0: # No valid items provided, return an error message data = { diff --git a/InvenTree/templates/js/translated/label.js b/InvenTree/templates/js/translated/label.js index 2e1a477387..f91d9c2431 100644 --- a/InvenTree/templates/js/translated/label.js +++ b/InvenTree/templates/js/translated/label.js @@ -222,7 +222,8 @@ function selectLabel(labels, items, options={}) { async: false, success: function(response) { response.forEach(function(plugin) { - if (plugin.mixins && plugin.mixins.labels) { + // Look for active plugins which implement the 'labels' mixin class + if (plugin.active && plugin.mixins && plugin.mixins.labels) { // This plugin supports label printing plugins.push(plugin); }