Coverage for src/backend/InvenTree/plugin/base/locate/mixins.py: 43%

21 statements  

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

1"""Plugin mixin for locating stock items and locations.""" 

2 

3import structlog 

4 

5from plugin import PluginMixinEnum 

6from plugin.helpers import MixinNotImplementedError 

7 

8logger = structlog.get_logger('inventree') 

9 

10 

11class LocateMixin: 

12 """Mixin class which provides support for 'locating' inventory items, for example identifying the location of a particular StockLocation. 

13 

14 Plugins could implement audible or visual cues to direct attention to the location, 

15 with (for e.g.) LED strips or buzzers, or some other method. 

16 

17 The plugins may also be used to *deliver* a particular stock item to the user. 

18 

19 A class which implements this mixin may implement the following methods: 

20 

21 - locate_stock_item : Used to locate / identify a particular stock item 

22 - locate_stock_location : Used to locate / identify a particular stock location 

23 

24 Refer to the default method implementations below for more information! 

25 """ 

26 

27 class MixinMeta: 

28 """Meta for mixin.""" 

29 

30 MIXIN_NAME = 'Locate' 

31 

32 def __init__(self): 

33 """Register the mixin.""" 

34 super().__init__() 

35 self.add_mixin(PluginMixinEnum.LOCATE, True, __class__) 

36 

37 def locate_stock_item(self, item_pk): 

38 """Attempt to locate a particular StockItem. 

39 

40 Arguments: 

41 item_pk: The PK (primary key) of the StockItem to be located 

42 

43 The default implementation for locating a StockItem 

44 attempts to locate the StockLocation where the item is located. 

45 

46 An attempt is only made if the StockItem is *in stock* 

47 

48 Note: A custom implementation could always change this behaviour 

49 """ 

50 logger.info('LocateMixin: Attempting to locate StockItem pk=%s', item_pk) 

51 

52 from stock.models import StockItem 

53 

54 try: 

55 item = StockItem.objects.get(pk=item_pk) 

56 

57 if item.in_stock and item.location is not None: 

58 self.locate_stock_location(item.location.pk) 

59 

60 except StockItem.DoesNotExist: # pragma: no cover 

61 logger.warning('LocateMixin: StockItem pk={item_pk} not found') 

62 

63 def locate_stock_location(self, location_pk): 

64 """Attempt to location a particular StockLocation. 

65 

66 Arguments: 

67 location_pk: The PK (primary key) of the StockLocation to be located 

68 

69 Note: The default implementation here does nothing! 

70 """ 

71 raise MixinNotImplementedError