mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 20:46:47 +00:00
docstrings
This commit is contained in:
parent
3ae84617d0
commit
3ff4ed67c3
@ -19,18 +19,12 @@ class ActionMixin:
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.add_mixin('action', 'has_action', __class__)
|
self.add_mixin('action', 'has_action', __class__)
|
||||||
|
|
||||||
@property
|
|
||||||
def has_action(self):
|
|
||||||
"""
|
|
||||||
Does this plugin have everything needed for an action?
|
|
||||||
"""
|
|
||||||
return True
|
|
||||||
|
|
||||||
def action_name(self):
|
def action_name(self):
|
||||||
"""
|
"""
|
||||||
Return the action name for this plugin.
|
Action name for this plugin.
|
||||||
|
|
||||||
If the ACTION_NAME parameter is empty,
|
If the ACTION_NAME parameter is empty,
|
||||||
look at the PLUGIN_NAME instead.
|
uses the PLUGIN_NAME instead.
|
||||||
"""
|
"""
|
||||||
if self.ACTION_NAME:
|
if self.ACTION_NAME:
|
||||||
return self.ACTION_NAME
|
return self.ACTION_NAME
|
||||||
|
@ -87,6 +87,9 @@ class ScheduleMixin:
|
|||||||
SCHEDULED_TASKS = {}
|
SCHEDULED_TASKS = {}
|
||||||
|
|
||||||
class MixinMeta:
|
class MixinMeta:
|
||||||
|
"""
|
||||||
|
Meta options for this mixin
|
||||||
|
"""
|
||||||
MIXIN_NAME = 'Schedule'
|
MIXIN_NAME = 'Schedule'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -98,6 +101,9 @@ class ScheduleMixin:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def has_scheduled_tasks(self):
|
def has_scheduled_tasks(self):
|
||||||
|
"""
|
||||||
|
Are tasks defined for this plugin
|
||||||
|
"""
|
||||||
return bool(self.scheduled_tasks)
|
return bool(self.scheduled_tasks)
|
||||||
|
|
||||||
def validate_scheduled_tasks(self):
|
def validate_scheduled_tasks(self):
|
||||||
@ -126,11 +132,17 @@ class ScheduleMixin:
|
|||||||
raise MixinImplementationError(f"Task '{key}' is missing 'minutes' parameter")
|
raise MixinImplementationError(f"Task '{key}' is missing 'minutes' parameter")
|
||||||
|
|
||||||
def get_task_name(self, key):
|
def get_task_name(self, key):
|
||||||
|
"""
|
||||||
|
Task name for key
|
||||||
|
"""
|
||||||
# Generate a 'unique' task name
|
# Generate a 'unique' task name
|
||||||
slug = self.plugin_slug()
|
slug = self.plugin_slug()
|
||||||
return f"plugin.{slug}.{key}"
|
return f"plugin.{slug}.{key}"
|
||||||
|
|
||||||
def get_task_names(self):
|
def get_task_names(self):
|
||||||
|
"""
|
||||||
|
All defined task names
|
||||||
|
"""
|
||||||
# Returns a list of all task names associated with this plugin instance
|
# Returns a list of all task names associated with this plugin instance
|
||||||
return [self.get_task_name(key) for key in self.scheduled_tasks.keys()]
|
return [self.get_task_name(key) for key in self.scheduled_tasks.keys()]
|
||||||
|
|
||||||
@ -192,10 +204,17 @@ class EventMixin:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def process_event(self, event, *args, **kwargs):
|
def process_event(self, event, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Function to handle events
|
||||||
|
Must be overridden by plugin
|
||||||
|
"""
|
||||||
# Default implementation does not do anything
|
# Default implementation does not do anything
|
||||||
raise MixinNotImplementedError
|
raise MixinNotImplementedError
|
||||||
|
|
||||||
class MixinMeta:
|
class MixinMeta:
|
||||||
|
"""
|
||||||
|
Meta options for this mixin
|
||||||
|
"""
|
||||||
MIXIN_NAME = 'Events'
|
MIXIN_NAME = 'Events'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -209,6 +228,9 @@ class UrlsMixin:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
class MixinMeta:
|
class MixinMeta:
|
||||||
|
"""
|
||||||
|
Meta options for this mixin
|
||||||
|
"""
|
||||||
MIXIN_NAME = 'URLs'
|
MIXIN_NAME = 'URLs'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -218,28 +240,28 @@ class UrlsMixin:
|
|||||||
|
|
||||||
def setup_urls(self):
|
def setup_urls(self):
|
||||||
"""
|
"""
|
||||||
setup url endpoints for this plugin
|
Setup url endpoints for this plugin
|
||||||
"""
|
"""
|
||||||
return getattr(self, 'URLS', None)
|
return getattr(self, 'URLS', None)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def base_url(self):
|
def base_url(self):
|
||||||
"""
|
"""
|
||||||
returns base url for this plugin
|
Base url for this plugin
|
||||||
"""
|
"""
|
||||||
return f'{PLUGIN_BASE}/{self.slug}/'
|
return f'{PLUGIN_BASE}/{self.slug}/'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def internal_name(self):
|
def internal_name(self):
|
||||||
"""
|
"""
|
||||||
returns the internal url pattern name
|
Internal url pattern name
|
||||||
"""
|
"""
|
||||||
return f'plugin:{self.slug}:'
|
return f'plugin:{self.slug}:'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def urlpatterns(self):
|
def urlpatterns(self):
|
||||||
"""
|
"""
|
||||||
returns the urlpatterns for this plugin
|
Urlpatterns for this plugin
|
||||||
"""
|
"""
|
||||||
if self.has_urls:
|
if self.has_urls:
|
||||||
return url(f'^{self.slug}/', include((self.urls, self.slug)), name=self.slug)
|
return url(f'^{self.slug}/', include((self.urls, self.slug)), name=self.slug)
|
||||||
@ -248,7 +270,7 @@ class UrlsMixin:
|
|||||||
@property
|
@property
|
||||||
def has_urls(self):
|
def has_urls(self):
|
||||||
"""
|
"""
|
||||||
does this plugin use custom urls
|
Does this plugin use custom urls
|
||||||
"""
|
"""
|
||||||
return bool(self.urls)
|
return bool(self.urls)
|
||||||
|
|
||||||
@ -263,7 +285,7 @@ class NavigationMixin:
|
|||||||
|
|
||||||
class MixinMeta:
|
class MixinMeta:
|
||||||
"""
|
"""
|
||||||
meta options for this mixin
|
Meta options for this mixin
|
||||||
"""
|
"""
|
||||||
MIXIN_NAME = 'Navigation Links'
|
MIXIN_NAME = 'Navigation Links'
|
||||||
|
|
||||||
@ -274,7 +296,7 @@ class NavigationMixin:
|
|||||||
|
|
||||||
def setup_navigation(self):
|
def setup_navigation(self):
|
||||||
"""
|
"""
|
||||||
setup navigation links for this plugin
|
Setup navigation links for this plugin
|
||||||
"""
|
"""
|
||||||
nav_links = getattr(self, 'NAVIGATION', None)
|
nav_links = getattr(self, 'NAVIGATION', None)
|
||||||
if nav_links:
|
if nav_links:
|
||||||
@ -287,13 +309,15 @@ class NavigationMixin:
|
|||||||
@property
|
@property
|
||||||
def has_naviation(self):
|
def has_naviation(self):
|
||||||
"""
|
"""
|
||||||
does this plugin define navigation elements
|
Does this plugin define navigation elements
|
||||||
"""
|
"""
|
||||||
return bool(self.navigation)
|
return bool(self.navigation)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def navigation_name(self):
|
def navigation_name(self):
|
||||||
"""name for navigation tab"""
|
"""
|
||||||
|
Name for navigation tab
|
||||||
|
"""
|
||||||
name = getattr(self, 'NAVIGATION_TAB_NAME', None)
|
name = getattr(self, 'NAVIGATION_TAB_NAME', None)
|
||||||
if not name:
|
if not name:
|
||||||
name = self.human_name
|
name = self.human_name
|
||||||
@ -301,7 +325,9 @@ class NavigationMixin:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def navigation_icon(self):
|
def navigation_icon(self):
|
||||||
"""icon for navigation tab"""
|
"""
|
||||||
|
Icon-name for navigation tab
|
||||||
|
"""
|
||||||
return getattr(self, 'NAVIGATION_TAB_ICON', "fas fa-question")
|
return getattr(self, 'NAVIGATION_TAB_ICON', "fas fa-question")
|
||||||
|
|
||||||
|
|
||||||
@ -311,7 +337,9 @@ class AppMixin:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
class MixinMeta:
|
class MixinMeta:
|
||||||
"""meta options for this mixin"""
|
"""m
|
||||||
|
Mta options for this mixin
|
||||||
|
"""
|
||||||
MIXIN_NAME = 'App registration'
|
MIXIN_NAME = 'App registration'
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@ -321,7 +349,7 @@ class AppMixin:
|
|||||||
@property
|
@property
|
||||||
def has_app(self):
|
def has_app(self):
|
||||||
"""
|
"""
|
||||||
this plugin is always an app with this plugin
|
This plugin is always an app with this plugin
|
||||||
"""
|
"""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -10,6 +10,9 @@ from django.conf import settings
|
|||||||
|
|
||||||
# region logging / errors
|
# region logging / errors
|
||||||
def log_plugin_error(error, reference: str = 'general'):
|
def log_plugin_error(error, reference: str = 'general'):
|
||||||
|
"""
|
||||||
|
Log an plugin error
|
||||||
|
"""
|
||||||
from plugin import registry
|
from plugin import registry
|
||||||
|
|
||||||
# make sure the registry is set up
|
# make sure the registry is set up
|
||||||
@ -21,6 +24,9 @@ def log_plugin_error(error, reference: str = 'general'):
|
|||||||
|
|
||||||
|
|
||||||
class IntegrationPluginError(Exception):
|
class IntegrationPluginError(Exception):
|
||||||
|
"""
|
||||||
|
Error that encapsulates another error and adds the path / reference of the raising plugin
|
||||||
|
"""
|
||||||
def __init__(self, path, message):
|
def __init__(self, path, message):
|
||||||
self.path = path
|
self.path = path
|
||||||
self.message = message
|
self.message = message
|
||||||
@ -85,7 +91,9 @@ def handle_plugin_error(error, do_raise: bool = True, do_log: bool = True, do_re
|
|||||||
|
|
||||||
# region git-helpers
|
# region git-helpers
|
||||||
def get_git_log(path):
|
def get_git_log(path):
|
||||||
"""get dict with info of the last commit to file named in path"""
|
"""
|
||||||
|
Get dict with info of the last commit to file named in path
|
||||||
|
"""
|
||||||
path = path.replace(os.path.dirname(settings.BASE_DIR), '')[1:]
|
path = path.replace(os.path.dirname(settings.BASE_DIR), '')[1:]
|
||||||
command = ['git', 'log', '-n', '1', "--pretty=format:'%H%n%aN%n%aE%n%aI%n%f%n%G?%n%GK'", '--follow', '--', path]
|
command = ['git', 'log', '-n', '1', "--pretty=format:'%H%n%aN%n%aE%n%aI%n%f%n%G?%n%GK'", '--follow', '--', path]
|
||||||
try:
|
try:
|
||||||
@ -100,9 +108,13 @@ def get_git_log(path):
|
|||||||
|
|
||||||
|
|
||||||
class GitStatus:
|
class GitStatus:
|
||||||
"""class for resolving git gpg singing state"""
|
"""
|
||||||
|
Class for resolving git gpg singing state
|
||||||
|
"""
|
||||||
class Definition:
|
class Definition:
|
||||||
"""definition of a git gpg sing state"""
|
"""
|
||||||
|
Definition of a git gpg sing state
|
||||||
|
"""
|
||||||
key: str = 'N'
|
key: str = 'N'
|
||||||
status: int = 2
|
status: int = 2
|
||||||
msg: str = ''
|
msg: str = ''
|
||||||
|
@ -20,7 +20,7 @@ logger = logging.getLogger("inventree")
|
|||||||
|
|
||||||
class MixinBase:
|
class MixinBase:
|
||||||
"""
|
"""
|
||||||
General base for mixins
|
Base set of mixin functions and mechanisms
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
@ -87,20 +87,31 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def _is_package(self):
|
def _is_package(self):
|
||||||
|
"""
|
||||||
|
Is the plugin delivered as a package
|
||||||
|
"""
|
||||||
return getattr(self, 'is_package', False)
|
return getattr(self, 'is_package', False)
|
||||||
|
|
||||||
# region properties
|
# region properties
|
||||||
@property
|
@property
|
||||||
def slug(self):
|
def slug(self):
|
||||||
|
"""
|
||||||
|
Slug of plugin
|
||||||
|
"""
|
||||||
return self.plugin_slug()
|
return self.plugin_slug()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
"""
|
||||||
|
Name of plugin
|
||||||
|
"""
|
||||||
return self.plugin_name()
|
return self.plugin_name()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def human_name(self):
|
def human_name(self):
|
||||||
"""human readable name for labels etc."""
|
"""
|
||||||
|
Human readable name of plugin
|
||||||
|
"""
|
||||||
human_name = getattr(self, 'PLUGIN_TITLE', None)
|
human_name = getattr(self, 'PLUGIN_TITLE', None)
|
||||||
if not human_name:
|
if not human_name:
|
||||||
human_name = self.plugin_name()
|
human_name = self.plugin_name()
|
||||||
@ -108,7 +119,9 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def description(self):
|
def description(self):
|
||||||
"""description of plugin"""
|
"""
|
||||||
|
Description of plugin
|
||||||
|
"""
|
||||||
description = getattr(self, 'DESCRIPTION', None)
|
description = getattr(self, 'DESCRIPTION', None)
|
||||||
if not description:
|
if not description:
|
||||||
description = self.plugin_name()
|
description = self.plugin_name()
|
||||||
@ -116,7 +129,9 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def author(self):
|
def author(self):
|
||||||
"""returns author of plugin - either from plugin settings or git"""
|
"""
|
||||||
|
Author of plugin - either from plugin settings or git
|
||||||
|
"""
|
||||||
author = getattr(self, 'AUTHOR', None)
|
author = getattr(self, 'AUTHOR', None)
|
||||||
if not author:
|
if not author:
|
||||||
author = self.package.get('author')
|
author = self.package.get('author')
|
||||||
@ -126,7 +141,9 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def pub_date(self):
|
def pub_date(self):
|
||||||
"""returns publishing date of plugin - either from plugin settings or git"""
|
"""
|
||||||
|
Publishing date of plugin - either from plugin settings or git
|
||||||
|
"""
|
||||||
pub_date = getattr(self, 'PUBLISH_DATE', None)
|
pub_date = getattr(self, 'PUBLISH_DATE', None)
|
||||||
if not pub_date:
|
if not pub_date:
|
||||||
pub_date = self.package.get('date')
|
pub_date = self.package.get('date')
|
||||||
@ -138,42 +155,56 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def version(self):
|
def version(self):
|
||||||
"""returns version of plugin"""
|
"""
|
||||||
|
Version of plugin
|
||||||
|
"""
|
||||||
version = getattr(self, 'VERSION', None)
|
version = getattr(self, 'VERSION', None)
|
||||||
return version
|
return version
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def website(self):
|
def website(self):
|
||||||
"""returns website of plugin"""
|
"""
|
||||||
|
Website of plugin - if set else None
|
||||||
|
"""
|
||||||
website = getattr(self, 'WEBSITE', None)
|
website = getattr(self, 'WEBSITE', None)
|
||||||
return website
|
return website
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def license(self):
|
def license(self):
|
||||||
"""returns license of plugin"""
|
"""
|
||||||
|
License of plugin
|
||||||
|
"""
|
||||||
license = getattr(self, 'LICENSE', None)
|
license = getattr(self, 'LICENSE', None)
|
||||||
return license
|
return license
|
||||||
# endregion
|
# endregion
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def package_path(self):
|
def package_path(self):
|
||||||
"""returns the path to the plugin"""
|
"""
|
||||||
|
Path to the plugin
|
||||||
|
"""
|
||||||
if self._is_package:
|
if self._is_package:
|
||||||
return self.__module__
|
return self.__module__
|
||||||
return pathlib.Path(self.def_path).relative_to(settings.BASE_DIR)
|
return pathlib.Path(self.def_path).relative_to(settings.BASE_DIR)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def settings_url(self):
|
def settings_url(self):
|
||||||
"""returns url to the settings panel"""
|
"""
|
||||||
|
URL to the settings panel for this plugin
|
||||||
|
"""
|
||||||
return f'{reverse("settings")}#select-plugin-{self.slug}'
|
return f'{reverse("settings")}#select-plugin-{self.slug}'
|
||||||
|
|
||||||
# region mixins
|
# region mixins
|
||||||
def mixin(self, key):
|
def mixin(self, key):
|
||||||
"""check if mixin is registered"""
|
"""
|
||||||
|
Check if mixin is registered
|
||||||
|
"""
|
||||||
return key in self._mixins
|
return key in self._mixins
|
||||||
|
|
||||||
def mixin_enabled(self, key):
|
def mixin_enabled(self, key):
|
||||||
"""check if mixin is enabled and ready"""
|
"""
|
||||||
|
Check if mixin is registered, enabled and ready
|
||||||
|
"""
|
||||||
if self.mixin(key):
|
if self.mixin(key):
|
||||||
fnc_name = self._mixins.get(key)
|
fnc_name = self._mixins.get(key)
|
||||||
|
|
||||||
@ -187,15 +218,21 @@ class IntegrationPluginBase(MixinBase, plugin.InvenTreePlugin):
|
|||||||
|
|
||||||
# region package info
|
# region package info
|
||||||
def get_package_commit(self):
|
def get_package_commit(self):
|
||||||
"""get last git commit for plugin"""
|
"""
|
||||||
|
Get last git commit for the plugin
|
||||||
|
"""
|
||||||
return get_git_log(self.def_path)
|
return get_git_log(self.def_path)
|
||||||
|
|
||||||
def get_package_metadata(self):
|
def get_package_metadata(self):
|
||||||
"""get package metadata for plugin"""
|
"""
|
||||||
|
Get package metadata for plugin
|
||||||
|
"""
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
def set_package(self):
|
def set_package(self):
|
||||||
"""add packaging info of the plugins into plugins context"""
|
"""
|
||||||
|
Add package info of the plugin into plugins context
|
||||||
|
"""
|
||||||
package = self.get_package_metadata() if self._is_package else self.get_package_commit()
|
package = self.get_package_metadata() if self._is_package else self.get_package_commit()
|
||||||
|
|
||||||
# process date
|
# process date
|
||||||
|
Loading…
x
Reference in New Issue
Block a user