Skip to content

Commit

Permalink
Experimental Django template loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
pelme committed Jul 25, 2024
1 parent 47165fc commit d75d4d9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
7 changes: 6 additions & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ def django_env() -> None:
import django
from django.conf import settings

settings.configure(TEMPLATES=[{"BACKEND": "django.template.backends.django.DjangoTemplates"}])
settings.configure(
TEMPLATES=[
{"BACKEND": "django.template.backends.django.DjangoTemplates"},
{"BACKEND": "htpy.django.HTPYTemplates", "NAME": "htpy"},
]
)
django.setup()
30 changes: 30 additions & 0 deletions htpy/django.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from collections.abc import Callable
from typing import Any

from django.http import HttpRequest
from django.template import Context, TemplateDoesNotExist

from . import Element, render_node


class _HTPYTemplate:
def __init__(self, func: Callable[[Context | None, HttpRequest | None], Element]) -> None:
self.func = func

def render(self, context: Context | None, request: HttpRequest | None) -> str:
return render_node(self.func(context, request))


class HTPYTemplates:
def __init__(self, config: Any):
pass

def get_template(self, name: str) -> _HTPYTemplate:
from django.utils.module_loading import import_string

try:
func = import_string(name)
except ImportError:
raise TemplateDoesNotExist(name)

return _HTPYTemplate(func)
30 changes: 28 additions & 2 deletions tests/test_django.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from typing import Any

import pytest
from django.forms.utils import ErrorList
from django.template import Context, Template
from django.http import HttpRequest
from django.template import Context, Template, TemplateDoesNotExist
from django.template.loader import render_to_string
from django.utils.html import escape
from django.utils.safestring import SafeString

from htpy import div, li, ul
from htpy import Element, Node, div, li, ul

pytestmark = pytest.mark.usefixtures("django_env")

Expand All @@ -29,3 +33,25 @@ def test_explicit_escape() -> None:
def test_errorlist() -> None:
result = div[ErrorList(["my error"])]
assert str(result) == """<div><ul class="errorlist"><li>my error</li></ul></div>"""


def my_template(context: dict[str, Any], request: HttpRequest | None) -> Element:
return div[f"hey {context['name']}"]


def my_template_fragment(context: dict[str, Any], request: HttpRequest | None) -> Node:
return [div[f"hey {context['name']}"]]


class Test_custom_template_loader:
def test_render(self) -> None:
result = render_to_string(__name__ + ".my_template", {"name": "andreas"})
assert result == "<div>hey andreas</div>"

def test_list_fragment(self) -> None:
result = render_to_string(__name__ + ".my_template_fragment", {"name": "andreas"})
assert result == "<div>hey andreas</div>"

def test_template_does_not_exist(self) -> None:
with pytest.raises(TemplateDoesNotExist):
render_to_string(__name__ + ".fooasdf", {})

0 comments on commit d75d4d9

Please sign in to comment.