Merge pull request #2772 from SchrodingersGat/print-failed

Send notification if printing fails
This commit is contained in:
Oliver 2022-03-25 16:22:30 +11:00 committed by GitHub
commit 42f03296d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 6 deletions

View File

@ -154,11 +154,18 @@ class UIMessageNotification(SingleNotificationMethod):
return True return True
def trigger_notifaction(obj, category=None, obj_ref='pk', targets=None, target_fnc=None, target_args=[], target_kwargs={}, context={}): def trigger_notifaction(obj, category=None, obj_ref='pk', **kwargs):
""" """
Send out a notification Send out a notification
""" """
targets = kwargs.get('targets', None)
target_fnc = kwargs.get('target_fnc', None)
target_args = kwargs.get('target_args', [])
target_kwargs = kwargs.get('target_kwargs', {})
context = kwargs.get('context', {})
delivery_methods = kwargs.get('delivery_methods', None)
# Check if data is importing currently # Check if data is importing currently
if isImportingData(): if isImportingData():
return return
@ -190,7 +197,8 @@ def trigger_notifaction(obj, category=None, obj_ref='pk', targets=None, target_f
logger.info(f"Sending notification '{category}' for '{str(obj)}'") logger.info(f"Sending notification '{category}' for '{str(obj)}'")
# Collect possible methods # Collect possible methods
delivery_methods = inheritors(NotificationMethod) if delivery_methods is None:
delivery_methods = inheritors(NotificationMethod)
for method in [a for a in delivery_methods if a not in [SingleNotificationMethod, BulkNotificationMethod]]: for method in [a for a in delivery_methods if a not in [SingleNotificationMethod, BulkNotificationMethod]]:
logger.info(f"Triggering method '{method.METHOD_NAME}'") logger.info(f"Triggering method '{method.METHOD_NAME}'")

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,22 @@ 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,
delivery_methods=[common.notifications.UIMessageNotification]
)