-
-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
69 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.