From 531c39725c26bde6984413bba358912bb21014d4 Mon Sep 17 00:00:00 2001 From: Oliver Date: Wed, 12 Oct 2022 00:16:12 +1100 Subject: [PATCH] Fix for allowing plugins without explicit metadata (#3769) --- InvenTree/plugin/helpers.py | 4 +++- InvenTree/plugin/plugin.py | 15 +++++++++++++-- .../InvenTree/settings/plugin_settings.html | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 89edb1a571..9dcefdb1a1 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -64,6 +64,7 @@ def handle_error(error, do_raise: bool = True, do_log: bool = True, log_name: st """Handles an error and casts it as an IntegrationPluginError.""" package_path = traceback.extract_tb(error.__traceback__)[-1].filename install_path = sysconfig.get_paths()["purelib"] + try: package_name = pathlib.Path(package_path).relative_to(install_path).parts[0] except ValueError: @@ -88,9 +89,10 @@ def handle_error(error, do_raise: bool = True, do_log: bool = True, log_name: st log_error({package_name: str(error)}, **log_kwargs) if do_raise: - # do a straight raise if we are playing with enviroment variables at execution time, ignore the broken sample + # do a straight raise if we are playing with environment variables at execution time, ignore the broken sample if settings.TESTING_ENV and package_name != 'integration.broken_sample' and isinstance(error, IntegrityError): raise error # pragma: no cover + raise IntegrationPluginError(package_name, str(error)) # endregion diff --git a/InvenTree/plugin/plugin.py b/InvenTree/plugin/plugin.py index b59b42bc3b..4388aaaa83 100644 --- a/InvenTree/plugin/plugin.py +++ b/InvenTree/plugin/plugin.py @@ -6,7 +6,7 @@ import os import pathlib import warnings from datetime import datetime -from importlib.metadata import metadata +from importlib.metadata import PackageNotFoundError, metadata from django.conf import settings from django.db.utils import OperationalError, ProgrammingError @@ -294,7 +294,18 @@ class InvenTreePlugin(MixinBase, MetaBase): @classmethod def _get_package_metadata(cls): """Get package metadata for plugin.""" - meta = metadata(cls.__name__) + + # Try simple metadata lookup + try: + meta = metadata(cls.__name__) + # Simple lookup did not work - get data from module + except PackageNotFoundError: + + try: + meta = metadata(cls.__module__.split('.')[0]) + except PackageNotFoundError: + # Not much information we can extract at this point + return {} return { 'author': meta['Author-email'], diff --git a/InvenTree/templates/InvenTree/settings/plugin_settings.html b/InvenTree/templates/InvenTree/settings/plugin_settings.html index 2ba3a401b2..4ed8e945c5 100644 --- a/InvenTree/templates/InvenTree/settings/plugin_settings.html +++ b/InvenTree/templates/InvenTree/settings/plugin_settings.html @@ -7,7 +7,7 @@ {% block heading %} -{% blocktrans with name=plugin.human_name %}Plugin details for {{name}}{% endblocktrans %} +{% trans "Plugin" %}: {{ plugin.human_name }} {% endblock %} {% block content %}