forked from enviPath/enviPy
[Feature] Simple template extension mechanism (#361)
Co-authored-by: Tim Lorsbach <tim@lorsba.ch> Reviewed-on: enviPath/enviPy#361
This commit is contained in:
@ -16,6 +16,10 @@ class EPDBConfig(AppConfig):
|
||||
model_name = getattr(settings, "EPDB_PACKAGE_MODEL", "epdb.Package")
|
||||
logger.info(f"Using Package model: {model_name}")
|
||||
|
||||
from .autodiscovery import autodiscover
|
||||
|
||||
autodiscover()
|
||||
|
||||
if settings.PLUGINS_ENABLED:
|
||||
from bridge.contracts import Property
|
||||
from utilities.plugin import discover_plugins
|
||||
|
||||
5
epdb/autodiscovery.py
Normal file
5
epdb/autodiscovery.py
Normal file
@ -0,0 +1,5 @@
|
||||
from django.utils.module_loading import autodiscover_modules
|
||||
|
||||
|
||||
def autodiscover():
|
||||
autodiscover_modules("epdb_hooks")
|
||||
17
epdb/template_registry.py
Normal file
17
epdb/template_registry.py
Normal file
@ -0,0 +1,17 @@
|
||||
from collections import defaultdict
|
||||
from threading import Lock
|
||||
|
||||
_registry = defaultdict(list)
|
||||
_lock = Lock()
|
||||
|
||||
|
||||
def register_template(slot: str, template_name: str, *, order: int = 100):
|
||||
item = (order, template_name)
|
||||
with _lock:
|
||||
if item not in _registry[slot]:
|
||||
_registry[slot].append(item)
|
||||
_registry[slot].sort(key=lambda x: x[0])
|
||||
|
||||
|
||||
def get_templates(slot: str):
|
||||
return [template_name for _, template_name in _registry.get(slot, [])]
|
||||
@ -2,6 +2,8 @@ from django import template
|
||||
from pydantic import AnyHttpUrl, ValidationError
|
||||
from pydantic.type_adapter import TypeAdapter
|
||||
|
||||
from epdb.template_registry import get_templates
|
||||
|
||||
register = template.Library()
|
||||
|
||||
url_adapter = TypeAdapter(AnyHttpUrl)
|
||||
@ -19,3 +21,8 @@ def is_url(value):
|
||||
return True
|
||||
except ValidationError:
|
||||
return False
|
||||
|
||||
|
||||
@register.simple_tag
|
||||
def epdb_slot_templates(slot):
|
||||
return get_templates(slot)
|
||||
|
||||
Reference in New Issue
Block a user