Coverage for src/backend/InvenTree/plugin/base/integration/UrlsMixin.py: 51%

37 statements  

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

1"""Plugin mixin class for UrlsMixin.""" 

2 

3from django.conf import settings 

4 

5import structlog 

6 

7from common.settings import get_global_setting 

8from plugin import PluginMixinEnum 

9from plugin.urls import PLUGIN_BASE 

10 

11logger = structlog.get_logger('inventree') 

12 

13 

14class UrlsMixin: 

15 """Mixin that enables custom URLs for the plugin.""" 

16 

17 class MixinMeta: 

18 """Meta options for this mixin.""" 

19 

20 MIXIN_NAME = 'URLs' 

21 

22 def __init__(self): 

23 """Register mixin.""" 

24 super().__init__() 

25 self.add_mixin(PluginMixinEnum.URLS, 'has_urls', __class__) 

26 self.urls = self.setup_urls() 

27 

28 @classmethod 

29 def _activate_mixin( 

30 cls, 

31 registry, 

32 plugins, 

33 force_reload=False, 

34 full_reload: bool = False, 

35 *args, 

36 **kwargs, 

37 ): 

38 """Activate UrlsMixin plugins - add custom urls . 

39 

40 Args: 

41 registry (PluginRegistry): The registry that should be used 

42 plugins (dict): List of IntegrationPlugins that should be installed 

43 force_reload (bool, optional): Only reload base apps. Defaults to False. 

44 full_reload (bool, optional): Reload everything - including plugin mechanism. Defaults to False. 

45 """ 

46 if settings.PLUGIN_TESTING or get_global_setting('ENABLE_PLUGINS_URL'): 46 ↛ 47line 46 didn't jump to line 47 because the condition on line 46 was never true

47 logger.info('Registering UrlsMixin Plugin') 

48 urls_changed = False 

49 # check whether an activated plugin extends UrlsMixin 

50 for _key, plugin in plugins: 

51 if plugin.mixin_enabled(PluginMixinEnum.URLS): 

52 urls_changed = True 

53 # if apps were changed or force loading base apps -> reload 

54 if urls_changed or force_reload or full_reload: 

55 # update urls - must be last as models must be registered for creating admin routes 

56 registry._update_urls() 

57 

58 def setup_urls(self): 

59 """Setup url endpoints for this plugin.""" 

60 return getattr(self, 'URLS', None) 

61 

62 @property 

63 def base_url(self): 

64 """Base url for this plugin.""" 

65 return f'{PLUGIN_BASE}/{self.slug}/' 

66 

67 @property 

68 def internal_name(self): 

69 """Internal url pattern name.""" 

70 return f'plugin:{self.slug}:' 

71 

72 @property 

73 def urlpatterns(self): 

74 """URL patterns for this plugin.""" 

75 return self.urls or None 

76 

77 @property 

78 def has_urls(self): 

79 """Does this plugin use custom urls.""" 

80 return bool(self.urls)