Coverage for src/backend/InvenTree/plugin/base/label/label.py: 26%

25 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1"""Functions to print a label to a mixin printer.""" 

2 

3from django.conf import settings 

4from django.utils.translation import gettext_lazy as _ 

5 

6import structlog 

7 

8import common.notifications 

9from InvenTree.exceptions import log_error 

10from plugin.registry import registry 

11 

12logger = structlog.get_logger('inventree') 

13 

14 

15def print_label(plugin_slug: str, **kwargs): 

16 """Print label with the provided plugin. 

17 

18 This task is nominally handled by the background worker. 

19 If the printing fails (throws an exception) then the user is notified. 

20 

21 Arguments: 

22 plugin_slug (str): The unique slug (key) of the plugin. 

23 

24 kwargs: 

25 passed through to the plugin.print_label() method 

26 """ 

27 from plugin.builtin.integration.core_notifications import InvenTreeUINotifications 

28 

29 logger.info("Plugin '%s' is printing a label", plugin_slug) 

30 

31 plugin = registry.get_plugin(plugin_slug, active=True) 

32 

33 if plugin is None: # pragma: no cover 

34 logger.error("Could not find matching active plugin for '%s'", plugin_slug) 

35 return 

36 

37 try: 

38 plugin.print_label(**kwargs) 

39 except Exception as e: # pragma: no cover 

40 # Plugin threw an error - notify the user who attempted to print 

41 ctx = {'name': _('Label printing failed'), 'message': str(e)} 

42 

43 user = kwargs.get('user') 

44 

45 if user: 

46 # Log an error message to the database 

47 log_error('print_label', plugin=plugin.slug) 

48 logger.exception( 

49 "Label printing failed: Sending notification to user '%s'", user 

50 ) # pragma: no cover 

51 

52 # Throw an error against the plugin instance 

53 common.notifications.trigger_notification( 

54 plugin.plugin_config(), 

55 'label.printing_failed', 

56 targets=[user], 

57 context=ctx, 

58 delivery_methods={InvenTreeUINotifications}, 

59 ) 

60 

61 if settings.TESTING: 

62 # If we are in testing mode, we want to know about this exception 

63 raise e