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
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1"""Plugin mixin for handling state transitions."""
3import structlog
5from generic.states.transition import TransitionMethod
6from plugin import PluginMixinEnum
8logger = structlog.get_logger('inventree')
11class TransitionMixin:
12 """Mixin class for handling state transitions.
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 """
18 # List of transition methods that can be used by plugins
19 TRANSITION_HANDLERS: list[TransitionMethod]
21 class MixinMeta:
22 """Meta class for the StateTransitionMixin."""
24 MIXIN_NAME = 'StateTransition'
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 )
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
42 def get_transition_handlers(self) -> list[TransitionMethod]:
43 """Get the list of transition methods available in this mixin.
45 The default implementation returns the TRANSITION_HANDLERS list.
46 """
47 handlers = getattr(self, 'TRANSITION_HANDLERS', None) or []
49 if not isinstance(handlers, list):
50 raise TypeError(
51 'INVE-E9: TRANSITION_HANDLERS must be a list of TransitionMethod instances'
52 )
54 handler_methods = []
56 for handler in handlers:
57 if not isinstance(handler, TransitionMethod):
58 logger.error('INVE-E9: Invalid transition handler type: %s', handler)
59 continue
61 handler_methods.append(handler)
63 return handler_methods