[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2024-04-16 12:37:59 +00:00 committed by Juro Oravec
parent 089cda54c5
commit ab7f3e0cdb
5 changed files with 27 additions and 20 deletions

View file

@ -20,19 +20,24 @@ from django.views import View
# way the two modules depend on one another. # way the two modules depend on one another.
from django_components.component_registry import registry # NOQA from django_components.component_registry import registry # NOQA
from django_components.component_registry import AlreadyRegistered, ComponentRegistry, NotRegistered, register # NOQA from django_components.component_registry import AlreadyRegistered, ComponentRegistry, NotRegistered, register # NOQA
from django_components.context import capture_root_context, set_root_context, get_root_context, set_slot_component_association from django_components.context import (
capture_root_context,
get_root_context,
set_root_context,
set_slot_component_association,
)
from django_components.logger import logger, trace_msg from django_components.logger import logger, trace_msg
from django_components.middleware import is_dependency_middleware_active from django_components.middleware import is_dependency_middleware_active
from django_components.node import walk_nodelist from django_components.node import walk_nodelist
from django_components.slots import ( from django_components.slots import (
DEFAULT_SLOT_KEY,
FillContent, FillContent,
FillNode, FillNode,
SlotName, SlotName,
SlotNode, SlotNode,
render_component_template_with_slots, render_component_template_with_slots,
DEFAULT_SLOT_KEY,
) )
from django_components.utils import search, gen_id from django_components.utils import gen_id, search
RENDERED_COMMENT_TEMPLATE = "<!-- _RENDERED {name} -->" RENDERED_COMMENT_TEMPLATE = "<!-- _RENDERED {name} -->"

View file

@ -5,7 +5,7 @@ pass data across components, nodes, slots, and contexts.
You can think of the Context as our storage system. You can think of the Context as our storage system.
""" """
from typing import Optional, TYPE_CHECKING from typing import TYPE_CHECKING, Optional
from django.template import Context from django.template import Context

View file

@ -9,15 +9,15 @@ else:
from typing import TypeAlias from typing import TypeAlias
from django.template import Context, Template from django.template import Context, Template
from django.template.base import FilterExpression, Node, NodeList, TextNode, Parser from django.template.base import FilterExpression, Node, NodeList, Parser, TextNode
from django.template.defaulttags import CommentNode from django.template.defaulttags import CommentNode
from django.template.exceptions import TemplateSyntaxError from django.template.exceptions import TemplateSyntaxError
from django.utils.safestring import SafeString, mark_safe from django.utils.safestring import SafeString, mark_safe
from django_components.app_settings import SlotContextBehavior, app_settings from django_components.app_settings import SlotContextBehavior, app_settings
from django_components.context import get_root_context, get_slot_fill, set_slot_fill, get_slot_component_association from django_components.context import get_root_context, get_slot_component_association, get_slot_fill, set_slot_fill
from django_components.node import nodelist_has_content
from django_components.logger import trace_msg from django_components.logger import trace_msg
from django_components.node import nodelist_has_content
from django_components.utils import gen_id from django_components.utils import gen_id
DEFAULT_SLOT_KEY = "_DJANGO_COMPONENTS_DEFAULT_SLOT" DEFAULT_SLOT_KEY = "_DJANGO_COMPONENTS_DEFAULT_SLOT"
@ -58,10 +58,11 @@ class UserSlotVar:
class ComponentIdMixin: class ComponentIdMixin:
""" """
Mixin for classes use or pass through component ID. Mixin for classes use or pass through component ID.
We use component IDs to identify which slots should be We use component IDs to identify which slots should be
rendered with which fills for which components. rendered with which fills for which components.
""" """
_component_id: str _component_id: str
@property @property
@ -369,7 +370,7 @@ def render_component_template_with_slots(
NOTE: The nodes in the template are mutated in the process! NOTE: The nodes in the template are mutated in the process!
""" """
# ---- Prepare slot fills ---- # ---- Prepare slot fills ----
slot_name2fill_content = _collect_slot_fills_from_component_template(template, fill_content, registered_name) slot_name2fill_content = _collect_slot_fills_from_component_template(template, fill_content, registered_name)
# Give slot nodes knowledge of their parent component. # Give slot nodes knowledge of their parent component.
@ -411,7 +412,7 @@ def _collect_slot_fills_from_component_template(
# Type check so the rest of the logic has type of `node` is inferred # Type check so the rest of the logic has type of `node` is inferred
if not isinstance(node, SlotNode): if not isinstance(node, SlotNode):
continue continue
slot_name = node.name slot_name = node.name
if slot_name in slot_name2fill_content: if slot_name in slot_name2fill_content:
raise TemplateSyntaxError( raise TemplateSyntaxError(
@ -440,7 +441,7 @@ def _collect_slot_fills_from_component_template(
content_data = named_fills_content.get(node.name) content_data = named_fills_content.get(node.name)
slot_name2fill_content[slot_name] = content_data slot_name2fill_content[slot_name] = content_data
# Check: Only component templates that include a 'default' slot # Check: Only component templates that include a 'default' slot
# can be invoked with implicit filling. # can be invoked with implicit filling.
if default_fill_content and not default_slot_encountered: if default_fill_content and not default_slot_encountered:

View file

@ -190,7 +190,7 @@ def do_fill(parser: Parser, token: Token) -> FillNode:
alias_fexp = FilterExpression(alias, parser) alias_fexp = FilterExpression(alias, parser)
else: else:
raise TemplateSyntaxError(f"'{tag}' tag takes either 1 or 3 arguments: Received {len(args)}.") raise TemplateSyntaxError(f"'{tag}' tag takes either 1 or 3 arguments: Received {len(args)}.")
# Use a unique ID to be able to tie the fill nodes with components and slots # Use a unique ID to be able to tie the fill nodes with components and slots
# NOTE: MUST be called BEFORE `parser.parse()` to ensure predictable numbering # NOTE: MUST be called BEFORE `parser.parse()` to ensure predictable numbering
fill_id = gen_id() fill_id = gen_id()

View file

@ -240,7 +240,7 @@ class ComponentTest(SimpleTestCase):
) )
# {{ name }} should be "Jannete" everywhere # {{ name }} should be "Jannete" everywhere
rendered = self.template.render(Context({ "day": "Monday", "name": "Jannete" })) rendered = self.template.render(Context({"day": "Monday", "name": "Jannete"}))
self.assertHTMLEqual( self.assertHTMLEqual(
rendered, rendered,
""" """
@ -537,6 +537,7 @@ class ComponentIsolationTests(SimpleTestCase):
""", """,
) )
class SlotBehaviorTests(SimpleTestCase): class SlotBehaviorTests(SimpleTestCase):
def setUp(self): def setUp(self):
class SlottedComponent(component.Component): class SlottedComponent(component.Component):
@ -578,7 +579,7 @@ class SlotBehaviorTests(SimpleTestCase):
) )
def test_slot_context_allow_override(self): def test_slot_context_allow_override(self):
# {{ name }} should be neither Jannete not empty, because overriden everywhere # {{ name }} should be neither Jannete not empty, because overriden everywhere
rendered = self.template.render(Context({ "day": "Monday", "name": "Jannete" })) rendered = self.template.render(Context({"day": "Monday", "name": "Jannete"}))
self.assertHTMLEqual( self.assertHTMLEqual(
rendered, rendered,
""" """
@ -597,7 +598,7 @@ class SlotBehaviorTests(SimpleTestCase):
) )
# {{ name }} should be effectively the same as before, because overriden everywhere # {{ name }} should be effectively the same as before, because overriden everywhere
rendered2 = self.template.render(Context({ "day": "Monday" })) rendered2 = self.template.render(Context({"day": "Monday"}))
self.assertHTMLEqual(rendered2, rendered) self.assertHTMLEqual(rendered2, rendered)
@override_settings( @override_settings(
@ -605,7 +606,7 @@ class SlotBehaviorTests(SimpleTestCase):
) )
def test_slot_context_isolated(self): def test_slot_context_isolated(self):
# {{ name }} should be "Jannete" everywhere # {{ name }} should be "Jannete" everywhere
rendered = self.template.render(Context({ "day": "Monday", "name": "Jannete" })) rendered = self.template.render(Context({"day": "Monday", "name": "Jannete"}))
self.assertHTMLEqual( self.assertHTMLEqual(
rendered, rendered,
""" """
@ -624,7 +625,7 @@ class SlotBehaviorTests(SimpleTestCase):
) )
# {{ name }} should be empty everywhere # {{ name }} should be empty everywhere
rendered2 = self.template.render(Context({ "day": "Monday" })) rendered2 = self.template.render(Context({"day": "Monday"}))
self.assertHTMLEqual( self.assertHTMLEqual(
rendered2, rendered2,
""" """
@ -644,12 +645,12 @@ class SlotBehaviorTests(SimpleTestCase):
@override_settings( @override_settings(
COMPONENTS={ COMPONENTS={
"slot_context_behavior": "prefer_root", "slot_context_behavior": "prefer_root",
}, },
) )
def test_slot_context_prefer_root(self): def test_slot_context_prefer_root(self):
# {{ name }} should be "Jannete" everywhere # {{ name }} should be "Jannete" everywhere
rendered = self.template.render(Context({ "day": "Monday", "name": "Jannete" })) rendered = self.template.render(Context({"day": "Monday", "name": "Jannete"}))
self.assertHTMLEqual( self.assertHTMLEqual(
rendered, rendered,
""" """
@ -668,7 +669,7 @@ class SlotBehaviorTests(SimpleTestCase):
) )
# {{ name }} should be neither "Jannete" nor empty anywhere # {{ name }} should be neither "Jannete" nor empty anywhere
rendered = self.template.render(Context({ "day": "Monday" })) rendered = self.template.render(Context({"day": "Monday"}))
self.assertHTMLEqual( self.assertHTMLEqual(
rendered, rendered,
""" """