Coverage for src/backend/InvenTree/plugin/samples/locate/locate_sample.py: 0%

27 statements  

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

1"""Sample plugin for locating stock items / locations. 

2 

3Note: This plugin does not *actually* locate anything! 

4""" 

5 

6import structlog 

7 

8from plugin import InvenTreePlugin 

9from plugin.mixins import LocateMixin 

10 

11logger = structlog.get_logger('inventree') 

12 

13 

14class SampleLocatePlugin(LocateMixin, InvenTreePlugin): 

15 """A very simple example of the 'locate' plugin. 

16 

17 This plugin class simply prints location information to the logger. 

18 """ 

19 

20 NAME = 'SampleLocatePlugin' 

21 SLUG = 'samplelocate' 

22 TITLE = 'Sample plugin for locating items' 

23 

24 VERSION = '0.2' 

25 

26 def locate_stock_item(self, item_pk): 

27 """Locate a StockItem. 

28 

29 Args: 

30 item_pk: primary key for item 

31 """ 

32 from stock.models import StockItem 

33 

34 logger.info('SampleLocatePlugin attempting to locate item ID %s', item_pk) 

35 

36 try: 

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

38 logger.info('StockItem %s located!', item_pk) 

39 

40 # Tag metadata 

41 item.set_metadata('located', True) 

42 

43 except (ValueError, StockItem.DoesNotExist): # pragma: no cover 

44 logger.exception('StockItem ID %s does not exist!', item_pk) 

45 

46 def locate_stock_location(self, location_pk): 

47 """Locate a StockLocation. 

48 

49 Args: 

50 location_pk: primary key for location 

51 """ 

52 from stock.models import StockLocation 

53 

54 logger.info( 

55 'SampleLocatePlugin attempting to locate location ID %s', location_pk 

56 ) 

57 

58 try: 

59 location = StockLocation.objects.get(pk=location_pk) 

60 logger.info("Location exists at '%s'", location.pathstring) 

61 

62 # Tag metadata 

63 location.set_metadata('located', True) 

64 

65 except (ValueError, StockLocation.DoesNotExist): # pragma: no cover 

66 logger.exception('Location ID %s does not exist!', location_pk)