refactor: rename template_name to template_file (#878)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Juro Oravec 2025-01-01 17:06:14 +01:00 committed by GitHub
parent b99e32e9d5
commit d94a459c8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 251 additions and 138 deletions

View file

@ -7,7 +7,7 @@ from django_components import Component, register
@register("multi_file_component")
class MultFileComponent(Component):
template_name = "multi_file/multi_file.html"
template_file = "multi_file/multi_file.html"
def post(self, request, *args, **kwargs) -> HttpResponse:
variable = request.POST.get("variable")

View file

@ -7,7 +7,7 @@ from django_components import Component, register
@register("relative_file_component")
class RelativeFileComponent(Component):
template_name = "relative_file.html"
template_file = "relative_file.html"
class Media:
js = "relative_file.js"

View file

@ -26,7 +26,7 @@ class PathObj:
@register("relative_file_pathobj_component")
class RelativeFileWithPathObjComponent(Component):
template_name = "relative_file_pathobj.html"
template_file = "relative_file_pathobj.html"
class Media:
js = PathObj("relative_file_pathobj.js")

View file

@ -6,7 +6,7 @@ from django_components import Component, register
# Used for testing the staticfiles finder in `test_staticfiles.py`
@register("staticfiles_component")
class RelativeFileWithPathObjComponent(Component):
template_name = "staticfiles.html"
template_file = "staticfiles.html"
class Media:
js = "staticfiles.js"

View file

@ -6,7 +6,7 @@ from django_components import Component, register
# Used for testing the template_loader
@register("app_lvl_comp")
class AppLvlCompComponent(Component):
template_name: Optional[str] = "app_lvl_comp.html"
template_file: Optional[str] = "app_lvl_comp.html"
js_file = "app_lvl_comp.js"
css_file = "app_lvl_comp.css"

View file

@ -6,7 +6,7 @@ from django_components import Component, register
# Used for testing the template_loader
@register("custom_app_lvl_comp")
class AppLvlCompComponent(Component):
template_name = "app_lvl_comp.html"
template_file = "app_lvl_comp.html"
class Media:
js = "app_lvl_comp.js"

View file

@ -206,9 +206,9 @@ class ComponentTest(BaseTestCase):
)
@parametrize_context_behavior(["django", "isolated"])
def test_template_name_static(self):
def test_template_file_static(self):
class SimpleComponent(Component):
template_name = "simple_template.html"
template_file = "simple_template.html"
def get_context_data(self, variable=None):
return {
@ -228,7 +228,58 @@ class ComponentTest(BaseTestCase):
)
@parametrize_context_behavior(["django", "isolated"])
def test_template_name_dynamic(self):
def test_template_file_static__compat(self):
class SimpleComponent(Component):
template_name = "simple_template.html"
def get_context_data(self, variable=None):
return {
"variable": variable,
}
class Media:
css = "style.css"
js = "script.js"
self.assertEqual(SimpleComponent.template_name, "simple_template.html")
self.assertEqual(SimpleComponent.template_file, "simple_template.html")
SimpleComponent.template_name = "other_template.html"
self.assertEqual(SimpleComponent.template_name, "other_template.html")
self.assertEqual(SimpleComponent.template_file, "other_template.html")
SimpleComponent.template_name = "simple_template.html"
rendered = SimpleComponent.render(kwargs={"variable": "test"})
self.assertHTMLEqual(
rendered,
"""
Variable: <strong data-djc-id-a1bc3e>test</strong>
""",
)
comp = SimpleComponent()
self.assertEqual(comp.template_name, "simple_template.html")
self.assertEqual(comp.template_file, "simple_template.html")
# NOTE: Setting `template_file` on INSTANCE is not supported, as users should work
# with classes and not instances. This is tested for completeness.
comp.template_name = "other_template_2.html"
self.assertEqual(comp.template_name, "other_template_2.html")
self.assertEqual(comp.template_file, "other_template_2.html")
self.assertEqual(SimpleComponent.template_name, "other_template_2.html")
self.assertEqual(SimpleComponent.template_file, "other_template_2.html")
SimpleComponent.template_name = "simple_template.html"
rendered = comp.render(kwargs={"variable": "test"})
self.assertHTMLEqual(
rendered,
"""
Variable: <strong data-djc-id-a1bc3f>test</strong>
""",
)
@parametrize_context_behavior(["django", "isolated"])
def test_template_file_dynamic(self):
class SvgComponent(Component):
def get_context_data(self, name, css_class="", title="", **attrs):
return {

View file

@ -31,7 +31,7 @@ class MainMediaTest(BaseTestCase):
def test_html_filepath(self):
class Test(Component):
template_name = "simple_template.html"
template_file = "simple_template.html"
rendered = Test.render(context={"variable": "test"})
@ -86,7 +86,7 @@ class MainMediaTest(BaseTestCase):
from tests.test_app.components.app_lvl_comp.app_lvl_comp import AppLvlCompComponent
class TestComponent(AppLvlCompComponent):
template_name = None
template_file = None
template = """
{% load component_tags %}
{% component_js_dependencies %}
@ -167,7 +167,7 @@ class MainMediaTest(BaseTestCase):
from tests.test_app.components.app_lvl_comp.app_lvl_comp import AppLvlCompComponent
class TestComponent(AppLvlCompComponent):
template_name = None
template_file = None
template = """
{% load component_tags %}
{% component_js_dependencies %}

View file

@ -13,7 +13,7 @@ setup_test_config({"autodiscover": False})
class SlottedComponent(Component):
template_name = "slotted_template.html"
template_file = "slotted_template.html"
#######################
@ -42,7 +42,7 @@ class TemplateInstrumentationTest(BaseTestCase):
@register("inner_component")
class SimpleComponent(Component):
template_name = "simple_template.html"
template_file = "simple_template.html"
def get_context_data(self, variable, variable2="default"):
return {

View file

@ -9,7 +9,7 @@ setup_test_config({"autodiscover": False})
class SlottedComponent(Component):
template_name = "slotted_template.html"
template_file = "slotted_template.html"
class SlottedComponentWithContext(Component):

View file

@ -11,11 +11,11 @@ setup_test_config({"autodiscover": False})
class SlottedComponent(Component):
template_name = "slotted_template.html"
template_file = "slotted_template.html"
class BlockedAndSlottedComponent(Component):
template_name = "blocked_and_slotted_template.html"
template_file = "blocked_and_slotted_template.html"
#######################
@ -418,7 +418,7 @@ class ExtendsCompatTests(BaseTestCase):
@register("extended_component")
class _ExtendedComponent(Component):
template_name = "included.html"
template_file = "included.html"
template: types.django_html = """
{% extends 'block.html' %}
@ -587,7 +587,7 @@ class ExtendsCompatTests(BaseTestCase):
@register("block_in_component_parent")
class BlockInCompParent(Component):
template_name = "block_in_component_parent.html"
template_file = "block_in_component_parent.html"
template: types.django_html = """
{% load component_tags %}
@ -620,7 +620,7 @@ class ExtendsCompatTests(BaseTestCase):
@register("block_inside_slot_v1")
class BlockInSlotInComponent(Component):
template_name = "block_in_slot_in_component.html"
template_file = "block_in_slot_in_component.html"
template: types.django_html = """
{% load component_tags %}