slugs for plugins

This commit is contained in:
Matthias 2021-10-04 07:59:07 +02:00
parent b416a13cc8
commit 7654d176cb
No known key found for this signature in database
GPG Key ID: F50EF5741D33E076
4 changed files with 23 additions and 9 deletions

View File

@ -667,14 +667,13 @@ INTEGRATION_PLUGIN_LIST = {}
for plugin in inventree_plugins.load_integration_plugins():
plugin = plugin()
plugin_name = plugin.plugin_name()
INTEGRATION_PLUGINS.append(plugin)
INTEGRATION_PLUGIN_LIST[plugin_name] = plugin
INTEGRATION_PLUGIN_LIST[plugin.slug] = plugin
if plugin.mixin_enabled('settings'):
plugin_setting = plugin.settingspatterns
INTEGRATION_PLUGIN_SETTING[plugin_name] = plugin_setting
INTEGRATION_PLUGIN_SETTING[plugin.slug] = plugin_setting
INTEGRATION_PLUGIN_SETTINGS.update(plugin_setting)
if plugin.mixin_enabled('app'):

View File

@ -8,6 +8,7 @@ import inspect
from django.conf.urls import url, include
from django.conf import settings
from django.utils.text import slugify
import plugins.plugin as plugin
@ -82,7 +83,7 @@ class SettingsMixin:
get patterns for InvenTreeSetting defintion
"""
if self.has_settings:
return {f'PLUGIN_{self.plugin_name().upper()}_{key}': value for key, value in self.settings.items()}
return {f'PLUGIN_{self.slug.upper()}_{key}': value for key, value in self.settings.items()}
return None
def get_setting(self, key):
@ -90,7 +91,7 @@ class SettingsMixin:
get plugin setting by key
"""
from common.models import InvenTreeSetting
return InvenTreeSetting.get_setting(f'PLUGIN_{self.PLUGIN_NAME.upper()}_{key}')
return InvenTreeSetting.get_setting(f'PLUGIN_{self.slug.upper()}_{key}')
class UrlsMixin:
@ -115,7 +116,14 @@ class UrlsMixin:
"""
returns base url for this plugin
"""
return f'{settings.PLUGIN_URL}/{self.plugin_name()}/'
return f'{settings.PLUGIN_URL}/{self.slug}/'
@property
def internal_name(self):
"""
returns the internal url pattern name
"""
return f'plugin:{self.slug}:'
@property
def urlpatterns(self):
@ -123,7 +131,7 @@ class UrlsMixin:
returns the urlpatterns for this plugin
"""
if self.has_urls:
return url(f'^{self.plugin_name()}/', include((self.urls, self.plugin_name())), name=self.plugin_name())
return url(f'^{self.slug}/', include((self.urls, self.slug)), name=self.slug)
return None
@property
@ -238,6 +246,12 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
self.set_sign_values()
@property
def slug(self):
"""slug for the plugin"""
name = getattr(self, 'PLUGIN_SLUG', self.plugin_name())
return slugify(name)
def mixin(self, key):
"""check if mixin is registered"""
return key in self._mixins

View File

@ -12,6 +12,7 @@ class SampleIntegrationPlugin(SettingsMixin, UrlsMixin, NavigationMixin, Integra
"""
PLUGIN_NAME = "SampleIntegrationPlugin"
PLUGIN_SLUG= "sample"
def view_test(self, request):
"""very basic view"""
@ -38,5 +39,5 @@ class SampleIntegrationPlugin(SettingsMixin, UrlsMixin, NavigationMixin, Integra
}
NAVIGATION = [
{'name': 'SampleIntegration', 'link': 'plugin:SampleIntegrationPlugin:hi'},
{'name': 'SampleIntegration', 'link': 'plugin:sample:hi'},
]

View File

@ -36,7 +36,7 @@ class SettingsMixinTest(BaseMixinDefinition, TestCase):
self.assertEqual(self.mixin.settings, self.TEST_SETTINGS)
# settings pattern
target_pattern = {f'PLUGIN_{self.mixin.plugin_name().upper()}_{key}': value for key, value in self.mixin.settings.items()}
target_pattern = {f'PLUGIN_{self.mixin.slug.upper()}_{key}': value for key, value in self.mixin.settings.items()}
self.assertEqual(self.mixin.settingspatterns, target_pattern)
# no settings