mirror of
https://github.com/django-components/django-components.git
synced 2025-08-04 14:28:18 +00:00
refactor: Update docs and tests to use get_template_data() (#1161)
* refactor: update docs and tests to use get_template_data() * refactor: fix linting * docs: add note about difference between the two methods
This commit is contained in:
parent
c69980493d
commit
28b61c1609
69 changed files with 795 additions and 725 deletions
|
@ -23,5 +23,5 @@ class MultFileComponent(Component):
|
|||
kwargs={"variable": "GET"},
|
||||
)
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context) -> Dict[str, Any]:
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
|
|
@ -27,5 +27,5 @@ class RelativeFileComponent(Component):
|
|||
kwargs={"variable": "GET"},
|
||||
)
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context) -> Dict[str, Any]:
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
|
|
@ -32,5 +32,5 @@ class RelativeFileWithPathObjComponent(Component):
|
|||
js = PathObj("relative_file_pathobj.js")
|
||||
css = PathObj("relative_file_pathobj.css")
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context) -> Dict[str, Any]:
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
|
|
@ -29,5 +29,5 @@ class SingleFileComponent(Component):
|
|||
kwargs={"variable": "GET"},
|
||||
)
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context) -> Dict[str, Any]:
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
|
|
@ -12,5 +12,5 @@ class RelativeFileWithPathObjComponent(Component):
|
|||
js = "staticfiles.js"
|
||||
css = "staticfiles.css"
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context) -> Dict[str, Any]:
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
|
|
@ -17,10 +17,13 @@ class SimpleComponent(Component):
|
|||
globalThis.testSimpleComponent = 'kapowww!'
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
class Defaults:
|
||||
variable2 = "default"
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs["variable2"],
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -48,8 +51,8 @@ class SimpleComponentNested(Component):
|
|||
globalThis.testSimpleComponentNested = 'bongo!'
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
||||
class Media:
|
||||
css = ["style.css", "style2.css"]
|
||||
|
@ -72,8 +75,8 @@ class OtherComponent(Component):
|
|||
globalThis.testOtherComponent = 'wowzee!'
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
||||
class Media:
|
||||
css = "style.css"
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Any, Dict, Optional
|
||||
from typing import Optional
|
||||
|
||||
from django_components import Component, register
|
||||
|
||||
|
@ -14,5 +14,5 @@ class AppLvlCompComponent(Component):
|
|||
js = "app_lvl_comp.js"
|
||||
css = "app_lvl_comp.css"
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
from typing import Any, Dict
|
||||
|
||||
from django_components import Component, register
|
||||
|
||||
|
||||
|
@ -12,5 +10,5 @@ class AppLvlCompComponent(Component):
|
|||
js = "app_lvl_comp.js"
|
||||
css = "app_lvl_comp.css"
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
from typing import Any, Dict
|
||||
|
||||
from django_components import Component, register
|
||||
|
||||
|
||||
|
@ -10,5 +8,5 @@ class AppLvlCompComponent(Component):
|
|||
{{ variable }}
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
|
|
@ -144,9 +144,9 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"attrs": attrs,
|
||||
"attrs": kwargs["attrs"],
|
||||
"defaults": {"class": "override-me"},
|
||||
}
|
||||
|
||||
|
@ -172,9 +172,9 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"attrs": attrs,
|
||||
"attrs": kwargs["attrs"],
|
||||
"defaults": {"class": "override-me"},
|
||||
"class": "123 457",
|
||||
}
|
||||
|
@ -197,9 +197,9 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"attrs": attrs,
|
||||
"attrs": kwargs["attrs"],
|
||||
"defaults": {"class": "override-me"},
|
||||
}
|
||||
|
||||
|
@ -225,9 +225,9 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"attrs": attrs,
|
||||
"attrs": kwargs["attrs"],
|
||||
"defaults": {"class": "override-me"},
|
||||
}
|
||||
|
||||
|
@ -253,10 +253,10 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"props": {
|
||||
"attrs": attrs,
|
||||
"attrs": kwargs["attrs"],
|
||||
"defaults": {"class": "override-me"},
|
||||
"class": "added_class",
|
||||
"data-id": 123,
|
||||
|
@ -285,8 +285,8 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
return {"attrs": attrs}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"attrs": kwargs["attrs"]}
|
||||
|
||||
template = Template(self.template_str)
|
||||
rendered = template.render(Context({"class_var": "padding-top-8"}))
|
||||
|
@ -318,8 +318,8 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
return {"attrs": attrs}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"attrs": kwargs["attrs"]}
|
||||
|
||||
template = Template(self.template_str)
|
||||
|
||||
|
@ -346,8 +346,8 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
return {"attrs": attrs}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"attrs": kwargs["attrs"]}
|
||||
|
||||
template = Template(self.template_str)
|
||||
|
||||
|
@ -367,8 +367,8 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
return {"attrs": attrs}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"attrs": kwargs["attrs"]}
|
||||
|
||||
template = Template(self.template_str)
|
||||
rendered = template.render(Context({"class_var": "padding-top-8"}))
|
||||
|
@ -391,8 +391,8 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
return {"attrs": attrs}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"attrs": kwargs["attrs"]}
|
||||
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -421,8 +421,8 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
""" # noqa: E501
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
return {"attrs": attrs}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"attrs": kwargs["attrs"]}
|
||||
|
||||
template = Template(self.template_str)
|
||||
rendered = template.render(Context({"class_var": "padding-top-8"}))
|
||||
|
@ -446,9 +446,9 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"attrs": attrs,
|
||||
"attrs": kwargs["attrs"],
|
||||
"defaults": {"class": "override-me"},
|
||||
}
|
||||
|
||||
|
@ -474,7 +474,7 @@ class TestHtmlAttrs:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self, *args, attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"attrs": None,
|
||||
"defaults": None,
|
||||
|
|
|
@ -4,7 +4,7 @@ For tests focusing on the `component` tag, see `test_templatetags_component.py`
|
|||
"""
|
||||
|
||||
import re
|
||||
from typing import Any, Dict, no_type_check
|
||||
from typing import Dict, no_type_check
|
||||
|
||||
import pytest
|
||||
from django.conf import settings
|
||||
|
@ -48,9 +48,9 @@ class CustomClient(Client):
|
|||
super().__init__(*args, **kwargs)
|
||||
|
||||
|
||||
# TODO_REMOVE_IN_V1 - Superseded by `self.get_template` in v1
|
||||
@djc_test
|
||||
class TestComponentOldTemplateApi:
|
||||
class TestComponentLegacyApi:
|
||||
# TODO_REMOVE_IN_V1 - Superseded by `self.get_template` in v1
|
||||
@djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR)
|
||||
def test_get_template_string(self, components_settings):
|
||||
class SimpleComponent(Component):
|
||||
|
@ -60,6 +60,31 @@ class TestComponentOldTemplateApi:
|
|||
"""
|
||||
return content
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": kwargs.get("variable", None),
|
||||
}
|
||||
|
||||
class Media:
|
||||
css = "style.css"
|
||||
js = "script.js"
|
||||
|
||||
rendered = SimpleComponent.render(kwargs={"variable": "test"})
|
||||
assertHTMLEqual(
|
||||
rendered,
|
||||
"""
|
||||
Variable: <strong data-djc-id-ca1bc3e>test</strong>
|
||||
""",
|
||||
)
|
||||
|
||||
# TODO_REMOVE_IN_V2 - `get_context_data()` was superseded by `self.get_template_data`
|
||||
@djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR)
|
||||
def test_get_context_data(self, components_settings):
|
||||
class SimpleComponent(Component):
|
||||
template = """
|
||||
Variable: <strong>{{ variable }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable=None):
|
||||
return {
|
||||
"variable": variable,
|
||||
|
@ -95,9 +120,9 @@ class TestComponent:
|
|||
Variable: <strong>{{ variable }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable=None):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs.get("variable", None),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -121,9 +146,9 @@ class TestComponent:
|
|||
"""
|
||||
return content
|
||||
|
||||
def get_context_data(self, variable=None):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs.get("variable", None),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -143,9 +168,9 @@ class TestComponent:
|
|||
class SimpleComponent(Component):
|
||||
template_file = "simple_template.html"
|
||||
|
||||
def get_context_data(self, variable=None):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs.get("variable", None),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -165,9 +190,9 @@ class TestComponent:
|
|||
class SimpleComponent(Component):
|
||||
template_name = "simple_template.html"
|
||||
|
||||
def get_context_data(self, variable=None):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs.get("variable", None),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -214,12 +239,12 @@ class TestComponent:
|
|||
@djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR)
|
||||
def test_template_file_dynamic(self, components_settings):
|
||||
class SvgComponent(Component):
|
||||
def get_context_data(self, name, css_class="", title="", **attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"name": name,
|
||||
"css_class": css_class,
|
||||
"title": title,
|
||||
**attrs,
|
||||
"name": kwargs.pop("name", None),
|
||||
"css_class": kwargs.pop("css_class", None),
|
||||
"title": kwargs.pop("title", None),
|
||||
**kwargs,
|
||||
}
|
||||
|
||||
def get_template_name(self, context):
|
||||
|
@ -241,9 +266,9 @@ class TestComponent:
|
|||
@djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR)
|
||||
def test_allows_to_return_template(self, components_settings):
|
||||
class TestComponent(Component):
|
||||
def get_context_data(self, variable, **attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs.pop("variable", None),
|
||||
}
|
||||
|
||||
def get_template(self, context):
|
||||
|
@ -261,7 +286,7 @@ class TestComponent:
|
|||
def test_input(self):
|
||||
class TestComponent(Component):
|
||||
@no_type_check
|
||||
def get_context_data(self, var1, var2, variable, another, **attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
assert self.input.args == [123, "str"]
|
||||
assert self.input.kwargs == {"variable": "test", "another": 1}
|
||||
assert isinstance(self.input.context, Context)
|
||||
|
@ -269,7 +294,7 @@ class TestComponent:
|
|||
assert self.input.slots["my_slot"](Context(), None, None) == "MY_SLOT"
|
||||
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs["variable"],
|
||||
}
|
||||
|
||||
@no_type_check
|
||||
|
@ -312,15 +337,15 @@ class TestComponent:
|
|||
</main>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
data = self.inject("my_provide")
|
||||
data["data1"] # This should raise TypeError
|
||||
return {"data": data}
|
||||
|
||||
@register("provider")
|
||||
class Provider(Component):
|
||||
def get_context_data(self, data: Any) -> Any:
|
||||
return {"data": data}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"data": kwargs["data"]}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -331,8 +356,8 @@ class TestComponent:
|
|||
|
||||
@register("parent")
|
||||
class Parent(Component):
|
||||
def get_context_data(self, data: Any) -> Any:
|
||||
return {"data": data}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"data": kwargs["data"]}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -387,7 +412,7 @@ class TestComponent:
|
|||
class SimpleComponent(Component):
|
||||
template = "Hello"
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return None
|
||||
|
||||
assert SimpleComponent.render() == "Hello"
|
||||
|
@ -412,11 +437,11 @@ class TestComponentRender:
|
|||
{% endslot %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, the_arg2=None, *args, the_kwarg=None, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"the_arg2": the_arg2,
|
||||
"the_kwarg": the_kwarg,
|
||||
"args": args,
|
||||
"the_arg2": args[0] if args else None,
|
||||
"the_kwarg": kwargs.pop("the_kwarg", None),
|
||||
"args": args[1:],
|
||||
"kwargs": kwargs,
|
||||
}
|
||||
|
||||
|
@ -425,7 +450,7 @@ class TestComponentRender:
|
|||
rendered,
|
||||
"""
|
||||
the_arg2: None
|
||||
args: ()
|
||||
args: []
|
||||
the_kwarg: None
|
||||
kwargs: {}
|
||||
---
|
||||
|
@ -456,12 +481,12 @@ class TestComponentRender:
|
|||
{% endslot %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, the_arg, the_arg2=None, *args, the_kwarg, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"the_arg": the_arg,
|
||||
"the_arg2": the_arg2,
|
||||
"the_kwarg": the_kwarg,
|
||||
"args": args,
|
||||
"the_arg": args[0],
|
||||
"the_arg2": args[1],
|
||||
"the_kwarg": kwargs.pop("the_kwarg", None),
|
||||
"args": args[2:],
|
||||
"kwargs": kwargs,
|
||||
}
|
||||
|
||||
|
@ -476,7 +501,7 @@ class TestComponentRender:
|
|||
"""
|
||||
the_arg: one
|
||||
the_arg2: two
|
||||
args: ('three',)
|
||||
args: ['three']
|
||||
the_kwarg: test
|
||||
kwargs: {'kw2': 'ooo'}
|
||||
---
|
||||
|
@ -509,12 +534,12 @@ class TestComponentRender:
|
|||
{% endslot %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, the_arg, the_arg2=None, *args, the_kwarg, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"the_arg": the_arg,
|
||||
"the_arg2": the_arg2,
|
||||
"the_kwarg": the_kwarg,
|
||||
"args": args,
|
||||
"the_arg": args[0],
|
||||
"the_arg2": args[1],
|
||||
"the_kwarg": kwargs.pop("the_kwarg"),
|
||||
"args": args[2:],
|
||||
"kwargs": kwargs,
|
||||
}
|
||||
|
||||
|
@ -531,7 +556,7 @@ class TestComponentRender:
|
|||
"""
|
||||
the_arg: one
|
||||
the_arg2: two
|
||||
args: ('three',)
|
||||
args: ['three']
|
||||
the_kwarg: test
|
||||
kwargs: {'kw2': 'ooo'}
|
||||
---
|
||||
|
@ -580,10 +605,10 @@ class TestComponentRender:
|
|||
{% endslot %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, the_arg, the_kwarg=None, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"the_arg": the_arg,
|
||||
"the_kwarg": the_kwarg,
|
||||
"the_arg": args[0],
|
||||
"the_kwarg": kwargs.pop("the_kwarg", None),
|
||||
"kwargs": kwargs,
|
||||
}
|
||||
|
||||
|
@ -592,7 +617,7 @@ class TestComponentRender:
|
|||
# NOTE: Since the slot has access to the Context object, it should behave
|
||||
# the same way as it does in templates - when in "isolated" mode, then the
|
||||
# slot fill has access only to the "root" context, but not to the data of
|
||||
# get_context_data() of SimpleComponent.
|
||||
# get_template_data() of SimpleComponent.
|
||||
if is_isolated:
|
||||
assert ctx.get("the_arg") is None
|
||||
assert ctx.get("the_kwarg") is None
|
||||
|
@ -725,8 +750,8 @@ class TestComponentRender:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self, *args, how: str, **kwargs):
|
||||
return {"how": how}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"how": kwargs.pop("how")}
|
||||
|
||||
class View(ComponentView):
|
||||
def get(self, request):
|
||||
|
@ -734,7 +759,7 @@ class TestComponentRender:
|
|||
|
||||
return self.component.render_to_response(
|
||||
context=RequestContext(self.request),
|
||||
kwargs=self.component.get_context_data(how=how),
|
||||
kwargs={"how": how},
|
||||
)
|
||||
|
||||
client = CustomClient(urlpatterns=[path("test_thing/", Thing.as_view())])
|
||||
|
@ -869,7 +894,7 @@ class TestComponentRender:
|
|||
class TestComponent(Component):
|
||||
template = "Variable: <strong>{{ id }}</strong>"
|
||||
|
||||
def get_context_data(self, **attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"id": self.id,
|
||||
}
|
||||
|
@ -885,7 +910,7 @@ class TestComponentRender:
|
|||
class TestComponent(Component):
|
||||
template = "Variable: <strong>{{ id }}</strong>"
|
||||
|
||||
def get_context_data(self, **attrs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"id": self.id,
|
||||
}
|
||||
|
@ -923,7 +948,7 @@ class TestComponentHook:
|
|||
{% endcomponent %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"args": args,
|
||||
"kwargs": kwargs,
|
||||
|
@ -943,7 +968,7 @@ class TestComponentHook:
|
|||
assertHTMLEqual(
|
||||
rendered,
|
||||
"""
|
||||
args: ()
|
||||
args: []
|
||||
kwargs: {}
|
||||
---
|
||||
from_on_before: :)
|
||||
|
@ -984,7 +1009,7 @@ class TestComponentHook:
|
|||
{% endcomponent %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"args": args,
|
||||
"kwargs": kwargs,
|
||||
|
@ -1006,7 +1031,7 @@ class TestComponentHook:
|
|||
assertHTMLEqual(
|
||||
captured_content,
|
||||
"""
|
||||
args: ()
|
||||
args: []
|
||||
kwargs: {}
|
||||
---
|
||||
from_on_after:
|
||||
|
@ -1020,7 +1045,7 @@ class TestComponentHook:
|
|||
assertHTMLEqual(
|
||||
rendered,
|
||||
"""
|
||||
args: ()
|
||||
args: []
|
||||
kwargs: {}
|
||||
---
|
||||
from_on_after:
|
||||
|
@ -1060,7 +1085,7 @@ class TestComponentHook:
|
|||
{% endcomponent %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"args": args,
|
||||
"kwargs": kwargs,
|
||||
|
@ -1077,7 +1102,7 @@ class TestComponentHook:
|
|||
assertHTMLEqual(
|
||||
captured_content,
|
||||
"""
|
||||
args: ()
|
||||
args: []
|
||||
kwargs: {}
|
||||
---
|
||||
from_on_before:
|
||||
|
@ -1092,7 +1117,7 @@ class TestComponentHook:
|
|||
rendered,
|
||||
"""
|
||||
Chocolate cookie recipe:
|
||||
args: ()
|
||||
args: []
|
||||
kwargs: {}
|
||||
---
|
||||
from_on_before:
|
||||
|
@ -1131,7 +1156,7 @@ class TestComponentHook:
|
|||
{% endcomponent %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"args": args,
|
||||
"kwargs": kwargs,
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import time
|
||||
from typing import Any
|
||||
|
||||
from django.core.cache import caches
|
||||
from django.template import Template
|
||||
|
@ -33,7 +32,7 @@ class TestComponentCache:
|
|||
class Cache:
|
||||
enabled = True
|
||||
|
||||
def get_context_data(self, **kwargs: Any):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal did_call_get
|
||||
did_call_get = True
|
||||
return {}
|
||||
|
@ -55,7 +54,7 @@ class TestComponentCache:
|
|||
did_call_get = False
|
||||
component.render()
|
||||
|
||||
# get_context_data not called because the cache entry was returned
|
||||
# get_template_data not called because the cache entry was returned
|
||||
assert not did_call_get
|
||||
assert result == "Hello"
|
||||
|
||||
|
@ -68,7 +67,7 @@ class TestComponentCache:
|
|||
class Cache:
|
||||
enabled = False
|
||||
|
||||
def get_context_data(self, **kwargs: Any):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal did_call_get
|
||||
did_call_get = True
|
||||
return {}
|
||||
|
@ -89,7 +88,7 @@ class TestComponentCache:
|
|||
did_call_get = False
|
||||
result = component.render()
|
||||
|
||||
# get_context_data IS called because the cache is NOT used
|
||||
# get_template_data IS called because the cache is NOT used
|
||||
assert did_call_get
|
||||
assert result == "Hello"
|
||||
|
||||
|
@ -151,8 +150,8 @@ class TestComponentCache:
|
|||
class Cache:
|
||||
enabled = True
|
||||
|
||||
def get_context_data(self, input, **kwargs: Any):
|
||||
return {"input": input}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"input": kwargs["input"]}
|
||||
|
||||
component = TestComponent()
|
||||
component.render(
|
||||
|
@ -200,7 +199,7 @@ class TestComponentCache:
|
|||
# Custom hash method for args and kwargs
|
||||
return "custom-args-and-kwargs"
|
||||
|
||||
def get_context_data(self, *args, **kwargs: Any):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
component = TestComponent()
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
from dataclasses import field
|
||||
from typing import Any
|
||||
|
||||
from django.template import Context
|
||||
|
||||
|
@ -25,25 +24,36 @@ class TestComponentDefaults:
|
|||
extra = "extra"
|
||||
fn = lambda: "fn_as_val" # noqa: E731
|
||||
|
||||
def get_context_data(self, arg1: Any, variable: Any, another: Any, **attrs: Any):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal did_call_context
|
||||
did_call_context = True
|
||||
|
||||
# Check that args and slots are NOT affected by the defaults
|
||||
assert self.input.args == [123]
|
||||
assert [*self.input.slots.keys()] == ["my_slot"]
|
||||
assert self.input.slots["my_slot"](Context(), None, None) == "MY_SLOT" # type: ignore[arg-type]
|
||||
|
||||
assert kwargs == {
|
||||
"variable": "test", # User-given
|
||||
"another": 1, # Default because missing
|
||||
"extra": "extra", # Default because `None` was given
|
||||
"fn": self.Defaults.fn, # Default because missing
|
||||
}
|
||||
assert self.input.kwargs == {
|
||||
"variable": "test", # User-given
|
||||
"another": 1, # Default because missing
|
||||
"extra": "extra", # Default because `None` was given
|
||||
"fn": self.Defaults.fn, # Default because missing
|
||||
}
|
||||
|
||||
# Check that args and slots are NOT affected by the defaults
|
||||
assert args == [123]
|
||||
assert [*slots.keys()] == ["my_slot"]
|
||||
assert slots["my_slot"](Context(), None, None) == "MY_SLOT" # type: ignore[arg-type]
|
||||
|
||||
assert self.input.args == [123]
|
||||
assert [*self.input.slots.keys()] == ["my_slot"]
|
||||
assert self.input.slots["my_slot"](Context(), None, None) == "MY_SLOT" # type: ignore[arg-type]
|
||||
|
||||
assert isinstance(self.input.context, Context)
|
||||
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs["variable"],
|
||||
}
|
||||
|
||||
TestComponent.render(
|
||||
|
@ -64,10 +74,14 @@ class TestComponentDefaults:
|
|||
variable = "test"
|
||||
fn = Default(lambda: "fn_as_factory")
|
||||
|
||||
def get_context_data(self, variable: Any, **attrs: Any):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal did_call_context
|
||||
did_call_context = True
|
||||
|
||||
assert kwargs == {
|
||||
"variable": "test", # User-given
|
||||
"fn": "fn_as_factory", # Default because missing
|
||||
}
|
||||
assert self.input.kwargs == {
|
||||
"variable": "test", # User-given
|
||||
"fn": "fn_as_factory", # Default because missing
|
||||
|
@ -75,7 +89,7 @@ class TestComponentDefaults:
|
|||
assert isinstance(self.input.context, Context)
|
||||
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs["variable"],
|
||||
}
|
||||
|
||||
TestComponent.render(
|
||||
|
@ -94,10 +108,15 @@ class TestComponentDefaults:
|
|||
variable = "test"
|
||||
fn = field(default=lambda: "fn_as_factory")
|
||||
|
||||
def get_context_data(self, variable: Any, **attrs: Any):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal did_call_context
|
||||
did_call_context = True
|
||||
|
||||
assert kwargs == {
|
||||
"variable": "test", # User-given
|
||||
# NOTE: NOT a factory, because it was set as `field(default=...)`
|
||||
"fn": self.Defaults.fn.default, # type: ignore[attr-defined]
|
||||
}
|
||||
assert self.input.kwargs == {
|
||||
"variable": "test", # User-given
|
||||
# NOTE: NOT a factory, because it was set as `field(default=...)`
|
||||
|
@ -106,7 +125,7 @@ class TestComponentDefaults:
|
|||
assert isinstance(self.input.context, Context)
|
||||
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs["variable"],
|
||||
}
|
||||
|
||||
TestComponent.render(
|
||||
|
@ -125,10 +144,15 @@ class TestComponentDefaults:
|
|||
variable = "test"
|
||||
fn = field(default_factory=lambda: "fn_as_factory")
|
||||
|
||||
def get_context_data(self, variable: Any, **attrs: Any):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal did_call_context
|
||||
did_call_context = True
|
||||
|
||||
assert kwargs == {
|
||||
"variable": "test", # User-given
|
||||
# NOTE: IS a factory, because it was set as `field(default_factory=...)`
|
||||
"fn": "fn_as_factory", # Default because missing
|
||||
}
|
||||
assert self.input.kwargs == {
|
||||
"variable": "test", # User-given
|
||||
# NOTE: IS a factory, because it was set as `field(default_factory=...)`
|
||||
|
@ -137,7 +161,7 @@ class TestComponentDefaults:
|
|||
assert isinstance(self.input.context, Context)
|
||||
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs["variable"],
|
||||
}
|
||||
|
||||
TestComponent.render(
|
||||
|
|
|
@ -132,9 +132,9 @@ class TestMainMedia:
|
|||
css_file = "style.css"
|
||||
js_file = "script.js"
|
||||
|
||||
def get_context_data(self, variable):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs["variable"],
|
||||
}
|
||||
|
||||
registry.register("test", TestComponent)
|
||||
|
@ -231,10 +231,10 @@ class TestMainMedia:
|
|||
Var2 (uppercased): <strong>{{ var2|upper }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, var1=None, var2=None):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"var1": var1,
|
||||
"var2": var2,
|
||||
"var1": kwargs["var1"],
|
||||
"var2": kwargs["var2"],
|
||||
}
|
||||
|
||||
rendered = FilteredComponent.render(kwargs={"var1": "test1", "var2": "test2"})
|
||||
|
@ -871,7 +871,7 @@ class TestMediaRelativePath:
|
|||
</div>
|
||||
""" # noqa
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"shadowing_variable": "NOT SHADOWED"}
|
||||
|
||||
class VariableDisplay(Component):
|
||||
|
@ -881,12 +881,12 @@ class TestMediaRelativePath:
|
|||
<h1>Uniquely named variable = {{ unique_variable }}</h1>
|
||||
"""
|
||||
|
||||
def get_context_data(self, shadowing_variable=None, new_variable=None):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
context = {}
|
||||
if shadowing_variable is not None:
|
||||
context["shadowing_variable"] = shadowing_variable
|
||||
if new_variable is not None:
|
||||
context["unique_variable"] = new_variable
|
||||
if kwargs["shadowing_variable"] is not None:
|
||||
context["shadowing_variable"] = kwargs["shadowing_variable"]
|
||||
if kwargs["new_variable"] is not None:
|
||||
context["unique_variable"] = kwargs["new_variable"]
|
||||
return context
|
||||
|
||||
# Settings required for autodiscover to work
|
||||
|
|
|
@ -142,7 +142,7 @@ class TestComponentTyping:
|
|||
css_data_instance = None
|
||||
|
||||
class Button(Component):
|
||||
# Data returned from `get_context_data`
|
||||
# Data returned from `get_template_data`
|
||||
@dataclass
|
||||
class TemplateData:
|
||||
data1: str
|
||||
|
@ -232,7 +232,7 @@ class TestComponentTyping:
|
|||
css_data_instance = None
|
||||
|
||||
class Button(Component):
|
||||
# Data returned from `get_context_data`
|
||||
# Data returned from `get_template_data`
|
||||
@dataclass
|
||||
class TemplateData:
|
||||
data1: str
|
||||
|
@ -348,7 +348,7 @@ class TestComponentTyping:
|
|||
# The generic specifies the data available to the slot function
|
||||
footer: NotRequired[Slot[ButtonFooterSlotData]]
|
||||
|
||||
# Data returned from `get_context_data`
|
||||
# Data returned from `get_template_data`
|
||||
class TemplateData(NamedTuple):
|
||||
data1: str
|
||||
data2: int
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Any, Dict
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from django.conf import settings
|
||||
|
@ -60,8 +60,8 @@ class TestComponentAsView(SimpleTestCase):
|
|||
</form>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
||||
def render_template_view(request):
|
||||
template = Template(
|
||||
|
@ -90,8 +90,8 @@ class TestComponentAsView(SimpleTestCase):
|
|||
</form>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
return {"inner_var": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"inner_var": kwargs["variable"]}
|
||||
|
||||
class View(ComponentView):
|
||||
def get(self, request, *args, **kwargs) -> HttpResponse:
|
||||
|
@ -115,8 +115,8 @@ class TestComponentAsView(SimpleTestCase):
|
|||
</form>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
return {"inner_var": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"inner_var": kwargs["variable"]}
|
||||
|
||||
def get(self, request, *args, **kwargs) -> HttpResponse:
|
||||
return self.render_to_response(kwargs={"variable": "GET"})
|
||||
|
@ -139,8 +139,8 @@ class TestComponentAsView(SimpleTestCase):
|
|||
</form>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
return {"inner_var": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"inner_var": kwargs["variable"]}
|
||||
|
||||
class View(ComponentView):
|
||||
def post(self, request, *args, **kwargs) -> HttpResponse:
|
||||
|
@ -165,8 +165,8 @@ class TestComponentAsView(SimpleTestCase):
|
|||
</form>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
return {"inner_var": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"inner_var": kwargs["variable"]}
|
||||
|
||||
def post(self, request, *args, **kwargs) -> HttpResponse:
|
||||
variable = request.POST.get("variable")
|
||||
|
@ -188,8 +188,8 @@ class TestComponentAsView(SimpleTestCase):
|
|||
</form>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
return {"inner_var": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"inner_var": kwargs["variable"]}
|
||||
|
||||
def get(self, request, *args, **kwargs) -> HttpResponse:
|
||||
return self.render_to_response(kwargs={"variable": self.name})
|
||||
|
|
|
@ -24,8 +24,8 @@ class SimpleComponent(Component):
|
|||
Variable: <strong>{{ variable }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable=None):
|
||||
return {"variable": variable} if variable is not None else {}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"variable": kwargs.get("variable", None)} if "variable" in kwargs else {}
|
||||
|
||||
|
||||
class VariableDisplay(Component):
|
||||
|
@ -35,12 +35,12 @@ class VariableDisplay(Component):
|
|||
<h1>Uniquely named variable = {{ unique_variable }}</h1>
|
||||
"""
|
||||
|
||||
def get_context_data(self, shadowing_variable=None, new_variable=None):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
context = {}
|
||||
if shadowing_variable is not None:
|
||||
context["shadowing_variable"] = shadowing_variable
|
||||
if new_variable is not None:
|
||||
context["unique_variable"] = new_variable
|
||||
if kwargs["shadowing_variable"] is not None:
|
||||
context["shadowing_variable"] = kwargs["shadowing_variable"]
|
||||
if kwargs["new_variable"] is not None:
|
||||
context["unique_variable"] = kwargs["new_variable"]
|
||||
return context
|
||||
|
||||
|
||||
|
@ -55,8 +55,8 @@ class IncrementerComponent(Component):
|
|||
super().__init__(*args, **kwargs)
|
||||
self.call_count = 0
|
||||
|
||||
def get_context_data(self, value=0):
|
||||
value = int(value)
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
value = int(kwargs.get("value", 0))
|
||||
if hasattr(self, "call_count"):
|
||||
self.call_count += 1
|
||||
else:
|
||||
|
@ -88,7 +88,7 @@ class TestContext:
|
|||
</div>
|
||||
""" # noqa
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"shadowing_variable": "NOT SHADOWED"}
|
||||
|
||||
@djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR)
|
||||
|
@ -231,8 +231,8 @@ class TestParentArgs:
|
|||
</div>
|
||||
""" # noqa
|
||||
|
||||
def get_context_data(self, parent_value):
|
||||
return {"inner_parent_value": parent_value}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"inner_parent_value": kwargs["parent_value"]}
|
||||
|
||||
@djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR)
|
||||
def test_parent_args_can_be_drawn_from_context(self, components_settings):
|
||||
|
@ -463,7 +463,7 @@ class TestIsolatedContext:
|
|||
registry.register(name="simple_component", component=SimpleComponent)
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
{% component 'simple_component' variable only %}{% endcomponent %}
|
||||
{% component 'simple_component' variable=variable only %}{% endcomponent %}
|
||||
"""
|
||||
template = Template(template_str)
|
||||
rendered = template.render(Context({"variable": "outer_value"})).strip()
|
||||
|
@ -490,7 +490,7 @@ class TestIsolatedContextSetting:
|
|||
registry.register(name="simple_component", component=SimpleComponent)
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
{% component 'simple_component' variable %}{% endcomponent %}
|
||||
{% component 'simple_component' variable=variable %}{% endcomponent %}
|
||||
"""
|
||||
template = Template(template_str)
|
||||
rendered = template.render(Context({"variable": "outer_value"}))
|
||||
|
@ -516,7 +516,7 @@ class TestIsolatedContextSetting:
|
|||
registry.register(name="simple_component", component=SimpleComponent)
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
{% component 'simple_component' variable %}
|
||||
{% component 'simple_component' variable=variable %}
|
||||
{% endcomponent %}
|
||||
"""
|
||||
template = Template(template_str)
|
||||
|
@ -549,7 +549,7 @@ class TestContextProcessors:
|
|||
class TestComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal inner_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -585,7 +585,7 @@ class TestContextProcessors:
|
|||
{% component "test_child" / %}
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal parent_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -596,7 +596,7 @@ class TestContextProcessors:
|
|||
class TestChildComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data_child
|
||||
nonlocal child_request
|
||||
context_processors_data_child = self.context_processors_data
|
||||
|
@ -635,7 +635,7 @@ class TestContextProcessors:
|
|||
{% endcomponent %}
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal parent_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -646,7 +646,7 @@ class TestContextProcessors:
|
|||
class TestChildComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data_child
|
||||
nonlocal child_request
|
||||
context_processors_data_child = self.context_processors_data
|
||||
|
@ -680,7 +680,7 @@ class TestContextProcessors:
|
|||
class TestComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal inner_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -709,7 +709,7 @@ class TestContextProcessors:
|
|||
{% component "test_child" / %}
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal parent_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -720,7 +720,7 @@ class TestContextProcessors:
|
|||
class TestChildComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data_child
|
||||
nonlocal child_request
|
||||
context_processors_data_child = self.context_processors_data
|
||||
|
@ -746,7 +746,7 @@ class TestContextProcessors:
|
|||
class TestComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal inner_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -774,7 +774,7 @@ class TestContextProcessors:
|
|||
{% component "test_child" / %}
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal parent_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -785,7 +785,7 @@ class TestContextProcessors:
|
|||
class TestChildComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data_child
|
||||
nonlocal child_request
|
||||
context_processors_data_child = self.context_processors_data
|
||||
|
@ -811,7 +811,7 @@ class TestContextProcessors:
|
|||
class TestComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal inner_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -834,7 +834,7 @@ class TestContextProcessors:
|
|||
class TestComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal inner_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -857,7 +857,7 @@ class TestContextProcessors:
|
|||
class TestComponent(Component):
|
||||
template: types.django_html = """{% csrf_token %}"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal context_processors_data
|
||||
nonlocal inner_request
|
||||
context_processors_data = self.context_processors_data
|
||||
|
@ -902,7 +902,7 @@ class TestOuterContextProperty:
|
|||
Variable: <strong>{{ variable }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return self.outer_context.flatten() # type: ignore[union-attr]
|
||||
|
||||
template_str: types.django_html = """
|
||||
|
|
|
@ -38,10 +38,10 @@ class SimpleComponent(Component):
|
|||
console.log("xyz");
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -551,7 +551,7 @@ class TestDependenciesStrategySimple:
|
|||
console.log("Hello");
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
class Media:
|
||||
|
@ -573,7 +573,7 @@ class TestDependenciesStrategySimple:
|
|||
console.log("xyz");
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
class Media:
|
||||
|
@ -711,7 +711,7 @@ class TestDependenciesStrategyPrepend:
|
|||
console.log("Hello");
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
class Media:
|
||||
|
@ -733,7 +733,7 @@ class TestDependenciesStrategyPrepend:
|
|||
console.log("xyz");
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
class Media:
|
||||
|
@ -868,7 +868,7 @@ class TestDependenciesStrategyAppend:
|
|||
console.log("Hello");
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
class Media:
|
||||
|
@ -890,7 +890,7 @@ class TestDependenciesStrategyAppend:
|
|||
console.log("xyz");
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
class Media:
|
||||
|
|
|
@ -25,10 +25,10 @@ class SimpleComponent(Component):
|
|||
Variable: <strong>{{ variable }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -55,7 +55,7 @@ class SimpleComponentNested(Component):
|
|||
console.log("Hello");
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
class Media:
|
||||
|
@ -78,7 +78,7 @@ class OtherComponent(Component):
|
|||
console.log("xyz");
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
class Media:
|
||||
|
@ -91,7 +91,7 @@ class SimpleComponentWithSharedDependency(Component):
|
|||
Variable: <strong>{{ variable }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
class Media:
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
"""Catch-all for tests that use template tags and don't fit other files"""
|
||||
|
||||
import re
|
||||
from typing import Any, Dict
|
||||
from typing import Dict
|
||||
|
||||
import pytest
|
||||
from django.template import Context, Template, TemplateSyntaxError
|
||||
|
@ -68,21 +68,15 @@ class TestDynamicExpr:
|
|||
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(
|
||||
self,
|
||||
pos_var1: Any,
|
||||
*args: Any,
|
||||
bool_var: bool,
|
||||
list_var: Dict,
|
||||
):
|
||||
captured["pos_var1"] = pos_var1
|
||||
captured["bool_var"] = bool_var
|
||||
captured["list_var"] = list_var
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
captured["pos_var1"] = args[0]
|
||||
captured["bool_var"] = kwargs["bool_var"]
|
||||
captured["list_var"] = kwargs["list_var"]
|
||||
|
||||
return {
|
||||
"pos_var1": pos_var1,
|
||||
"bool_var": bool_var,
|
||||
"list_var": list_var,
|
||||
"pos_var1": args[0],
|
||||
"bool_var": kwargs["bool_var"],
|
||||
"list_var": kwargs["list_var"],
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
|
@ -135,24 +129,17 @@ class TestDynamicExpr:
|
|||
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(
|
||||
self,
|
||||
pos_var1: Any,
|
||||
*args: Any,
|
||||
bool_var: bool,
|
||||
list_var: Dict,
|
||||
dict_var: Dict,
|
||||
):
|
||||
captured["pos_var1"] = pos_var1
|
||||
captured["bool_var"] = bool_var
|
||||
captured["list_var"] = list_var
|
||||
captured["dict_var"] = dict_var
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
captured["pos_var1"] = args[0]
|
||||
captured["bool_var"] = kwargs["bool_var"]
|
||||
captured["list_var"] = kwargs["list_var"]
|
||||
captured["dict_var"] = kwargs["dict_var"]
|
||||
|
||||
return {
|
||||
"pos_var1": pos_var1,
|
||||
"bool_var": bool_var,
|
||||
"list_var": list_var,
|
||||
"dict_var": dict_var,
|
||||
"pos_var1": args[0],
|
||||
"bool_var": kwargs["bool_var"],
|
||||
"list_var": kwargs["list_var"],
|
||||
"dict_var": kwargs["dict_var"],
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
|
@ -209,24 +196,17 @@ class TestDynamicExpr:
|
|||
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(
|
||||
self,
|
||||
pos_var1: Any,
|
||||
pos_var2: Any,
|
||||
*args: Any,
|
||||
bool_var: bool,
|
||||
list_var: Dict,
|
||||
):
|
||||
captured["pos_var1"] = pos_var1
|
||||
captured["pos_var2"] = pos_var2
|
||||
captured["bool_var"] = bool_var
|
||||
captured["list_var"] = list_var
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
captured["pos_var1"] = args[0]
|
||||
captured["pos_var2"] = args[1]
|
||||
captured["bool_var"] = kwargs["bool_var"]
|
||||
captured["list_var"] = kwargs["list_var"]
|
||||
|
||||
return {
|
||||
"pos_var1": pos_var1,
|
||||
"pos_var2": pos_var2,
|
||||
"bool_var": bool_var,
|
||||
"list_var": list_var,
|
||||
"pos_var1": args[0],
|
||||
"pos_var2": args[1],
|
||||
"bool_var": kwargs["bool_var"],
|
||||
"list_var": kwargs["list_var"],
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
|
@ -281,26 +261,18 @@ class TestDynamicExpr:
|
|||
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(
|
||||
self,
|
||||
pos_var1: Any,
|
||||
pos_var2: Any,
|
||||
*args: Any,
|
||||
bool_var: bool,
|
||||
list_var: Dict,
|
||||
dict_var: Dict,
|
||||
):
|
||||
captured["pos_var1"] = pos_var1
|
||||
captured["bool_var"] = bool_var
|
||||
captured["list_var"] = list_var
|
||||
captured["dict_var"] = dict_var
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
captured["pos_var1"] = args[0]
|
||||
captured["bool_var"] = kwargs["bool_var"]
|
||||
captured["list_var"] = kwargs["list_var"]
|
||||
captured["dict_var"] = kwargs["dict_var"]
|
||||
|
||||
return {
|
||||
"pos_var1": pos_var1,
|
||||
"pos_var2": pos_var2,
|
||||
"bool_var": bool_var,
|
||||
"list_var": list_var,
|
||||
"dict_var": dict_var,
|
||||
"pos_var1": args[0],
|
||||
"pos_var2": args[1],
|
||||
"bool_var": kwargs["bool_var"],
|
||||
"list_var": kwargs["list_var"],
|
||||
"dict_var": kwargs["dict_var"],
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
|
@ -357,17 +329,11 @@ class TestDynamicExpr:
|
|||
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(
|
||||
self,
|
||||
pos_var1: Any,
|
||||
pos_var2: Any,
|
||||
*args: Any,
|
||||
bool_var: bool,
|
||||
):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"pos_var1": pos_var1,
|
||||
"pos_var2": pos_var2,
|
||||
"bool_var": bool_var,
|
||||
"pos_var1": args[0],
|
||||
"pos_var2": args[1],
|
||||
"bool_var": kwargs["bool_var"],
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
|
@ -404,15 +370,10 @@ class TestDynamicExpr:
|
|||
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(
|
||||
self,
|
||||
pos_var1: Any,
|
||||
*args: Any,
|
||||
bool_var: bool,
|
||||
):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"pos_var1": pos_var1,
|
||||
"bool_var": bool_var,
|
||||
"pos_var1": args[0],
|
||||
"bool_var": kwargs["bool_var"],
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
|
@ -461,17 +422,12 @@ class TestSpreadOperator:
|
|||
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(
|
||||
self,
|
||||
pos_var1: Any,
|
||||
*args: Any,
|
||||
**kwargs: Any,
|
||||
):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal captured
|
||||
captured = kwargs
|
||||
|
||||
return {
|
||||
"pos_var1": pos_var1,
|
||||
"pos_var1": args[0],
|
||||
**kwargs,
|
||||
}
|
||||
|
||||
|
@ -531,7 +487,7 @@ class TestSpreadOperator:
|
|||
def test_slot(self, components_settings):
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"my_dict": {
|
||||
"attrs:@click": "() => {}",
|
||||
|
@ -568,7 +524,7 @@ class TestSpreadOperator:
|
|||
def test_fill(self, components_settings):
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"my_dict": {
|
||||
"attrs:@click": "() => {}",
|
||||
|
@ -618,7 +574,7 @@ class TestSpreadOperator:
|
|||
def test_provide(self, components_settings):
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
data = self.inject("test")
|
||||
return {
|
||||
"attrs": data.attrs,
|
||||
|
@ -692,11 +648,7 @@ class TestSpreadOperator:
|
|||
def test_later_spreads_do_not_overwrite_earlier(self, components_settings):
|
||||
@register("test")
|
||||
class SimpleComponent(Component):
|
||||
def get_context_data(
|
||||
self,
|
||||
*args: Any,
|
||||
**kwargs: Any,
|
||||
):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
**kwargs,
|
||||
}
|
||||
|
@ -794,7 +746,7 @@ class TestSpreadOperator:
|
|||
class SimpleComponent(Component):
|
||||
template = ""
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal captured
|
||||
captured = args, kwargs
|
||||
return {}
|
||||
|
@ -820,7 +772,7 @@ class TestSpreadOperator:
|
|||
template.render(context)
|
||||
|
||||
assert captured == (
|
||||
("a", "b", "c", 1, 2, 3),
|
||||
["a", "b", "c", 1, 2, 3],
|
||||
{},
|
||||
)
|
||||
|
||||
|
@ -858,7 +810,7 @@ class TestAggregateKwargs:
|
|||
class Test(Component):
|
||||
template = ""
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal captured
|
||||
captured = args, kwargs
|
||||
return {}
|
||||
|
@ -879,7 +831,7 @@ class TestAggregateKwargs:
|
|||
template.render(Context({"class_var": "padding-top-8", "four": 4}))
|
||||
|
||||
assert captured == (
|
||||
(),
|
||||
[],
|
||||
{
|
||||
"attrs": {
|
||||
"@click.stop": "dispatch('click_event')",
|
||||
|
|
|
@ -116,8 +116,8 @@ def with_component_cls(on_created: Callable):
|
|||
class TempComponent(Component):
|
||||
template = "Hello {{ name }}!"
|
||||
|
||||
def get_context_data(self, name="World"):
|
||||
return {"name": name}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"name": kwargs.get("name", "World")}
|
||||
|
||||
on_created()
|
||||
|
||||
|
@ -143,8 +143,8 @@ class TestExtension:
|
|||
class TestAccessComp(Component):
|
||||
template = "Hello {{ name }}!"
|
||||
|
||||
def get_context_data(self, arg1, arg2, name="World"):
|
||||
return {"name": name}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"name": kwargs.get("name", "World")}
|
||||
|
||||
ext_class = TestAccessComp.TestExtension # type: ignore[attr-defined]
|
||||
assert issubclass(ext_class, ComponentExtension.ExtensionClass)
|
||||
|
@ -240,8 +240,8 @@ class TestExtensionHooks:
|
|||
class TestComponent(Component):
|
||||
template = "Hello {{ name }}!"
|
||||
|
||||
def get_context_data(self, name="World"):
|
||||
return {"name": name}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"name": kwargs.get("name", "World")}
|
||||
|
||||
registry.register("test_comp", TestComponent)
|
||||
extension = cast(DummyExtension, app_settings.EXTENSIONS[3])
|
||||
|
@ -268,13 +268,13 @@ class TestExtensionHooks:
|
|||
class TestComponent(Component):
|
||||
template = "Hello {{ name }}!"
|
||||
|
||||
def get_context_data(self, arg1, arg2, name="World"):
|
||||
return {"name": name}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"name": kwargs.get("name", "World")}
|
||||
|
||||
def get_js_data(self, *args, **kwargs):
|
||||
def get_js_data(self, args, kwargs, slots, context):
|
||||
return {"script": "console.log('Hello!')"}
|
||||
|
||||
def get_css_data(self, *args, **kwargs):
|
||||
def get_css_data(self, args, kwargs, slots, context):
|
||||
return {"style": "body { color: blue; }"}
|
||||
|
||||
# Render the component with some args and kwargs
|
||||
|
|
|
@ -182,9 +182,9 @@ class TestMultipleComponentRegistries:
|
|||
Slot: {% slot "default" default / %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable=None):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs.get("variable", None),
|
||||
}
|
||||
|
||||
registry_a.register("simple_a", SimpleComponent)
|
||||
|
|
|
@ -51,10 +51,10 @@ class TestTemplateSignal:
|
|||
class InnerComponent(Component):
|
||||
template_file = "simple_template.html"
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
|
|
@ -2706,7 +2706,7 @@ class TestResolver:
|
|||
|
||||
@register("test")
|
||||
class Test(Component):
|
||||
def get_context_data(self, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal captured
|
||||
captured = kwargs
|
||||
return {}
|
||||
|
@ -2771,7 +2771,7 @@ class TestResolver:
|
|||
class Test(Component):
|
||||
template = "var"
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal captured
|
||||
captured = args, kwargs
|
||||
return {}
|
||||
|
@ -2783,7 +2783,7 @@ class TestResolver:
|
|||
"""
|
||||
Template(template_str).render(Context({"myvar": "myval", "val2": [1, 2, 3]}))
|
||||
|
||||
assert captured == ((42, "myval"), {"key": "val", "key2": [1, 2, 3]})
|
||||
assert captured == ([42, "myval"], {"key": "val", "key2": [1, 2, 3]})
|
||||
|
||||
def test_component_special_kwargs(self):
|
||||
captured = None
|
||||
|
@ -2792,7 +2792,7 @@ class TestResolver:
|
|||
class Test(Component):
|
||||
template = "var"
|
||||
|
||||
def get_context_data(self, *args, **kwargs):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal captured
|
||||
captured = args, kwargs
|
||||
return {}
|
||||
|
@ -2805,7 +2805,7 @@ class TestResolver:
|
|||
Template(template_str).render(Context({"date": 2024, "bzz": "fzz"}))
|
||||
|
||||
assert captured == (
|
||||
tuple([]),
|
||||
[],
|
||||
{
|
||||
"date": 2024,
|
||||
"@lol": 2,
|
||||
|
|
|
@ -33,9 +33,9 @@ class TestTemplateCache:
|
|||
"""
|
||||
return content
|
||||
|
||||
def get_context_data(self, variable=None):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable": kwargs.get("variable", None),
|
||||
}
|
||||
|
||||
comp = SimpleComponent()
|
||||
|
|
|
@ -215,8 +215,8 @@ class TestTemplateParser:
|
|||
Slot: {% slot "content" default / %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, var: str) -> dict:
|
||||
return {"var": var}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"var": kwargs["var"]}
|
||||
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
|
|
@ -29,17 +29,17 @@ class TestMultilineTags:
|
|||
Variable: <strong>{{ variable }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
{% component
|
||||
"test_component"
|
||||
123
|
||||
variable=123
|
||||
variable2="abc"
|
||||
%}
|
||||
{% endcomponent %}
|
||||
|
@ -58,9 +58,9 @@ class TestNestedTags:
|
|||
Variable: <strong>{{ var }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, var):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"var": var,
|
||||
"var": kwargs["var"],
|
||||
}
|
||||
|
||||
# See https://github.com/django-components/django-components/discussions/671
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import re
|
||||
from typing import NamedTuple
|
||||
|
||||
import pytest
|
||||
from django.template import Context, Template, TemplateSyntaxError
|
||||
|
@ -25,8 +26,8 @@ class SlottedComponentWithContext(Component):
|
|||
</custom-template>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
||||
|
||||
#######################
|
||||
|
@ -41,10 +42,10 @@ class TestComponentTemplateTag:
|
|||
Variable: <strong>{{ variable }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -114,10 +115,10 @@ class TestComponentTemplateTag:
|
|||
{% endif %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -178,8 +179,11 @@ class TestComponentTemplateTag:
|
|||
Default: <p>{{ default_param }}</p>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, default_param="default text"):
|
||||
return {"variable": variable, "default_param": default_param}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": kwargs["variable"],
|
||||
"default_param": kwargs.get("default_param", "default text"),
|
||||
}
|
||||
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -204,10 +208,17 @@ class TestDynamicComponentTemplateTag:
|
|||
Variable: <strong>{{ variable }}</strong>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
class Kwargs(NamedTuple):
|
||||
variable: str
|
||||
variable2: str
|
||||
|
||||
class Defaults:
|
||||
variable2 = "default"
|
||||
|
||||
def get_template_data(self, args, kwargs: Kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs.variable,
|
||||
"variable2": kwargs.variable2,
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -375,10 +386,10 @@ class TestDynamicComponentTemplateTag:
|
|||
Slot: {% slot "default" default / %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
registry.register(name="test", component=SimpleSlottedComponent)
|
||||
|
@ -412,10 +423,10 @@ class TestDynamicComponentTemplateTag:
|
|||
Slot 2: {% slot "two" / %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
registry.register(name="test", component=SimpleSlottedComponent)
|
||||
|
@ -455,10 +466,10 @@ class TestDynamicComponentTemplateTag:
|
|||
Slot 2: {% slot "two" / %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
registry.register(name="test", component=SimpleSlottedComponent)
|
||||
|
@ -722,8 +733,11 @@ class TestAggregateInput:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self, *args, attrs, my_dict):
|
||||
return {"attrs": attrs, "my_dict": my_dict}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"attrs": kwargs["attrs"],
|
||||
"my_dict": kwargs["my_dict"],
|
||||
}
|
||||
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -751,9 +765,12 @@ class TestRecursiveComponent:
|
|||
|
||||
@register("recursive")
|
||||
class Recursive(Component):
|
||||
def get_context_data(self, depth: int = 0):
|
||||
print("depth:", depth)
|
||||
return {"depth": depth + 1, "DEPTH": DEPTH}
|
||||
class Defaults:
|
||||
depth = 0
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
print("depth:", kwargs["depth"])
|
||||
return {"depth": kwargs["depth"] + 1, "DEPTH": DEPTH}
|
||||
|
||||
template: types.django_html = """
|
||||
<div>
|
||||
|
|
|
@ -820,7 +820,7 @@ class TestExtendsCompat:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("block_provide")
|
||||
return {"var": var}
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
import re
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from django.template import Context, Template, TemplateSyntaxError
|
||||
|
@ -29,7 +28,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -79,7 +78,7 @@ class TestProvideTemplateTag:
|
|||
<div> another: {{ another }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
my_provide = self.inject("my_provide")
|
||||
return {
|
||||
"key": my_provide.key,
|
||||
|
@ -114,7 +113,7 @@ class TestProvideTemplateTag:
|
|||
<div> another: {{ my_provide.another }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
my_provide = self.inject("my_provide")
|
||||
return {
|
||||
"my_provide": my_provide,
|
||||
|
@ -147,7 +146,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -179,7 +178,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -214,7 +213,7 @@ class TestProvideTemplateTag:
|
|||
<div></div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {}
|
||||
|
||||
template_str: types.django_html = """
|
||||
|
@ -246,7 +245,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -279,7 +278,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -318,7 +317,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -361,7 +360,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -390,7 +389,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -419,7 +418,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -446,7 +445,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -478,7 +477,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -515,7 +514,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -558,7 +557,7 @@ class TestProvideTemplateTag:
|
|||
<div> second_provide: {{ second_provide|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
first_provide = self.inject("first_provide", "default")
|
||||
second_provide = self.inject("second_provide", "default")
|
||||
return {
|
||||
|
@ -595,7 +594,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -626,7 +625,7 @@ class TestProvideTemplateTag:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -674,7 +673,7 @@ class TestInject:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("my_provide")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -704,7 +703,7 @@ class TestInject:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("abc")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -728,7 +727,7 @@ class TestInject:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("abc", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -755,7 +754,7 @@ class TestInject:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -783,7 +782,7 @@ class TestInject:
|
|||
<div> injected: {{ var|safe }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
var = self.inject("abc", "default")
|
||||
return {"var": var}
|
||||
|
||||
|
@ -806,14 +805,14 @@ class TestInject:
|
|||
</main>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
data = self.inject("my_provide")
|
||||
return {"data": data}
|
||||
|
||||
@register("provider")
|
||||
class Provider(Component):
|
||||
def get_context_data(self, data: Any) -> Any:
|
||||
return {"data": data}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"data": kwargs["data"]}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -824,8 +823,8 @@ class TestInject:
|
|||
|
||||
@register("parent")
|
||||
class Parent(Component):
|
||||
def get_context_data(self, data: Any) -> Any:
|
||||
return {"data": data}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"data": kwargs["data"]}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -875,14 +874,14 @@ class TestInject:
|
|||
</main>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
data = self.inject("my_provide")
|
||||
return {"data": data}
|
||||
|
||||
@register("provider")
|
||||
class Provider(Component):
|
||||
def get_context_data(self, data: Any) -> Any:
|
||||
return {"data": data}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"data": kwargs["data"]}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -893,8 +892,8 @@ class TestInject:
|
|||
|
||||
@register("parent")
|
||||
class Parent(Component):
|
||||
def get_context_data(self, data: Any) -> Any:
|
||||
return {"data": data}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"data": kwargs["data"]}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -949,7 +948,7 @@ class TestProvideCache:
|
|||
<div> Ran: {{ ran }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
assert len(provide_cache) == 1
|
||||
|
||||
data = self.inject("my_provide")
|
||||
|
@ -989,7 +988,7 @@ class TestProvideCache:
|
|||
class Injectee(Component):
|
||||
template = ""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
assert len(provide_cache) == 1
|
||||
data = self.inject("my_provide")
|
||||
|
||||
|
@ -1022,7 +1021,7 @@ class TestProvideCache:
|
|||
<div> Ran: {{ ran }} </div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
assert len(provide_cache) == 1
|
||||
|
||||
data = self.inject("my_provide")
|
||||
|
@ -1059,7 +1058,7 @@ class TestProvideCache:
|
|||
class Injectee(Component):
|
||||
template = ""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
assert len(provide_cache) == 1
|
||||
|
||||
data = self.inject("my_provide")
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import re
|
||||
from typing import Any, Dict, List, Optional
|
||||
from typing import Dict
|
||||
|
||||
import pytest
|
||||
from django.template import Context, Template, TemplateSyntaxError
|
||||
|
@ -40,10 +40,10 @@ class TestComponentSlot:
|
|||
class SimpleComponent(Component):
|
||||
template = """Variable: <strong>{{ variable }}</strong>"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
class Media:
|
||||
|
@ -96,10 +96,10 @@ class TestComponentSlot:
|
|||
class SimpleComponent(Component):
|
||||
template = """Variable: <strong>{{ variable }}</strong>"""
|
||||
|
||||
def get_context_data(self, variable, variable2="default"):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"variable": variable,
|
||||
"variable2": variable2,
|
||||
"variable": kwargs["variable"],
|
||||
"variable2": kwargs.get("variable2", "default"),
|
||||
}
|
||||
|
||||
template_str: types.django_html = """
|
||||
|
@ -152,8 +152,8 @@ class TestComponentSlot:
|
|||
</custom-template>
|
||||
"""
|
||||
|
||||
def get_context_data(self, variable):
|
||||
return {"variable": variable}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"variable": kwargs["variable"]}
|
||||
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -294,9 +294,9 @@ class TestComponentSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"name": name,
|
||||
"name": kwargs.get("name", None),
|
||||
}
|
||||
|
||||
registry.register("test", SlottedComponent)
|
||||
|
@ -451,8 +451,8 @@ class TestComponentSlot:
|
|||
@djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR)
|
||||
def test_multiple_slots_with_same_name_different_flags(self, components_settings):
|
||||
class TestComp(Component):
|
||||
def get_context_data(self, required: bool) -> Any:
|
||||
return {"required": required}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"required": kwargs["required"]}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -824,8 +824,8 @@ class TestComponentSlotDefault:
|
|||
def test_implicit_fill_when_slot_marked_default_not_rendered(self, components_settings):
|
||||
@register("test_comp")
|
||||
class ConditionalSlotted(Component):
|
||||
def get_context_data(self, var: bool) -> Any:
|
||||
return {"var": var}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"var": kwargs["var"]}
|
||||
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -885,9 +885,9 @@ class TestPassthroughSlots:
|
|||
</custom-template>
|
||||
"""
|
||||
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"name": name,
|
||||
"name": kwargs.get("name", None),
|
||||
}
|
||||
|
||||
template_str: types.django_html = """
|
||||
|
@ -941,9 +941,9 @@ class TestPassthroughSlots:
|
|||
</custom-template>
|
||||
"""
|
||||
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"name": name,
|
||||
"name": kwargs.get("name", None),
|
||||
}
|
||||
|
||||
template_str: types.django_html = """
|
||||
|
@ -985,9 +985,9 @@ class TestPassthroughSlots:
|
|||
</custom-template>
|
||||
"""
|
||||
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"name": name,
|
||||
"name": kwargs.get("name", None),
|
||||
}
|
||||
|
||||
template_str: types.django_html = """
|
||||
|
@ -1021,7 +1021,7 @@ class TestPassthroughSlots:
|
|||
def test_slots_inside_loops(self, components_settings):
|
||||
@register("test_comp")
|
||||
class OuterComp(Component):
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"slots": ["header", "main", "footer"],
|
||||
}
|
||||
|
@ -1064,7 +1064,7 @@ class TestPassthroughSlots:
|
|||
|
||||
@register("test_comp")
|
||||
class OuterComp(Component):
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"slots": self.input.slots,
|
||||
}
|
||||
|
@ -1115,7 +1115,7 @@ class TestPassthroughSlots:
|
|||
|
||||
@register("test_comp")
|
||||
class OuterComp(Component):
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"slots": self.input.slots,
|
||||
}
|
||||
|
@ -1510,7 +1510,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "def",
|
||||
"var123": 456,
|
||||
|
@ -1545,7 +1545,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "def",
|
||||
"var123": 456,
|
||||
|
@ -1580,7 +1580,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "def",
|
||||
"var123": 456,
|
||||
|
@ -1617,7 +1617,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"slot_name": "my_slot",
|
||||
"abc": "def",
|
||||
|
@ -1653,7 +1653,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"slot_props": {
|
||||
"name": "my_slot",
|
||||
|
@ -1692,7 +1692,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "xyz",
|
||||
"var123": 456,
|
||||
|
@ -1728,7 +1728,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "def",
|
||||
"var123": 456,
|
||||
|
@ -1759,7 +1759,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "def",
|
||||
"var123": 456,
|
||||
|
@ -1811,7 +1811,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "def",
|
||||
"var123": 456,
|
||||
|
@ -1837,7 +1837,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "def",
|
||||
"var123": 456,
|
||||
|
@ -1880,7 +1880,7 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "def",
|
||||
"var123": 456,
|
||||
|
@ -1925,10 +1925,10 @@ class TestScopedSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self, input):
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"abc": "def",
|
||||
"input": input,
|
||||
"input": kwargs["input"],
|
||||
}
|
||||
|
||||
template_str: types.django_html = """
|
||||
|
@ -1974,9 +1974,9 @@ class TestDuplicateSlot:
|
|||
<footer>{% slot "footer" %}Default footer{% endslot %}</footer>
|
||||
"""
|
||||
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"name": name,
|
||||
"name": kwargs.get("name", None),
|
||||
}
|
||||
return DuplicateSlotComponent
|
||||
|
||||
|
@ -2001,9 +2001,9 @@ class TestDuplicateSlot:
|
|||
</div>
|
||||
"""
|
||||
|
||||
def get_context_data(self, items: List) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"items": items,
|
||||
"items": kwargs["items"],
|
||||
}
|
||||
return DuplicateSlotNestedComponent
|
||||
|
||||
|
@ -2243,9 +2243,9 @@ class TestSlotBehavior:
|
|||
</custom-template>
|
||||
"""
|
||||
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"name": name,
|
||||
"name": kwargs.get("name", None),
|
||||
}
|
||||
|
||||
registry.register("test", SlottedComponent)
|
||||
|
@ -2360,7 +2360,7 @@ class TestSlotBehavior:
|
|||
class TestSlotInput:
|
||||
@djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR)
|
||||
def test_slots_accessible_when_python_render(self, components_settings):
|
||||
slots: Dict = {}
|
||||
seen_slots: Dict = {}
|
||||
|
||||
@register("test")
|
||||
class SlottedComponent(Component):
|
||||
|
@ -2371,12 +2371,12 @@ class TestSlotInput:
|
|||
<footer>{% slot "footer" %}Default footer{% endslot %}</footer>
|
||||
"""
|
||||
|
||||
def get_context_data(self, input: Optional[int] = None) -> Dict[str, Any]:
|
||||
nonlocal slots
|
||||
slots = self.input.slots
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal seen_slots
|
||||
seen_slots = slots
|
||||
return {}
|
||||
|
||||
assert slots == {}
|
||||
assert seen_slots == {}
|
||||
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
@ -2390,14 +2390,14 @@ class TestSlotInput:
|
|||
template = Template(template_str)
|
||||
template.render(Context())
|
||||
|
||||
assert list(slots.keys()) == ["header", "main"]
|
||||
assert callable(slots["header"])
|
||||
assert callable(slots["main"])
|
||||
assert "footer" not in slots
|
||||
assert list(seen_slots.keys()) == ["header", "main"]
|
||||
assert callable(seen_slots["header"])
|
||||
assert callable(seen_slots["main"])
|
||||
assert "footer" not in seen_slots
|
||||
|
||||
@djc_test(parametrize=PARAMETRIZE_CONTEXT_BEHAVIOR)
|
||||
def test_slots_normalized_as_slot_instances(self, components_settings):
|
||||
slots: Dict[str, Slot] = {}
|
||||
seen_slots: Dict[str, Slot] = {}
|
||||
|
||||
@register("test")
|
||||
class SlottedComponent(Component):
|
||||
|
@ -2408,12 +2408,12 @@ class TestSlotInput:
|
|||
<footer>{% slot "footer" %}Default footer{% endslot %}</footer>
|
||||
"""
|
||||
|
||||
def get_context_data(self, input: Optional[int] = None) -> Dict[str, Any]:
|
||||
nonlocal slots
|
||||
slots = self.input.slots
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
nonlocal seen_slots
|
||||
seen_slots = slots
|
||||
return {}
|
||||
|
||||
assert slots == {}
|
||||
assert seen_slots == {}
|
||||
|
||||
header_slot = Slot(lambda *a, **kw: "HEADER_SLOT")
|
||||
main_slot_str = "MAIN_SLOT"
|
||||
|
@ -2427,11 +2427,11 @@ class TestSlotInput:
|
|||
}
|
||||
)
|
||||
|
||||
assert isinstance(slots["header"], Slot)
|
||||
assert slots["header"](Context(), None, None) == "HEADER_SLOT" # type: ignore[arg-type]
|
||||
assert isinstance(seen_slots["header"], Slot)
|
||||
assert seen_slots["header"](Context(), None, None) == "HEADER_SLOT" # type: ignore[arg-type]
|
||||
|
||||
assert isinstance(slots["main"], Slot)
|
||||
assert slots["main"](Context(), None, None) == "MAIN_SLOT" # type: ignore[arg-type]
|
||||
assert isinstance(seen_slots["main"], Slot)
|
||||
assert seen_slots["main"](Context(), None, None) == "MAIN_SLOT" # type: ignore[arg-type]
|
||||
|
||||
assert isinstance(slots["footer"], Slot)
|
||||
assert slots["footer"](Context(), None, None) == "FOOTER_SLOT" # type: ignore[arg-type]
|
||||
assert isinstance(seen_slots["footer"], Slot)
|
||||
assert seen_slots["footer"](Context(), None, None) == "FOOTER_SLOT" # type: ignore[arg-type]
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
"""This file tests various ways how the individual tags can be combined inside the templates"""
|
||||
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from django.template import Context, Template
|
||||
from pytest_django.asserts import assertHTMLEqual
|
||||
|
||||
|
@ -103,9 +101,9 @@ class TestNestedSlot:
|
|||
</custom-template>
|
||||
"""
|
||||
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"name": name,
|
||||
"name": kwargs.get("name", None),
|
||||
}
|
||||
|
||||
registry.clear()
|
||||
|
@ -168,8 +166,8 @@ class TestConditionalSlot:
|
|||
{% endif %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, branch=None):
|
||||
return {"branch": branch}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"branch": kwargs.get("branch", None)}
|
||||
|
||||
return ConditionalComponent
|
||||
|
||||
|
@ -267,9 +265,9 @@ class TestSlotIteration:
|
|||
{% endfor %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, objects, *args, **kwargs) -> dict:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"objects": objects,
|
||||
"objects": kwargs["objects"],
|
||||
}
|
||||
|
||||
return ComponentSimpleSlotInALoop
|
||||
|
@ -303,7 +301,7 @@ class TestSlotIteration:
|
|||
assertHTMLEqual(rendered, expected)
|
||||
|
||||
# NOTE: Second arg in tuple is expected result. In isolated mode, while loops should NOT leak,
|
||||
# we should still have access to root context (returned from get_context_data)
|
||||
# we should still have access to root context (returned from get_template_data)
|
||||
@djc_test(
|
||||
parametrize=(
|
||||
["components_settings", "expected"],
|
||||
|
@ -376,7 +374,7 @@ class TestSlotIteration:
|
|||
assertHTMLEqual(rendered, expected)
|
||||
|
||||
# NOTE: Second arg in tuple is expected result. In isolated mode, while loops should NOT leak,
|
||||
# we should still have access to root context (returned from get_context_data)
|
||||
# we should still have access to root context (returned from get_template_data)
|
||||
@djc_test(
|
||||
parametrize=(
|
||||
["components_settings", "expected"],
|
||||
|
@ -662,9 +660,9 @@ class TestComponentNesting:
|
|||
</custom-template>
|
||||
"""
|
||||
|
||||
def get_context_data(self, name: Optional[str] = None) -> Dict[str, Any]:
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {
|
||||
"name": name,
|
||||
"name": kwargs.get("name", None),
|
||||
}
|
||||
|
||||
registry.register("test", SlottedComponent)
|
||||
|
@ -817,8 +815,8 @@ class TestComponentNesting:
|
|||
{% endfor %}
|
||||
"""
|
||||
|
||||
def get_context_data(self, items, *args, **kwargs) -> Dict[str, Any]:
|
||||
return {"items": items}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"items": kwargs["items"]}
|
||||
|
||||
template_str: types.django_html = """
|
||||
{% load component_tags %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue