Coverage for src/backend/InvenTree/plugin/base/integration/TransitionMixin.py: 32%

28 statements  

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

1"""Plugin mixin for handling state transitions.""" 

2 

3import structlog 

4 

5from generic.states.transition import TransitionMethod 

6from plugin import PluginMixinEnum 

7 

8logger = structlog.get_logger('inventree') 

9 

10 

11class TransitionMixin: 

12 """Mixin class for handling state transitions. 

13 

14 - This mixin allows plugins to define custom state transition methods. 

15 - These methods can be used to apply custom logic when an object transitions from one state to another. 

16 """ 

17 

18 # List of transition methods that can be used by plugins 

19 TRANSITION_HANDLERS: list[TransitionMethod] 

20 

21 class MixinMeta: 

22 """Meta class for the StateTransitionMixin.""" 

23 

24 MIXIN_NAME = 'StateTransition' 

25 

26 def __init__(self): 

27 """Initialize the mixin and register it.""" 

28 super().__init__() 

29 self.add_mixin( 

30 PluginMixinEnum.STATE_TRANSITION, 'has_transition_handlers', __class__ 

31 ) 

32 

33 @property 

34 def has_transition_handlers(self) -> bool: 

35 """Check if there are any transition handlers defined.""" 

36 try: 

37 result = bool(self.get_transition_handlers()) 

38 return result 

39 except Exception: 

40 return False 

41 

42 def get_transition_handlers(self) -> list[TransitionMethod]: 

43 """Get the list of transition methods available in this mixin. 

44 

45 The default implementation returns the TRANSITION_HANDLERS list. 

46 """ 

47 handlers = getattr(self, 'TRANSITION_HANDLERS', None) or [] 

48 

49 if not isinstance(handlers, list): 

50 raise TypeError( 

51 'INVE-E9: TRANSITION_HANDLERS must be a list of TransitionMethod instances' 

52 ) 

53 

54 handler_methods = [] 

55 

56 for handler in handlers: 

57 if not isinstance(handler, TransitionMethod): 

58 logger.error('INVE-E9: Invalid transition handler type: %s', handler) 

59 continue 

60 

61 handler_methods.append(handler) 

62 

63 return handler_methods