mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-08 00:08:49 +00:00
* Add pre-commit to the stack * exclude static * Add locales to excludes * fix style errors * rename pipeline steps * also wait on precommit * make template matching simpler * Use the same code for python setup everywhere * use step and cache for python setup * move regular settings up into general envs * just use full update * Use invoke instead of static references * make setup actions more similar * use python3 * refactor names to be similar * fix runner version * fix references * remove incidential change * use matrix for os * Github can't do this right now * ignore docstyle errors * Add seperate docstring test * update flake call * do not fail on docstring * refactor setup into workflow * update reference * switch to action * resturcture * add bash statements * remove os from cache * update input checks * make code cleaner * fix boolean * no relative paths * install wheel by python * switch to install * revert back to simple wheel * refactor import export tests * move setup keys back to not disturbe tests * remove docstyle till that is fixed * update references * continue on error * add docstring test * use relativ action references * Change step / job docstrings * update to merge * reformat comments 1 * fix docstrings 2 * fix docstrings 3 * fix docstrings 4 * fix docstrings 5 * fix docstrings 6 * fix docstrings 7 * fix docstrings 8 * fix docstirns 9 * fix docstrings 10 * docstring adjustments * update the remaining docstrings * small docstring changes * fix function name * update support files for docstrings * Add missing args to docstrings * Remove outdated function * Add docstrings for the 'build' app * Make API code cleaner * add more docstrings for plugin app * Remove dead code for plugin settings No idea what that was even intended for * ignore __init__ files for docstrings * More docstrings * Update docstrings for the 'part' directory * Fixes for related_part functionality * Fix removed stuff from merge 99676ee * make more consistent * Show statistics for docstrings * add more docstrings * move specific register statements to make them clearer to understant * More docstrings for common * and more docstrings * and more * simpler call * docstrings for notifications * docstrings for common/tests * Add docs for common/models * Revert "move specific register statements to make them clearer to understant" This reverts commit ca9665462202c2d63f34b4fd920013b1457bbb6d. * use typing here * Revert "Make API code cleaner" This reverts commit 24fb68bd3e1ccfea2ee398c9e18afb01eb340fee. * docstring updates for the 'users' app * Add generic Meta info to simple Meta classes * remove unneeded unique_together statements * More simple metas * Remove unnecessary format specifier * Remove extra json format specifiers * Add docstrings for the 'plugin' app * Docstrings for the 'label' app * Add missing docstrings for the 'report' app * Fix build test regression * Fix top-level files * docstrings for InvenTree/InvenTree * reduce unneeded code * add docstrings * and more docstrings * more docstrings * more docstrings for stock * more docstrings * docstrings for order/views * Docstrings for various files in the 'order' app * Docstrings for order/test_api.py * Docstrings for order/serializers.py * Docstrings for order/admin.py * More docstrings for the order app * Add docstrings for the 'company' app * Add unit tests for rebuilding the reference fields * Prune out some more dead code * remove more dead code Co-authored-by: Oliver Walters <oliver.henry.walters@gmail.com>
314 lines
9.0 KiB
Python
314 lines
9.0 KiB
Python
"""Base Class for InvenTree plugins."""
|
|
|
|
import inspect
|
|
import logging
|
|
import os
|
|
import pathlib
|
|
import warnings
|
|
from datetime import datetime
|
|
|
|
from django.conf import settings
|
|
from django.db.utils import OperationalError, ProgrammingError
|
|
from django.urls.base import reverse
|
|
from django.utils.text import slugify
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from plugin.helpers import GitStatus, get_git_log
|
|
|
|
logger = logging.getLogger("inventree")
|
|
|
|
|
|
class MetaBase:
|
|
"""Base class for a plugins metadata."""
|
|
|
|
# Override the plugin name for each concrete plugin instance
|
|
NAME = ''
|
|
SLUG = None
|
|
TITLE = None
|
|
|
|
def get_meta_value(self, key: str, old_key: str = None, __default=None):
|
|
"""Reference a meta item with a key.
|
|
|
|
Args:
|
|
key (str): key for the value
|
|
old_key (str, optional): depreceated key - will throw warning
|
|
__default (optional): Value if nothing with key can be found. Defaults to None.
|
|
|
|
Returns:
|
|
Value referenced with key, old_key or __default if set and not value found
|
|
"""
|
|
value = getattr(self, key, None)
|
|
|
|
# The key was not used
|
|
if old_key and value is None:
|
|
value = getattr(self, old_key, None)
|
|
|
|
# Sound of a warning if old_key worked
|
|
if value:
|
|
warnings.warn(f'Usage of {old_key} was depreciated in 0.7.0 in favour of {key}', DeprecationWarning)
|
|
|
|
# Use __default if still nothing set
|
|
if (value is None) and __default:
|
|
return __default
|
|
return value
|
|
|
|
def plugin_name(self):
|
|
"""Name of plugin."""
|
|
return self.get_meta_value('NAME', 'PLUGIN_NAME')
|
|
|
|
@property
|
|
def name(self):
|
|
"""Name of plugin."""
|
|
return self.plugin_name()
|
|
|
|
def plugin_slug(self):
|
|
"""Slug of plugin.
|
|
|
|
If not set plugin name slugified
|
|
"""
|
|
slug = self.get_meta_value('SLUG', 'PLUGIN_SLUG', None)
|
|
if not slug:
|
|
slug = self.plugin_name()
|
|
|
|
return slugify(slug.lower())
|
|
|
|
@property
|
|
def slug(self):
|
|
"""Slug of plugin."""
|
|
return self.plugin_slug()
|
|
|
|
def plugin_title(self):
|
|
"""Title of plugin."""
|
|
title = self.get_meta_value('TITLE', 'PLUGIN_TITLE', None)
|
|
if title:
|
|
return title
|
|
return self.plugin_name()
|
|
|
|
@property
|
|
def human_name(self):
|
|
"""Human readable name of plugin."""
|
|
return self.plugin_title()
|
|
|
|
def plugin_config(self):
|
|
"""Return the PluginConfig object associated with this plugin."""
|
|
try:
|
|
import plugin.models
|
|
|
|
cfg, _ = plugin.models.PluginConfig.objects.get_or_create(
|
|
key=self.plugin_slug(),
|
|
name=self.plugin_name(),
|
|
)
|
|
except (OperationalError, ProgrammingError):
|
|
cfg = None
|
|
|
|
return cfg
|
|
|
|
def is_active(self):
|
|
"""Return True if this plugin is currently active."""
|
|
cfg = self.plugin_config()
|
|
|
|
if cfg:
|
|
return cfg.active
|
|
else:
|
|
return False # pragma: no cover
|
|
|
|
|
|
class MixinBase:
|
|
"""Base set of mixin functions and mechanisms."""
|
|
|
|
def __init__(self, *args, **kwargs) -> None:
|
|
"""Init sup-parts.
|
|
|
|
Adds state dicts.
|
|
"""
|
|
self._mixinreg = {}
|
|
self._mixins = {}
|
|
super().__init__(*args, **kwargs)
|
|
|
|
def mixin(self, key):
|
|
"""Check if mixin is registered."""
|
|
return key in self._mixins
|
|
|
|
def mixin_enabled(self, key):
|
|
"""Check if mixin is registered, enabled and ready."""
|
|
if self.mixin(key):
|
|
fnc_name = self._mixins.get(key)
|
|
|
|
# Allow for simple case where the mixin is "always" ready
|
|
if fnc_name is True:
|
|
return True
|
|
|
|
return getattr(self, fnc_name, True)
|
|
return False
|
|
|
|
def add_mixin(self, key: str, fnc_enabled=True, cls=None):
|
|
"""Add a mixin to the plugins registry."""
|
|
self._mixins[key] = fnc_enabled
|
|
self.setup_mixin(key, cls=cls)
|
|
|
|
def setup_mixin(self, key, cls=None):
|
|
"""Define mixin details for the current mixin -> provides meta details for all active mixins."""
|
|
# get human name
|
|
human_name = getattr(cls.MixinMeta, 'MIXIN_NAME', key) if cls and hasattr(cls, 'MixinMeta') else key
|
|
|
|
# register
|
|
self._mixinreg[key] = {
|
|
'key': key,
|
|
'human_name': human_name,
|
|
}
|
|
|
|
@property
|
|
def registered_mixins(self, with_base: bool = False):
|
|
"""Get all registered mixins for the plugin."""
|
|
mixins = getattr(self, '_mixinreg', None)
|
|
if mixins:
|
|
# filter out base
|
|
if not with_base and 'base' in mixins:
|
|
del mixins['base']
|
|
# only return dict
|
|
mixins = [a for a in mixins.values()]
|
|
return mixins
|
|
|
|
|
|
class InvenTreePlugin(MixinBase, MetaBase):
|
|
"""The InvenTreePlugin class is used to integrate with 3rd party software.
|
|
|
|
DO NOT USE THIS DIRECTLY, USE plugin.InvenTreePlugin
|
|
"""
|
|
|
|
AUTHOR = None
|
|
DESCRIPTION = None
|
|
PUBLISH_DATE = None
|
|
VERSION = None
|
|
WEBSITE = None
|
|
LICENSE = None
|
|
|
|
def __init__(self):
|
|
"""Init a plugin.
|
|
|
|
Set paths and load metadata.
|
|
"""
|
|
super().__init__()
|
|
self.add_mixin('base')
|
|
self.def_path = inspect.getfile(self.__class__)
|
|
self.path = os.path.dirname(self.def_path)
|
|
|
|
self.define_package()
|
|
|
|
# region properties
|
|
@property
|
|
def description(self):
|
|
"""Description of plugin."""
|
|
description = getattr(self, 'DESCRIPTION', None)
|
|
if not description:
|
|
description = self.plugin_name()
|
|
return description
|
|
|
|
@property
|
|
def author(self):
|
|
"""Author of plugin - either from plugin settings or git."""
|
|
author = getattr(self, 'AUTHOR', None)
|
|
if not author:
|
|
author = self.package.get('author')
|
|
if not author:
|
|
author = _('No author found') # pragma: no cover
|
|
return author
|
|
|
|
@property
|
|
def pub_date(self):
|
|
"""Publishing date of plugin - either from plugin settings or git."""
|
|
pub_date = getattr(self, 'PUBLISH_DATE', None)
|
|
if not pub_date:
|
|
pub_date = self.package.get('date')
|
|
else:
|
|
pub_date = datetime.fromisoformat(str(pub_date))
|
|
if not pub_date:
|
|
pub_date = _('No date found') # pragma: no cover
|
|
return pub_date
|
|
|
|
@property
|
|
def version(self):
|
|
"""Version of plugin."""
|
|
version = getattr(self, 'VERSION', None)
|
|
return version
|
|
|
|
@property
|
|
def website(self):
|
|
"""Website of plugin - if set else None."""
|
|
website = getattr(self, 'WEBSITE', None)
|
|
return website
|
|
|
|
@property
|
|
def license(self):
|
|
"""License of plugin."""
|
|
lic = getattr(self, 'LICENSE', None)
|
|
return lic
|
|
# endregion
|
|
|
|
@property
|
|
def _is_package(self):
|
|
"""Is the plugin delivered as a package."""
|
|
return getattr(self, 'is_package', False)
|
|
|
|
@property
|
|
def is_sample(self):
|
|
"""Is this plugin part of the samples?"""
|
|
path = str(self.package_path)
|
|
return path.startswith('plugin/samples/')
|
|
|
|
@property
|
|
def package_path(self):
|
|
"""Path to the plugin."""
|
|
if self._is_package:
|
|
return self.__module__ # pragma: no cover
|
|
return pathlib.Path(self.def_path).relative_to(settings.BASE_DIR)
|
|
|
|
@property
|
|
def settings_url(self):
|
|
"""URL to the settings panel for this plugin."""
|
|
return f'{reverse("settings")}#select-plugin-{self.slug}'
|
|
|
|
# region package info
|
|
def _get_package_commit(self):
|
|
"""Get last git commit for the plugin."""
|
|
return get_git_log(self.def_path)
|
|
|
|
def _get_package_metadata(self):
|
|
"""Get package metadata for plugin."""
|
|
return {} # pragma: no cover # TODO add usage for package metadata
|
|
|
|
def define_package(self):
|
|
"""Add package info of the plugin into plugins context."""
|
|
package = self._get_package_metadata() if self._is_package else self._get_package_commit()
|
|
|
|
# process date
|
|
if package.get('date'):
|
|
package['date'] = datetime.fromisoformat(package.get('date'))
|
|
|
|
# process sign state
|
|
sign_state = getattr(GitStatus, str(package.get('verified')), GitStatus.N)
|
|
if sign_state.status == 0:
|
|
self.sign_color = 'success' # pragma: no cover
|
|
elif sign_state.status == 1:
|
|
self.sign_color = 'warning'
|
|
else:
|
|
self.sign_color = 'danger' # pragma: no cover
|
|
|
|
# set variables
|
|
self.package = package
|
|
self.sign_state = sign_state
|
|
# endregion
|
|
|
|
|
|
class IntegrationPluginBase(InvenTreePlugin):
|
|
"""Legacy base class for plugins.
|
|
|
|
Do not use!
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
"""Send warning about using this reference."""
|
|
# TODO remove in 0.8.0
|
|
warnings.warn("This import is deprecated - use InvenTreePlugin", DeprecationWarning)
|
|
super().__init__(*args, **kwargs)
|