Refactor plugins registry

This commit is contained in:
jeremystretch 2022-01-27 13:37:24 -05:00
parent 5fea012eab
commit df95115e2e
5 changed files with 19 additions and 17 deletions

View File

@ -12,10 +12,12 @@ from utilities.choices import ButtonColorChoices
from extras.plugins.utils import import_object from extras.plugins.utils import import_object
# Initialize plugin registry stores # Initialize plugin registry
registry['plugin_template_extensions'] = collections.defaultdict(list) registry['plugins'] = {
registry['plugin_menu_items'] = {} 'template_extensions': collections.defaultdict(list),
registry['plugin_preferences'] = {} 'menu_items': {},
'preferences': {},
}
# #
@ -184,7 +186,7 @@ def register_template_extensions(class_list):
if template_extension.model is None: if template_extension.model is None:
raise TypeError(f"PluginTemplateExtension class {template_extension} does not define a valid model!") raise TypeError(f"PluginTemplateExtension class {template_extension} does not define a valid model!")
registry['plugin_template_extensions'][template_extension.model].append(template_extension) registry['plugins']['template_extensions'][template_extension.model].append(template_extension)
# #
@ -249,7 +251,7 @@ def register_menu_items(section_name, class_list):
if not isinstance(button, PluginMenuButton): if not isinstance(button, PluginMenuButton):
raise TypeError(f"{button} must be an instance of extras.plugins.PluginMenuButton") raise TypeError(f"{button} must be an instance of extras.plugins.PluginMenuButton")
registry['plugin_menu_items'][section_name] = class_list registry['plugins']['menu_items'][section_name] = class_list
# #
@ -260,4 +262,4 @@ def register_user_preferences(plugin_name, preferences):
""" """
Register a list of user preferences defined by a plugin. Register a list of user preferences defined by a plugin.
""" """
registry['plugin_preferences'][plugin_name] = preferences registry['plugins']['preferences'][plugin_name] = preferences

View File

@ -23,7 +23,7 @@ def _get_registered_content(obj, method, template_context):
} }
model_name = obj._meta.label_lower model_name = obj._meta.label_lower
template_extensions = registry['plugin_template_extensions'].get(model_name, []) template_extensions = registry['plugins']['template_extensions'].get(model_name, [])
for template_extension in template_extensions: for template_extension in template_extensions:
# If the class has not overridden the specified method, we can skip it (because we know it # If the class has not overridden the specified method, we can skip it (because we know it

View File

@ -61,8 +61,8 @@ class PluginTest(TestCase):
""" """
Check that plugin MenuItems and MenuButtons are registered. Check that plugin MenuItems and MenuButtons are registered.
""" """
self.assertIn('Dummy plugin', registry['plugin_menu_items']) self.assertIn('Dummy plugin', registry['plugins']['menu_items'])
menu_items = registry['plugin_menu_items']['Dummy plugin'] menu_items = registry['plugins']['menu_items']['Dummy plugin']
self.assertEqual(len(menu_items), 2) self.assertEqual(len(menu_items), 2)
self.assertEqual(len(menu_items[0].buttons), 2) self.assertEqual(len(menu_items[0].buttons), 2)
@ -72,14 +72,14 @@ class PluginTest(TestCase):
""" """
from extras.tests.dummy_plugin.template_content import SiteContent from extras.tests.dummy_plugin.template_content import SiteContent
self.assertIn(SiteContent, registry['plugin_template_extensions']['dcim.site']) self.assertIn(SiteContent, registry['plugins']['template_extensions']['dcim.site'])
def test_user_preferences(self): def test_user_preferences(self):
""" """
Check that plugin UserPreferences are registered. Check that plugin UserPreferences are registered.
""" """
self.assertIn('dummy_plugin', registry['plugin_preferences']) self.assertIn('dummy_plugin', registry['plugins']['preferences'])
user_preferences = registry['plugin_preferences']['dummy_plugin'] user_preferences = registry['plugins']['preferences']['dummy_plugin']
self.assertEqual(type(user_preferences), dict) self.assertEqual(type(user_preferences), dict)
self.assertEqual(list(user_preferences.keys()), ['pref1', 'pref2']) self.assertEqual(list(user_preferences.keys()), ['pref1', 'pref2'])

View File

@ -390,10 +390,10 @@ MENUS = [
# Add plugin menus # Add plugin menus
# #
if registry['plugin_menu_items']: if registry['plugins']['menu_items']:
plugin_menu_groups = [] plugin_menu_groups = []
for plugin_name, items in registry['plugin_menu_items'].items(): for plugin_name, items in registry['plugins']['menu_items'].items():
plugin_menu_groups.append( plugin_menu_groups.append(
MenuGroup( MenuGroup(
label=plugin_name, label=plugin_name,

View File

@ -49,10 +49,10 @@ PREFERENCES = {
} }
# Register plugin preferences # Register plugin preferences
if registry['plugin_preferences']: if registry['plugins']['preferences']:
plugin_preferences = {} plugin_preferences = {}
for plugin_name, preferences in registry['plugin_preferences'].items(): for plugin_name, preferences in registry['plugins']['preferences'].items():
for name, userpreference in preferences.items(): for name, userpreference in preferences.items():
PREFERENCES[f'plugins.{plugin_name}.{name}'] = userpreference PREFERENCES[f'plugins.{plugin_name}.{name}'] = userpreference