From 86b72549a2cc68a9262e7710e3dfd3e726906ac8 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 11 Oct 2022 23:15:24 +1100 Subject: [PATCH] Plugin load fix (#3768) * Catch potential secondary PackageNotFoundError * Plugin template tweaks --- InvenTree/plugin/helpers.py | 4 +++- InvenTree/plugin/plugin.py | 9 +++++++-- .../templates/InvenTree/settings/plugin_settings.html | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/InvenTree/plugin/helpers.py b/InvenTree/plugin/helpers.py index 7a873f0a90..00344f3df2 100644 --- a/InvenTree/plugin/helpers.py +++ b/InvenTree/plugin/helpers.py @@ -65,6 +65,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: @@ -92,9 +93,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)) diff --git a/InvenTree/plugin/plugin.py b/InvenTree/plugin/plugin.py index d39ab2187c..ece0395092 100644 --- a/InvenTree/plugin/plugin.py +++ b/InvenTree/plugin/plugin.py @@ -340,9 +340,14 @@ class InvenTreePlugin(VersionMixin, MixinBase, MetaBase): # Try simple metadata lookup try: meta = metadata(cls.__name__) - # Simpel lookup did not work - get data from module + # Simple lookup did not work - get data from module except PackageNotFoundError: - meta = metadata(cls.__module__.split('.')[0]) + + 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 0d06ead680..a242d03053 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 %}