Verified Commit 3da36273 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

Harmonized plugin_conf / plugins_conf

parent 8a3a42e4
...@@ -140,10 +140,10 @@ def get_all_plugins(): ...@@ -140,10 +140,10 @@ def get_all_plugins():
] ]
def generate_plugin_conf(plugins, user=None): def generate_plugins_conf(plugins, user=None):
from . import models from . import models
plugin_conf = [] plugins_conf = []
qs = models.Plugin.objects.filter(is_enabled=True).values("name", "config") qs = models.Plugin.objects.filter(is_enabled=True).values("name", "config")
by_plugin_name = {obj["name"]: obj["config"] for obj in qs} by_plugin_name = {obj["name"]: obj["config"] for obj in qs}
for plugin in plugins: for plugin in plugins:
...@@ -154,28 +154,28 @@ def generate_plugin_conf(plugins, user=None): ...@@ -154,28 +154,28 @@ def generate_plugin_conf(plugins, user=None):
"user": None, "user": None,
"settings": by_plugin_name[plugin.name] or {}, "settings": by_plugin_name[plugin.name] or {},
} }
plugin_conf.append(conf) plugins_conf.append(conf)
if plugin_conf and user and user.is_authenticated: if plugins_conf and user and user.is_authenticated:
qs = models.UserPlugin.objects.filter( qs = models.UserPlugin.objects.filter(
user=user, plugin__is_enabled=True, is_enabled=True user=user, plugin__is_enabled=True, is_enabled=True
).values("plugin__name", "config") ).values("plugin__name", "config")
by_plugin_name = {obj["plugin__name"]: obj["config"] for obj in qs} by_plugin_name = {obj["plugin__name"]: obj["config"] for obj in qs}
for row in plugin_conf: for row in plugins_conf:
if row["obj"].name in by_plugin_name: if row["obj"].name in by_plugin_name:
row["user"] = { row["user"] = {
"id": user.pk, "id": user.pk,
"settings": by_plugin_name[row["obj"].name], "settings": by_plugin_name[row["obj"].name],
} }
return plugin_conf return plugins_conf
def attach_plugin_conf(obj, user): def attach_plugins_conf(obj, user):
from funkwhale_api.common import preferences from funkwhale_api.common import preferences
plugins_enabled = preferences.get("plugins__enabled") plugins_enabled = preferences.get("plugins__enabled")
if plugins_enabled: if plugins_enabled:
conf = generate_plugin_conf(plugins=get_all_plugins(), user=user) conf = generate_plugins_conf(plugins=get_all_plugins(), user=user)
else: else:
conf = None conf = None
setattr(obj, "plugin_conf", conf) setattr(obj, "plugins_conf", conf)
...@@ -83,7 +83,7 @@ def test_get_all_plugins(mocker): ...@@ -83,7 +83,7 @@ def test_get_all_plugins(mocker):
assert all_plugins == [pl1, pl2] assert all_plugins == [pl1, pl2]
def test_generate_plugin_conf(factories, plugin_class): def test_generate_plugins_conf(factories, plugin_class):
plugin1 = plugin_class("test1", "test1") plugin1 = plugin_class("test1", "test1")
plugin2 = plugin_class("test2", "test2") plugin2 = plugin_class("test2", "test2")
plugin3 = plugin_class("test3", "test3") plugin3 = plugin_class("test3", "test3")
...@@ -122,11 +122,11 @@ def test_generate_plugin_conf(factories, plugin_class): ...@@ -122,11 +122,11 @@ def test_generate_plugin_conf(factories, plugin_class):
{"obj": plugin4, "settings": plugin4_db_conf.config, "user": None}, {"obj": plugin4, "settings": plugin4_db_conf.config, "user": None},
] ]
conf = plugins.generate_plugin_conf([plugin1, plugin2, plugin3, plugin4], user=user) conf = plugins.generate_plugins_conf([plugin1, plugin2, plugin3, plugin4], user=user)
assert conf == expected assert conf == expected
def test_generate_plugin_conf_anonymous_user(factories, plugin_class): def test_generate_plugins_conf_anonymous_user(factories, plugin_class):
plugin1 = plugin_class("test1", "test1") plugin1 = plugin_class("test1", "test1")
plugin2 = plugin_class("test2", "test2") plugin2 = plugin_class("test2", "test2")
plugin3 = plugin_class("test3", "test3") plugin3 = plugin_class("test3", "test3")
...@@ -154,28 +154,28 @@ def test_generate_plugin_conf_anonymous_user(factories, plugin_class): ...@@ -154,28 +154,28 @@ def test_generate_plugin_conf_anonymous_user(factories, plugin_class):
{"obj": plugin4, "settings": plugin4_db_conf.config, "user": None}, {"obj": plugin4, "settings": plugin4_db_conf.config, "user": None},
] ]
conf = plugins.generate_plugin_conf([plugin1, plugin2, plugin3, plugin4], user=None) conf = plugins.generate_plugins_conf([plugin1, plugin2, plugin3, plugin4], user=None)
assert conf == expected assert conf == expected
def test_attach_plugin_conf(mocker): def test_attach_plugins_conf(mocker):
request = mocker.Mock() request = mocker.Mock()
generate_plugin_conf = mocker.patch.object(plugins, "generate_plugin_conf") generate_plugins_conf = mocker.patch.object(plugins, "generate_plugins_conf")
get_all_plugins = mocker.patch.object(plugins, "get_all_plugins") get_all_plugins = mocker.patch.object(plugins, "get_all_plugins")
user = mocker.Mock() user = mocker.Mock()
plugins.attach_plugin_conf(request, user=user) plugins.attach_plugins_conf(request, user=user)
generate_plugin_conf.assert_called_once_with( generate_plugins_conf.assert_called_once_with(
plugins=get_all_plugins.return_value, user=user plugins=get_all_plugins.return_value, user=user
) )
assert request.plugin_conf == generate_plugin_conf.return_value assert request.plugins_conf == generate_plugins_conf.return_value
def test_attach_plugin_noop_if_plugins_disabled(mocker, preferences): def test_attach_plugin_noop_if_plugins_disabled(mocker, preferences):
preferences["plugins__enabled"] = False preferences["plugins__enabled"] = False
request = mocker.Mock() request = mocker.Mock()
plugins.attach_plugin_conf(request, user=None) plugins.attach_plugins_conf(request, user=None)
assert request.plugin_conf is None assert request.plugins_conf is None
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment