mirror of
https://github.com/django-components/django-components.git
synced 2025-08-31 03:07:19 +00:00
refactor: deprecate template caching, get_template_name, get_template, assoc template with Comp cls (#1222)
* refactor: deprecate template caching, get_template_name, get_template, assoc template with Comp cls * refactor: change implementation * refactor: handle cached template loader * refactor: fix tests * refactor: fix test on windows * refactor: try to fix type errors * refactor: Re-cast `context` to fix type errors * refactor: fix linter error * refactor: fix typing * refactor: more linter fixes * refactor: more linter errors * refactor: revert extra node metadata
This commit is contained in:
parent
fa9ae9892f
commit
8677ee7941
28 changed files with 1548 additions and 652 deletions
|
@ -210,17 +210,6 @@ class TestMainMedia:
|
|||
assert AppLvlCompComponent._component_media.js == 'console.log("JS file");\n' # type: ignore[attr-defined]
|
||||
assert AppLvlCompComponent._component_media.js_file == "app_lvl_comp/app_lvl_comp.js" # type: ignore[attr-defined]
|
||||
|
||||
def test_html_variable(self):
|
||||
class VariableHTMLComponent(Component):
|
||||
def get_template(self, context):
|
||||
return Template("<div class='variable-html'>{{ variable }}</div>")
|
||||
|
||||
rendered = VariableHTMLComponent.render(context=Context({"variable": "Dynamic Content"}))
|
||||
assertHTMLEqual(
|
||||
rendered,
|
||||
'<div class="variable-html" data-djc-id-ca1bc3e>Dynamic Content</div>',
|
||||
)
|
||||
|
||||
def test_html_variable_filtered(self):
|
||||
class FilteredComponent(Component):
|
||||
template: types.django_html = """
|
||||
|
@ -851,40 +840,46 @@ class TestMediaStaticfiles:
|
|||
|
||||
@djc_test
|
||||
class TestMediaRelativePath:
|
||||
class ParentComponent(Component):
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
<div>
|
||||
<h1>Parent content</h1>
|
||||
{% component "variable_display" shadowing_variable='override' new_variable='unique_val' %}
|
||||
{% endcomponent %}
|
||||
</div>
|
||||
<div>
|
||||
{% slot 'content' %}
|
||||
<h2>Slot content</h2>
|
||||
{% component "variable_display" shadowing_variable='slot_default_override' new_variable='slot_default_unique' %}
|
||||
def _gen_parent_component(self):
|
||||
class ParentComponent(Component):
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
<div>
|
||||
<h1>Parent content</h1>
|
||||
{% component "variable_display" shadowing_variable='override' new_variable='unique_val' %}
|
||||
{% endcomponent %}
|
||||
{% endslot %}
|
||||
</div>
|
||||
""" # noqa
|
||||
</div>
|
||||
<div>
|
||||
{% slot 'content' %}
|
||||
<h2>Slot content</h2>
|
||||
{% component "variable_display" shadowing_variable='slot_default_override' new_variable='slot_default_unique' %}
|
||||
{% endcomponent %}
|
||||
{% endslot %}
|
||||
</div>
|
||||
""" # noqa
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"shadowing_variable": "NOT SHADOWED"}
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
return {"shadowing_variable": "NOT SHADOWED"}
|
||||
|
||||
class VariableDisplay(Component):
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
<h1>Shadowing variable = {{ shadowing_variable }}</h1>
|
||||
<h1>Uniquely named variable = {{ unique_variable }}</h1>
|
||||
"""
|
||||
return ParentComponent
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
context = {}
|
||||
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
|
||||
def _gen_variable_display_component(self):
|
||||
class VariableDisplay(Component):
|
||||
template: types.django_html = """
|
||||
{% load component_tags %}
|
||||
<h1>Shadowing variable = {{ shadowing_variable }}</h1>
|
||||
<h1>Uniquely named variable = {{ unique_variable }}</h1>
|
||||
"""
|
||||
|
||||
def get_template_data(self, args, kwargs, slots, context):
|
||||
context = {}
|
||||
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
|
||||
|
||||
return VariableDisplay
|
||||
|
||||
# Settings required for autodiscover to work
|
||||
@djc_test(
|
||||
|
@ -896,8 +891,8 @@ class TestMediaRelativePath:
|
|||
}
|
||||
)
|
||||
def test_component_with_relative_media_paths(self):
|
||||
registry.register(name="parent_component", component=self.ParentComponent)
|
||||
registry.register(name="variable_display", component=self.VariableDisplay)
|
||||
registry.register(name="parent_component", component=self._gen_parent_component())
|
||||
registry.register(name="variable_display", component=self._gen_variable_display_component())
|
||||
|
||||
# Ensure that the module is executed again after import in autodiscovery
|
||||
if "tests.components.relative_file.relative_file" in sys.modules:
|
||||
|
@ -948,8 +943,8 @@ class TestMediaRelativePath:
|
|||
}
|
||||
)
|
||||
def test_component_with_relative_media_paths_as_subcomponent(self):
|
||||
registry.register(name="parent_component", component=self.ParentComponent)
|
||||
registry.register(name="variable_display", component=self.VariableDisplay)
|
||||
registry.register(name="parent_component", component=self._gen_parent_component())
|
||||
registry.register(name="variable_display", component=self._gen_variable_display_component())
|
||||
|
||||
# Ensure that the module is executed again after import in autodiscovery
|
||||
if "tests.components.relative_file.relative_file" in sys.modules:
|
||||
|
@ -995,8 +990,8 @@ class TestMediaRelativePath:
|
|||
|
||||
https://github.com/django-components/django-components/issues/522#issuecomment-2173577094
|
||||
"""
|
||||
registry.register(name="parent_component", component=self.ParentComponent)
|
||||
registry.register(name="variable_display", component=self.VariableDisplay)
|
||||
registry.register(name="parent_component", component=self._gen_parent_component())
|
||||
registry.register(name="variable_display", component=self._gen_variable_display_component())
|
||||
|
||||
# Ensure that the module is executed again after import in autodiscovery
|
||||
if "tests.components.relative_file_pathobj.relative_file_pathobj" in sys.modules:
|
||||
|
@ -1066,7 +1061,7 @@ class TestSubclassingMedia:
|
|||
js = "grandparent.js"
|
||||
|
||||
class ParentComponent(GrandParentComponent):
|
||||
Media = None
|
||||
Media = None # type: ignore[assignment]
|
||||
|
||||
class ChildComponent(ParentComponent):
|
||||
class Media:
|
||||
|
@ -1149,7 +1144,7 @@ class TestSubclassingMedia:
|
|||
js = "parent1.js"
|
||||
|
||||
class Parent2Component(GrandParent3Component, GrandParent4Component):
|
||||
Media = None
|
||||
Media = None # type: ignore[assignment]
|
||||
|
||||
class ChildComponent(Parent1Component, Parent2Component):
|
||||
template: types.django_html = """
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue