From d181eedeb9a03342f4485825573bff765d59d83a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Tue, 16 Feb 2016 16:41:45 +0100 Subject: [PATCH 1/2] Updated API for plugins. --- openslides/utils/plugins.py | 46 ++++++++++--------------------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/openslides/utils/plugins.py b/openslides/utils/plugins.py index f2ce9c3c9..f15461281 100644 --- a/openslides/utils/plugins.py +++ b/openslides/utils/plugins.py @@ -3,6 +3,7 @@ import pkgutil import sys from importlib import import_module +from django.apps import apps from django.conf import settings from pkg_resources import iter_entry_points @@ -12,8 +13,6 @@ from openslides.utils.main import ( get_win32_portable_user_data_path, ) -plugins = {} - # Methods to collect plugins. @@ -52,35 +51,14 @@ def collect_plugins(): return collected_plugins -# Methods to retrieve plugins and their metadata. - -def get_plugin(plugin): - """ - Returns the imported module. The plugin argument must be a python dotted - module path. - """ - try: - plugin = plugins[plugin] - except KeyError: - plugins[plugin] = import_module(plugin) - plugin = get_plugin(plugin) - return plugin - +# Methods to retrieve plugin metadata and urlpatterns. def get_plugin_verbose_name(plugin): """ Returns the verbose name of a plugin. The plugin argument must be a python dotted module path. """ - plugin = get_plugin(plugin) - try: - verbose_name = plugin.get_verbose_name() - except AttributeError: - try: - verbose_name = plugin.__verbose_name__ - except AttributeError: - verbose_name = plugin.__name__ - return verbose_name + return apps.get_app_config(plugin).verbose_name def get_plugin_description(plugin): @@ -88,12 +66,12 @@ def get_plugin_description(plugin): Returns the short descrption of a plugin. The plugin argument must be a python dotted module path. """ - plugin = get_plugin(plugin) + plugin_app_config = apps.get_app_config(plugin) try: - description = plugin.get_description() + description = plugin_app_config.get_description() except AttributeError: try: - description = plugin.__description__ + description = plugin_app_config.description except AttributeError: description = '' return description @@ -104,12 +82,12 @@ def get_plugin_version(plugin): Returns the version string of a plugin. The plugin argument must be a python dotted module path. """ - plugin = get_plugin(plugin) + plugin_app_config = apps.get_app_config(plugin) try: - version = plugin.get_version() + version = plugin_app_config.get_version() except AttributeError: try: - version = plugin.__version__ + version = plugin_app_config.version except AttributeError: version = 'unknown' return version @@ -120,12 +98,12 @@ def get_plugin_urlpatterns(plugin): Returns the urlpatterns object for a plugin. The plugin argument must be a python dotted module path. """ - plugin = get_plugin(plugin) + plugin_app_config = apps.get_app_config(plugin) try: - urlpatterns = plugin.get_urlpatterns() + urlpatterns = plugin_app_config.get_urlpatterns() except AttributeError: try: - urlpatterns = plugin.urls.urlpatterns + urlpatterns = plugin_app_config.urlpatterns except AttributeError: urlpatterns = None return urlpatterns From dcc325c716270dc5de483c0c1b0f07c97d7a6576 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Tue, 16 Feb 2016 22:51:38 +0100 Subject: [PATCH 2/2] Fixed test plugin. --- openslides/utils/plugins.py | 1 - tests/integration/core/test_views.py | 4 ++-- tests/integration/test_plugin/__init__.py | 4 ++++ tests/integration/test_plugin/apps.py | 10 ++++++++++ tests/old/utils/__init__.py | 3 +-- tests/old/utils/apps.py | 6 ++++++ tests/settings.py | 2 +- 7 files changed, 24 insertions(+), 6 deletions(-) create mode 100644 tests/integration/test_plugin/__init__.py create mode 100644 tests/integration/test_plugin/apps.py create mode 100644 tests/old/utils/apps.py diff --git a/openslides/utils/plugins.py b/openslides/utils/plugins.py index f15461281..9a23cd0d5 100644 --- a/openslides/utils/plugins.py +++ b/openslides/utils/plugins.py @@ -1,7 +1,6 @@ import os import pkgutil import sys -from importlib import import_module from django.apps import apps from django.conf import settings diff --git a/tests/integration/core/test_views.py b/tests/integration/core/test_views.py index 99d33511d..aa4a7ec55 100644 --- a/tests/integration/core/test_views.py +++ b/tests/integration/core/test_views.py @@ -70,8 +70,8 @@ class VersionView(TestCase): self.assertEqual(json.loads(response.content.decode()), { 'openslides_version': version, 'plugins': [ - {'verbose_name': 'Plugin tests.old.utils', - 'description': 'Description of plugin tests.old.utils', + {'verbose_name': 'OpenSlides Test Plugin', + 'description': 'This is a test plugin for OpenSlides.', 'version': 'unknown'}]}) diff --git a/tests/integration/test_plugin/__init__.py b/tests/integration/test_plugin/__init__.py new file mode 100644 index 000000000..e6e60f0e6 --- /dev/null +++ b/tests/integration/test_plugin/__init__.py @@ -0,0 +1,4 @@ +__verbose_name__ = 'OpenSlides Test Plugin' +__description__ = 'This is a test plugin for OpenSlides.' + +default_app_config = 'tests.integration.test_plugin.apps.TestPluginAppConfig' diff --git a/tests/integration/test_plugin/apps.py b/tests/integration/test_plugin/apps.py new file mode 100644 index 000000000..633d6db79 --- /dev/null +++ b/tests/integration/test_plugin/apps.py @@ -0,0 +1,10 @@ +from django.apps import AppConfig + +from . import __description__, __verbose_name__ + + +class TestPluginAppConfig(AppConfig): + name = 'tests.integration.test_plugin' + label = 'tests.integration.test_plugin' + verbose_name = __verbose_name__ + description = __description__ diff --git a/tests/old/utils/__init__.py b/tests/old/utils/__init__.py index 36b96fbb1..d41142aec 100644 --- a/tests/old/utils/__init__.py +++ b/tests/old/utils/__init__.py @@ -1,2 +1 @@ -__verbose_name__ = 'Plugin tests.old.utils' -__description__ = 'Description of plugin tests.old.utils' +default_app_config = 'tests.old.utils.apps.TestPluginAppConfig' diff --git a/tests/old/utils/apps.py b/tests/old/utils/apps.py new file mode 100644 index 000000000..109973207 --- /dev/null +++ b/tests/old/utils/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class TestPluginAppConfig(AppConfig): + name = 'tests.old.utils' + label = 'tests.old.utils' diff --git a/tests/settings.py b/tests/settings.py index be58c6922..5f1d60e81 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -17,7 +17,7 @@ SECRET_KEY = 'secret' # Add plugins to this list. INSTALLED_PLUGINS += ( - 'tests.old.utils', + 'tests.integration.test_plugin', ) INSTALLED_APPS += INSTALLED_PLUGINS