Skip to content

Commit

Permalink
WIP template outlet hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Dec 5, 2023
1 parent 81f5914 commit 4a73a74
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 15 deletions.
4 changes: 2 additions & 2 deletions misago/plugins/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def append(self, action: Action):
def prepend(self, action: Action):
self.actions.insert(0, action)

def call(self, *args, **kwargs) -> List[Any]:
def __call__(self, *args, **kwargs) -> List[Any]:
if not self.actions:
return []

Expand Down Expand Up @@ -52,7 +52,7 @@ def reduced_filter(*args, **kwargs):

return reduce(reduce_filter, self.filters, action)

def filter(self, action: Action, *args, **kwargs):
def __call__(self, action: Action, *args, **kwargs):
if self.cache is None:
self.cache = self.get_reduced_action(action)

Expand Down
46 changes: 46 additions & 0 deletions misago/plugins/outlets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from enum import StrEnum
from typing import Any, Dict, List, Protocol

from .hooks import ActionHook


class PluginOutletName(StrEnum):
"""Enum with standard plugin outlets defined by Misago"""

TEST = "TEST"


class PluginOutletHookAction:
def __call__(self, context: dict) -> str | None:
pass


class PluginOutletHook(ActionHook[PluginOutletHookAction]):
def __call__(self, context: dict) -> List[str | None]:
return super().__call__(context)


template_outlets: Dict[str, PluginOutletHook] = {}
for plugin_outlet in PluginOutletName:
template_outlets[plugin_outlet.value] = PluginOutletHook()


def append_template_plugin(
outlet_name: str | PluginOutletName, plugin: PluginOutletHookAction
):
get_outlet(outlet_name).append(plugin)


def prepend_template_plugin(
outlet_name: str | PluginOutletName, plugin: PluginOutletHookAction
):
get_outlet(outlet_name).prepend(plugin)


def get_outlet(outlet_name: str | PluginOutletName) -> PluginOutletHook:
try:
if isinstance(outlet_name, PluginOutletName):
return template_outlets[outlet_name.value]
return template_outlets[outlet_name]
except KeyError as exc:
raise KeyError(f"Unknown template outlet: {outlet_name}") from exc
10 changes: 9 additions & 1 deletion misago/plugins/templatetags/misago_plugins.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
from django import template

from ..outlets import template_outlets

register = template.Library()


@register.simple_tag(takes_context=True)
def pluginoutlet(context, name: str):
content = []
if name not in template_outlets:
return None

content = ""
for plugin_content in template_outlets[name](context):
if plugin_content is not None:
content += plugin_content
return content
12 changes: 6 additions & 6 deletions misago/plugins/tests/test_action_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@


class MockActionHook(ActionHook):
def call_action(self, base: str):
return self.call(base)
def __call__(self, base: str):
return super().__call__(base)


def lowercase_action(base: str) -> str:
Expand All @@ -22,21 +22,21 @@ def hook():


def test_action_hook_without_actions_returns_empty_list(hook):
assert hook.call_action("TeSt") == []
assert hook("TeSt") == []


def test_action_hook_calls_action_and_returns_its_result(hook):
hook.append(lowercase_action)
assert hook.call_action("TeSt") == ["test"]
assert hook("TeSt") == ["test"]


def test_action_hook_calls_multiple_actions_and_returns_their_results(hook):
hook.append(lowercase_action)
hook.append(uppercase_action)
assert hook.call_action("TeSt") == ["test", "TEST"]
assert hook("TeSt") == ["test", "TEST"]


def test_action_hook_action_can_be_prepended_before_other_actions(hook):
hook.append(lowercase_action)
hook.prepend(uppercase_action)
assert hook.call_action("TeSt") == ["TEST", "test"]
assert hook("TeSt") == ["TEST", "test"]
12 changes: 6 additions & 6 deletions misago/plugins/tests/test_filter_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@


class MockFilterHook(FilterHook):
def call_action(self, action):
return self.filter(action, [])
def __call__(self, action):
return super().__call__(action, [])


def action(data):
Expand All @@ -35,21 +35,21 @@ def hook():


def test_filter_hook_without_filters_just_calls_action(hook):
assert hook.call_action(action) == [ACTION]
assert hook(action) == [ACTION]


def test_filter_hook_calls_filter_before_action(hook):
hook.append(first_filter)
assert hook.call_action(action) == [ACTION, FIRST_FILTER]
assert hook(action) == [ACTION, FIRST_FILTER]


def test_filter_hook_calls_filters_in_order_of_adding(hook):
hook.append(first_filter)
hook.append(second_filter)
assert hook.call_action(action) == [ACTION, FIRST_FILTER, SECOND_FILTER]
assert hook(action) == [ACTION, FIRST_FILTER, SECOND_FILTER]


def test_filter_can_be_inserted_before_other_filters(hook):
hook.append(first_filter)
hook.prepend(second_filter)
assert hook.call_action(action) == [ACTION, SECOND_FILTER, FIRST_FILTER]
assert hook(action) == [ACTION, SECOND_FILTER, FIRST_FILTER]
Empty file.

0 comments on commit 4a73a74

Please sign in to comment.