mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
refactor mixin locations
This commit is contained in:
parent
2188025a93
commit
40cf7869d3
0
InvenTree/plugin/builtin/integration/__init__.py
Normal file
0
InvenTree/plugin/builtin/integration/__init__.py
Normal file
168
InvenTree/plugin/builtin/integration/mixins.py
Normal file
168
InvenTree/plugin/builtin/integration/mixins.py
Normal file
@ -0,0 +1,168 @@
|
||||
"""default shpping mixins for IntegrationMixins"""
|
||||
from django.conf import settings
|
||||
from django.conf.urls import url, include
|
||||
|
||||
|
||||
class GlobalSettingsMixin:
|
||||
"""Mixin that enables global settings for the plugin"""
|
||||
class MixinMeta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'Global settings'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.add_mixin('globalsettings', 'has_globalsettings', __class__)
|
||||
self.globalsettings = self.setup_globalsettings()
|
||||
|
||||
def setup_globalsettings(self):
|
||||
"""
|
||||
setup global settings for this plugin
|
||||
"""
|
||||
return getattr(self, 'GLOBALSETTINGS', None)
|
||||
|
||||
@property
|
||||
def has_globalsettings(self):
|
||||
"""
|
||||
does this plugin use custom global settings
|
||||
"""
|
||||
return bool(self.globalsettings)
|
||||
|
||||
@property
|
||||
def globalsettingspatterns(self):
|
||||
"""
|
||||
get patterns for InvenTreeSetting defintion
|
||||
"""
|
||||
if self.has_globalsettings:
|
||||
return {f'PLUGIN_{self.slug.upper()}_{key}': value for key, value in self.globalsettings.items()}
|
||||
return None
|
||||
|
||||
def _globalsetting_name(self, key):
|
||||
"""get global name of setting"""
|
||||
return f'PLUGIN_{self.slug.upper()}_{key}'
|
||||
|
||||
def get_globalsetting(self, key):
|
||||
"""
|
||||
get plugin global setting by key
|
||||
"""
|
||||
from common.models import InvenTreeSetting
|
||||
return InvenTreeSetting.get_setting(self._globalsetting_name(key))
|
||||
|
||||
def set_globalsetting(self, key, value, user):
|
||||
"""
|
||||
set plugin global setting by key
|
||||
"""
|
||||
from common.models import InvenTreeSetting
|
||||
return InvenTreeSetting.set_setting(self._globalsetting_name(key), value, user)
|
||||
|
||||
|
||||
class UrlsMixin:
|
||||
"""Mixin that enables urls for the plugin"""
|
||||
class MixinMeta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'URLs'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.add_mixin('urls', 'has_urls', __class__)
|
||||
self.urls = self.setup_urls()
|
||||
|
||||
def setup_urls(self):
|
||||
"""
|
||||
setup url endpoints for this plugin
|
||||
"""
|
||||
return getattr(self, 'URLS', None)
|
||||
|
||||
@property
|
||||
def base_url(self):
|
||||
"""
|
||||
returns base url for this plugin
|
||||
"""
|
||||
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):
|
||||
"""
|
||||
returns the urlpatterns for this plugin
|
||||
"""
|
||||
if self.has_urls:
|
||||
return url(f'^{self.slug}/', include((self.urls, self.slug)), name=self.slug)
|
||||
return None
|
||||
|
||||
@property
|
||||
def has_urls(self):
|
||||
"""
|
||||
does this plugin use custom urls
|
||||
"""
|
||||
return bool(self.urls)
|
||||
|
||||
|
||||
class NavigationMixin:
|
||||
"""Mixin that enables adding navigation links with the plugin"""
|
||||
NAVIGATION_TAB_NAME = None
|
||||
NAVIGATION_TAB_ICON = "fas fa-question"
|
||||
|
||||
class MixinMeta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'Navigation Links'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.add_mixin('navigation', 'has_naviation', __class__)
|
||||
self.navigation = self.setup_navigation()
|
||||
|
||||
def setup_navigation(self):
|
||||
"""
|
||||
setup navigation links for this plugin
|
||||
"""
|
||||
nav_links = getattr(self, 'NAVIGATION', None)
|
||||
if nav_links:
|
||||
# check if needed values are configured
|
||||
for link in nav_links:
|
||||
if False in [a in link for a in ('link', 'name', )]:
|
||||
raise NotImplementedError('Wrong Link definition', link)
|
||||
return nav_links
|
||||
|
||||
@property
|
||||
def has_naviation(self):
|
||||
"""
|
||||
does this plugin define navigation elements
|
||||
"""
|
||||
return bool(self.navigation)
|
||||
|
||||
@property
|
||||
def navigation_name(self):
|
||||
"""name for navigation tab"""
|
||||
name = getattr(self, 'NAVIGATION_TAB_NAME', None)
|
||||
if not name:
|
||||
name = self.human_name
|
||||
return name
|
||||
|
||||
@property
|
||||
def navigation_icon(self):
|
||||
"""icon for navigation tab"""
|
||||
return getattr(self, 'NAVIGATION_TAB_ICON', "fas fa-question")
|
||||
|
||||
|
||||
class AppMixin:
|
||||
"""Mixin that enables full django app functions for a plugin"""
|
||||
class MixinMeta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'App registration'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.add_mixin('app', 'has_app', __class__)
|
||||
|
||||
@property
|
||||
def has_app(self):
|
||||
"""
|
||||
this plugin is always an app with this plugin
|
||||
"""
|
||||
return True
|
@ -8,7 +8,6 @@ import inspect
|
||||
from datetime import datetime
|
||||
import pathlib
|
||||
|
||||
from django.conf.urls import url, include
|
||||
from django.urls.base import reverse
|
||||
from django.conf import settings
|
||||
from django.utils.text import slugify
|
||||
@ -20,7 +19,6 @@ import plugin.plugin as plugin
|
||||
logger = logging.getLogger("inventree")
|
||||
|
||||
|
||||
# region mixins
|
||||
class MixinBase:
|
||||
"""general base for mixins"""
|
||||
|
||||
@ -57,173 +55,6 @@ class MixinBase:
|
||||
return mixins
|
||||
|
||||
|
||||
class GlobalSettingsMixin:
|
||||
"""Mixin that enables global settings for the plugin"""
|
||||
class MixinMeta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'Global settings'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.add_mixin('globalsettings', 'has_globalsettings', __class__)
|
||||
self.globalsettings = self.setup_globalsettings()
|
||||
|
||||
def setup_globalsettings(self):
|
||||
"""
|
||||
setup global settings for this plugin
|
||||
"""
|
||||
return getattr(self, 'GLOBALSETTINGS', None)
|
||||
|
||||
@property
|
||||
def has_globalsettings(self):
|
||||
"""
|
||||
does this plugin use custom global settings
|
||||
"""
|
||||
return bool(self.globalsettings)
|
||||
|
||||
@property
|
||||
def globalsettingspatterns(self):
|
||||
"""
|
||||
get patterns for InvenTreeSetting defintion
|
||||
"""
|
||||
if self.has_globalsettings:
|
||||
return {f'PLUGIN_{self.slug.upper()}_{key}': value for key, value in self.globalsettings.items()}
|
||||
return None
|
||||
|
||||
def _globalsetting_name(self, key):
|
||||
"""get global name of setting"""
|
||||
return f'PLUGIN_{self.slug.upper()}_{key}'
|
||||
|
||||
def get_globalsetting(self, key):
|
||||
"""
|
||||
get plugin global setting by key
|
||||
"""
|
||||
from common.models import InvenTreeSetting
|
||||
return InvenTreeSetting.get_setting(self._globalsetting_name(key))
|
||||
|
||||
def set_globalsetting(self, key, value, user):
|
||||
"""
|
||||
set plugin global setting by key
|
||||
"""
|
||||
from common.models import InvenTreeSetting
|
||||
return InvenTreeSetting.set_setting(self._globalsetting_name(key), value, user)
|
||||
|
||||
|
||||
class UrlsMixin:
|
||||
"""Mixin that enables urls for the plugin"""
|
||||
class MixinMeta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'URLs'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.add_mixin('urls', 'has_urls', __class__)
|
||||
self.urls = self.setup_urls()
|
||||
|
||||
def setup_urls(self):
|
||||
"""
|
||||
setup url endpoints for this plugin
|
||||
"""
|
||||
return getattr(self, 'URLS', None)
|
||||
|
||||
@property
|
||||
def base_url(self):
|
||||
"""
|
||||
returns base url for this plugin
|
||||
"""
|
||||
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):
|
||||
"""
|
||||
returns the urlpatterns for this plugin
|
||||
"""
|
||||
if self.has_urls:
|
||||
return url(f'^{self.slug}/', include((self.urls, self.slug)), name=self.slug)
|
||||
return None
|
||||
|
||||
@property
|
||||
def has_urls(self):
|
||||
"""
|
||||
does this plugin use custom urls
|
||||
"""
|
||||
return bool(self.urls)
|
||||
|
||||
|
||||
class NavigationMixin:
|
||||
"""Mixin that enables adding navigation links with the plugin"""
|
||||
NAVIGATION_TAB_NAME = None
|
||||
NAVIGATION_TAB_ICON = "fas fa-question"
|
||||
|
||||
class MixinMeta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'Navigation Links'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.add_mixin('navigation', 'has_naviation', __class__)
|
||||
self.navigation = self.setup_navigation()
|
||||
|
||||
def setup_navigation(self):
|
||||
"""
|
||||
setup navigation links for this plugin
|
||||
"""
|
||||
nav_links = getattr(self, 'NAVIGATION', None)
|
||||
if nav_links:
|
||||
# check if needed values are configured
|
||||
for link in nav_links:
|
||||
if False in [a in link for a in ('link', 'name', )]:
|
||||
raise NotImplementedError('Wrong Link definition', link)
|
||||
return nav_links
|
||||
|
||||
@property
|
||||
def has_naviation(self):
|
||||
"""
|
||||
does this plugin define navigation elements
|
||||
"""
|
||||
return bool(self.navigation)
|
||||
|
||||
@property
|
||||
def navigation_name(self):
|
||||
"""name for navigation tab"""
|
||||
name = getattr(self, 'NAVIGATION_TAB_NAME', None)
|
||||
if not name:
|
||||
name = self.human_name
|
||||
return name
|
||||
|
||||
@property
|
||||
def navigation_icon(self):
|
||||
"""icon for navigation tab"""
|
||||
return getattr(self, 'NAVIGATION_TAB_ICON', "fas fa-question")
|
||||
|
||||
|
||||
class AppMixin:
|
||||
"""Mixin that enables full django app functions for a plugin"""
|
||||
class MixinMeta:
|
||||
"""meta options for this mixin"""
|
||||
MIXIN_NAME = 'App registration'
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.add_mixin('app', 'has_app', __class__)
|
||||
|
||||
@property
|
||||
def has_app(self):
|
||||
"""
|
||||
this plugin is always an app with this plugin
|
||||
"""
|
||||
return True
|
||||
|
||||
# endregion
|
||||
|
||||
|
||||
# region git-helpers
|
||||
def get_git_log(path):
|
||||
"""get dict with info of the last commit to file named in path"""
|
||||
|
@ -1,5 +1,6 @@
|
||||
"""sample implementation for IntegrationPlugin"""
|
||||
from plugin.integration import IntegrationPluginBase, UrlsMixin
|
||||
from plugin.integration import IntegrationPluginBase
|
||||
from plugin.builtin.integration.mixins import UrlsMixin
|
||||
|
||||
|
||||
class NoIntegrationPlugin(IntegrationPluginBase):
|
||||
|
@ -1,5 +1,6 @@
|
||||
"""sample implementations for IntegrationPlugin"""
|
||||
from plugin.integration import AppMixin, GlobalSettingsMixin, UrlsMixin, NavigationMixin, IntegrationPluginBase
|
||||
from plugin.integration import IntegrationPluginBase
|
||||
from plugin.builtin.integration.mixins import AppMixin, GlobalSettingsMixin, UrlsMixin, NavigationMixin
|
||||
|
||||
from django.http import HttpResponse
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
@ -7,7 +7,8 @@ from django.contrib.auth import get_user_model
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from plugin.integration import AppMixin, IntegrationPluginBase, GlobalSettingsMixin, UrlsMixin, NavigationMixin
|
||||
from plugin.integration import IntegrationPluginBase
|
||||
from plugin.builtin.integration.mixins import AppMixin, GlobalSettingsMixin, UrlsMixin, NavigationMixin
|
||||
|
||||
|
||||
class BaseMixinDefinition:
|
||||
|
Loading…
Reference in New Issue
Block a user