diff --git a/InvenTree/label/api.py b/InvenTree/label/api.py index 1a679d5ac5..5103d99676 100644 --- a/InvenTree/label/api.py +++ b/InvenTree/label/api.py @@ -138,6 +138,9 @@ class LabelPrintMixin: """ + # Label instance + label_instance = self.get_object() + for output in outputs: """ For each output, we generate a temporary image file, @@ -156,7 +159,9 @@ class LabelPrintMixin: offload_task( 'plugin.events.print_label', plugin.plugin_slug(), - image + image, + label_instance=label_instance, + user=request.user, ) return JsonResponse({ diff --git a/InvenTree/part/tasks.py b/InvenTree/part/tasks.py index 9bc34f83df..b5e02e1128 100644 --- a/InvenTree/part/tasks.py +++ b/InvenTree/part/tasks.py @@ -5,7 +5,6 @@ import logging from django.utils.translation import ugettext_lazy as _ - import InvenTree.helpers import InvenTree.tasks import common.notifications diff --git a/InvenTree/plugin/events.py b/InvenTree/plugin/events.py index 829aeaa935..7a2e0d2da3 100644 --- a/InvenTree/plugin/events.py +++ b/InvenTree/plugin/events.py @@ -7,12 +7,15 @@ from __future__ import unicode_literals import logging +from django.utils.translation import ugettext_lazy as _ + from django.conf import settings from django.db import transaction from django.db.models.signals import post_save, post_delete from django.dispatch.dispatcher import receiver from common.models import InvenTreeSetting +import common.notifications from InvenTree.ready import canAppAccessDatabase from InvenTree.tasks import offload_task @@ -192,12 +195,14 @@ def after_delete(sender, instance, **kwargs): ) -def print_label(plugin_slug, label_image, **kwargs): +def print_label(plugin_slug, label_image, label_instance=None, user=None): """ Print label with the provided plugin. This task is nominally handled by the background worker. + If the printing fails (throws an exception) then the user is notified. + Arguments: plugin_slug: The unique slug (key) of the plugin label_image: A PIL.Image image object to be printed @@ -211,4 +216,21 @@ def print_label(plugin_slug, label_image, **kwargs): logger.error(f"Could not find matching plugin for '{plugin_slug}'") return - plugin.print_label(label_image) + try: + plugin.print_label(label_image) + except Exception as e: + # Plugin threw an error - notify the user who attempted to print + + ctx = { + 'name': _('Label printing failed'), + 'message': str(e), + } + + logger.error(f"Label printing failed: Sending notification to user '{user}'") + + common.notifications.trigger_notifaction( + label_instance, + 'label.printing_failed', + targets=[user], + context=ctx, + )