mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Refactor printer into own base dir
This commit is contained in:
		| @@ -156,7 +156,7 @@ class LabelPrintMixin: | ||||
|  | ||||
|                 # Offload a background task to print the provided label | ||||
|                 offload_task( | ||||
|                     'plugin.events.print_label', | ||||
|                     'plugin.base.label.label.print_label', | ||||
|                     plugin.plugin_slug(), | ||||
|                     image, | ||||
|                     label_instance=label_instance, | ||||
|   | ||||
| @@ -14,8 +14,6 @@ from django.db import transaction | ||||
| from django.db.models.signals import post_save, post_delete | ||||
| from django.dispatch.dispatcher import receiver | ||||
|  | ||||
| import common.notifications | ||||
|  | ||||
| from InvenTree.ready import canAppAccessDatabase, isImportingData | ||||
| from InvenTree.tasks import offload_task | ||||
|  | ||||
| @@ -194,46 +192,3 @@ def after_delete(sender, instance, **kwargs): | ||||
|         f'{table}.deleted', | ||||
|         model=sender.__name__, | ||||
|     ) | ||||
|  | ||||
|  | ||||
| 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 | ||||
|     """ | ||||
|  | ||||
|     logger.info(f"Plugin '{plugin_slug}' is printing a label") | ||||
|  | ||||
|     plugin = registry.plugins.get(plugin_slug, None) | ||||
|  | ||||
|     if plugin is None: | ||||
|         logger.error(f"Could not find matching plugin for '{plugin_slug}'") | ||||
|         return | ||||
|  | ||||
|     try: | ||||
|         plugin.print_label(label_image, width=label_instance.width, height=label_instance.height) | ||||
|     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}'") | ||||
|  | ||||
|         # Throw an error against the plugin instance | ||||
|         common.notifications.trigger_notifaction( | ||||
|             plugin.plugin_config(), | ||||
|             'label.printing_failed', | ||||
|             targets=[user], | ||||
|             context=ctx, | ||||
|             delivery_methods=[common.notifications.UIMessageNotification] | ||||
|         ) | ||||
|   | ||||
							
								
								
									
										0
									
								
								InvenTree/plugin/base/label/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								InvenTree/plugin/base/label/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										52
									
								
								InvenTree/plugin/base/label/label.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								InvenTree/plugin/base/label/label.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| """Functions to print a label to a mixin printer""" | ||||
| import logging | ||||
|  | ||||
| from django.utils.translation import gettext_lazy as _ | ||||
|  | ||||
| from plugin.registry import registry | ||||
|  | ||||
|  | ||||
| logger = logging.getLogger('inventree') | ||||
|  | ||||
|  | ||||
| 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 | ||||
|     """ | ||||
|  | ||||
|     logger.info(f"Plugin '{plugin_slug}' is printing a label") | ||||
|  | ||||
|     plugin = registry.plugins.get(plugin_slug, None) | ||||
|  | ||||
|     if plugin is None: | ||||
|         logger.error(f"Could not find matching plugin for '{plugin_slug}'") | ||||
|         return | ||||
|  | ||||
|     try: | ||||
|         plugin.print_label(label_image, width=label_instance.width, height=label_instance.height) | ||||
|     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}'") | ||||
|  | ||||
|         # Throw an error against the plugin instance | ||||
|         common.notifications.trigger_notifaction( | ||||
|             plugin.plugin_config(), | ||||
|             'label.printing_failed', | ||||
|             targets=[user], | ||||
|             context=ctx, | ||||
|             delivery_methods=[common.notifications.UIMessageNotification] | ||||
|         ) | ||||
							
								
								
									
										37
									
								
								InvenTree/plugin/base/label/mixins.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								InvenTree/plugin/base/label/mixins.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| """Plugin mixin classes for label plugins""" | ||||
|  | ||||
|  | ||||
| class LabelPrintingMixin: | ||||
|     """ | ||||
|     Mixin which enables direct printing of stock labels. | ||||
|  | ||||
|     Each plugin must provide a NAME attribute, which is used to uniquely identify the printer. | ||||
|  | ||||
|     The plugin must also implement the print_label() function | ||||
|     """ | ||||
|  | ||||
|     class MixinMeta: | ||||
|         """ | ||||
|         Meta options for this mixin | ||||
|         """ | ||||
|         MIXIN_NAME = 'Label printing' | ||||
|  | ||||
|     def __init__(self):  # pragma: no cover | ||||
|         super().__init__() | ||||
|         self.add_mixin('labels', True, __class__) | ||||
|  | ||||
|     def print_label(self, label, **kwargs): | ||||
|         """ | ||||
|         Callback to print a single label | ||||
|  | ||||
|         Arguments: | ||||
|             label: A black-and-white pillow Image object | ||||
|  | ||||
|         kwargs: | ||||
|             length: The length of the label (in mm) | ||||
|             width: The width of the label (in mm) | ||||
|  | ||||
|         """ | ||||
|  | ||||
|         # Unimplemented (to be implemented by the particular plugin class) | ||||
|         ...  # pragma: no cover | ||||
| @@ -2,13 +2,14 @@ | ||||
| Utility class to enable simpler imports | ||||
| """ | ||||
|  | ||||
| from ..base.integration.mixins import APICallMixin, AppMixin, LabelPrintingMixin, SettingsMixin, ScheduleMixin, UrlsMixin, NavigationMixin, PanelMixin | ||||
| from ..base.integration.mixins import APICallMixin, AppMixin, SettingsMixin, ScheduleMixin, UrlsMixin, NavigationMixin, PanelMixin | ||||
|  | ||||
| from common.notifications import SingleNotificationMethod, BulkNotificationMethod | ||||
|  | ||||
| from ..base.action.mixins import ActionMixin | ||||
| from ..base.barcodes.mixins import BarcodeMixin | ||||
| from ..base.event.mixins import EventMixin | ||||
| from ..base.label.mixins import LabelPrintingMixin | ||||
|  | ||||
| __all__ = [ | ||||
|     'APICallMixin', | ||||
|   | ||||
		Reference in New Issue
	
	Block a user