Send notification if printing fails

If label printing fails (due to a plugin error) then the user is notified.
This commit is contained in:
Oliver 2022-03-25 15:57:32 +11:00
parent 5921964fdb
commit c1b408f8a3
3 changed files with 30 additions and 4 deletions
InvenTree

View File

@ -138,6 +138,9 @@ class LabelPrintMixin:
""" """
# Label instance
label_instance = self.get_object()
for output in outputs: for output in outputs:
""" """
For each output, we generate a temporary image file, For each output, we generate a temporary image file,
@ -156,7 +159,9 @@ class LabelPrintMixin:
offload_task( offload_task(
'plugin.events.print_label', 'plugin.events.print_label',
plugin.plugin_slug(), plugin.plugin_slug(),
image image,
label_instance=label_instance,
user=request.user,
) )
return JsonResponse({ return JsonResponse({

View File

@ -5,7 +5,6 @@ import logging
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
import InvenTree.helpers import InvenTree.helpers
import InvenTree.tasks import InvenTree.tasks
import common.notifications import common.notifications

View File

@ -7,12 +7,15 @@ from __future__ import unicode_literals
import logging import logging
from django.utils.translation import ugettext_lazy as _
from django.conf import settings from django.conf import settings
from django.db import transaction from django.db import transaction
from django.db.models.signals import post_save, post_delete from django.db.models.signals import post_save, post_delete
from django.dispatch.dispatcher import receiver from django.dispatch.dispatcher import receiver
from common.models import InvenTreeSetting from common.models import InvenTreeSetting
import common.notifications
from InvenTree.ready import canAppAccessDatabase from InvenTree.ready import canAppAccessDatabase
from InvenTree.tasks import offload_task 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. Print label with the provided plugin.
This task is nominally handled by the background worker. This task is nominally handled by the background worker.
If the printing fails (throws an exception) then the user is notified.
Arguments: Arguments:
plugin_slug: The unique slug (key) of the plugin plugin_slug: The unique slug (key) of the plugin
label_image: A PIL.Image image object to be printed 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}'") logger.error(f"Could not find matching plugin for '{plugin_slug}'")
return 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,
)