django-components 0.67__tar.gz → 0.68__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {django_components-0.67/src/django_components.egg-info → django_components-0.68}/PKG-INFO +8 -11
- {django_components-0.67 → django_components-0.68}/README.md +6 -6
- {django_components-0.67 → django_components-0.68}/pyproject.toml +2 -5
- {django_components-0.67 → django_components-0.68}/src/django_components/component.py +3 -16
- django_components-0.68/src/django_components/context.py +182 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/slots.py +14 -6
- {django_components-0.67 → django_components-0.68}/src/django_components/utils.py +8 -1
- {django_components-0.67 → django_components-0.68/src/django_components.egg-info}/PKG-INFO +8 -11
- django_components-0.68/src/django_components.egg-info/requires.txt +1 -0
- {django_components-0.67 → django_components-0.68}/tests/test_autodiscover.py +5 -5
- {django_components-0.67 → django_components-0.68}/tests/test_component.py +6 -6
- {django_components-0.67 → django_components-0.68}/tests/test_component_as_view.py +2 -2
- {django_components-0.67 → django_components-0.68}/tests/test_context.py +8 -8
- {django_components-0.67 → django_components-0.68}/tests/test_dependency_rendering.py +2 -3
- {django_components-0.67 → django_components-0.68}/tests/test_settings.py +3 -3
- {django_components-0.67 → django_components-0.68}/tests/test_templatetags.py +140 -15
- {django_components-0.67 → django_components-0.68}/tests/testutils.py +24 -17
- django_components-0.67/src/django_components/context.py +0 -122
- django_components-0.67/src/django_components.egg-info/requires.txt +0 -1
- {django_components-0.67 → django_components-0.68}/LICENSE +0 -0
- {django_components-0.67 → django_components-0.68}/setup.cfg +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/__init__.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/app_settings.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/apps.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/component_registry.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/logger.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/management/__init__.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/management/commands/__init__.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/management/commands/startcomponent.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/management/commands/upgradecomponent.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/middleware.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/node.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/py.typed +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/safer_staticfiles/apps.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/template_loader.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/templatetags/__init__.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/templatetags/component_tags.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components/types.py +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components.egg-info/SOURCES.txt +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.67 → django_components-0.68}/src/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.67 → django_components-0.68}/tests/test_registry.py +0 -0
- {django_components-0.67 → django_components-0.68}/tests/test_startcomponent_command.py +0 -0
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.68
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
5
|
Author-email: Emil Stenström <emil@emilstenstrom.se>
|
|
6
6
|
License: MIT
|
|
7
7
|
Project-URL: Homepage, https://github.com/EmilStenstrom/django-components/
|
|
8
8
|
Keywords: django,components,css,js,html
|
|
9
9
|
Classifier: Framework :: Django
|
|
10
|
-
Classifier: Framework :: Django :: 3.2
|
|
11
|
-
Classifier: Framework :: Django :: 4.0
|
|
12
|
-
Classifier: Framework :: Django :: 4.1
|
|
13
10
|
Classifier: Framework :: Django :: 4.2
|
|
14
11
|
Classifier: Framework :: Django :: 5.0
|
|
15
12
|
Classifier: Operating System :: OS Independent
|
|
@@ -23,7 +20,7 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
23
20
|
Requires-Python: <4.0,>=3.8
|
|
24
21
|
Description-Content-Type: text/markdown
|
|
25
22
|
License-File: LICENSE
|
|
26
|
-
Requires-Dist: Django>=
|
|
23
|
+
Requires-Dist: Django>=4.2
|
|
27
24
|
|
|
28
25
|
# <img src="logo/logo-black-on-white.svg" alt="django-components" style="max-width: 100%; background: white; color: black;">
|
|
29
26
|
<a href="https://github.com/EmilStenstrom/django-components/actions?query=workflow%3A%22Run+tests%22"><img align="right" src="https://github.com/EmilStenstrom/django-components/workflows/Run%20tests/badge.svg" alt="Show test status"></a>
|
|
@@ -145,7 +142,7 @@ STATICFILES_DIRS = [
|
|
|
145
142
|
|
|
146
143
|
### Optional
|
|
147
144
|
|
|
148
|
-
To avoid loading the app in each template using ``` {% load
|
|
145
|
+
To avoid loading the app in each template using ``` {% load component_tags %} ```, you can add the tag as a 'builtin' in settings.py
|
|
149
146
|
|
|
150
147
|
```python
|
|
151
148
|
TEMPLATES = [
|
|
@@ -171,10 +168,10 @@ Django-components supports all supported combinations versions of [Django](https
|
|
|
171
168
|
|
|
172
169
|
| Python version | Django version |
|
|
173
170
|
|----------------|--------------------------|
|
|
174
|
-
| 3.8 |
|
|
175
|
-
| 3.9 |
|
|
176
|
-
| 3.10 |
|
|
177
|
-
| 3.11 | 4.
|
|
171
|
+
| 3.8 | 4.2 |
|
|
172
|
+
| 3.9 | 4.2 |
|
|
173
|
+
| 3.10 | 4.2, 5.0 |
|
|
174
|
+
| 3.11 | 4.2, 5.0 |
|
|
178
175
|
| 3.12 | 4.2, 5.0 |
|
|
179
176
|
|
|
180
177
|
## Create your first component
|
|
@@ -507,7 +504,7 @@ Then, to use this component as a view, you should create a `urls.py` file in you
|
|
|
507
504
|
```python
|
|
508
505
|
# In a file called [project root]/components/urls.py
|
|
509
506
|
from django.urls import path
|
|
510
|
-
from calendar import Calendar
|
|
507
|
+
from components.calendar.calendar import Calendar
|
|
511
508
|
|
|
512
509
|
urlpatterns = [
|
|
513
510
|
path("calendar/", Calendar.as_view()),
|
|
@@ -118,7 +118,7 @@ STATICFILES_DIRS = [
|
|
|
118
118
|
|
|
119
119
|
### Optional
|
|
120
120
|
|
|
121
|
-
To avoid loading the app in each template using ``` {% load
|
|
121
|
+
To avoid loading the app in each template using ``` {% load component_tags %} ```, you can add the tag as a 'builtin' in settings.py
|
|
122
122
|
|
|
123
123
|
```python
|
|
124
124
|
TEMPLATES = [
|
|
@@ -144,10 +144,10 @@ Django-components supports all supported combinations versions of [Django](https
|
|
|
144
144
|
|
|
145
145
|
| Python version | Django version |
|
|
146
146
|
|----------------|--------------------------|
|
|
147
|
-
| 3.8 |
|
|
148
|
-
| 3.9 |
|
|
149
|
-
| 3.10 |
|
|
150
|
-
| 3.11 | 4.
|
|
147
|
+
| 3.8 | 4.2 |
|
|
148
|
+
| 3.9 | 4.2 |
|
|
149
|
+
| 3.10 | 4.2, 5.0 |
|
|
150
|
+
| 3.11 | 4.2, 5.0 |
|
|
151
151
|
| 3.12 | 4.2, 5.0 |
|
|
152
152
|
|
|
153
153
|
## Create your first component
|
|
@@ -480,7 +480,7 @@ Then, to use this component as a view, you should create a `urls.py` file in you
|
|
|
480
480
|
```python
|
|
481
481
|
# In a file called [project root]/components/urls.py
|
|
482
482
|
from django.urls import path
|
|
483
|
-
from calendar import Calendar
|
|
483
|
+
from components.calendar.calendar import Calendar
|
|
484
484
|
|
|
485
485
|
urlpatterns = [
|
|
486
486
|
path("calendar/", Calendar.as_view()),
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "django_components"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.68"
|
|
8
8
|
requires-python = ">=3.8, <4.0"
|
|
9
9
|
description = "A way to create simple reusable template components in Django."
|
|
10
10
|
keywords = ["django", "components", "css", "js", "html"]
|
|
@@ -14,9 +14,6 @@ authors = [
|
|
|
14
14
|
]
|
|
15
15
|
classifiers = [
|
|
16
16
|
"Framework :: Django",
|
|
17
|
-
"Framework :: Django :: 3.2",
|
|
18
|
-
"Framework :: Django :: 4.0",
|
|
19
|
-
"Framework :: Django :: 4.1",
|
|
20
17
|
"Framework :: Django :: 4.2",
|
|
21
18
|
"Framework :: Django :: 5.0",
|
|
22
19
|
"Operating System :: OS Independent",
|
|
@@ -29,7 +26,7 @@ classifiers = [
|
|
|
29
26
|
"Programming Language :: Python :: 3.12",
|
|
30
27
|
]
|
|
31
28
|
dependencies = [
|
|
32
|
-
'Django>=
|
|
29
|
+
'Django>=4.2',
|
|
33
30
|
]
|
|
34
31
|
license = {text = "MIT"}
|
|
35
32
|
|
|
@@ -20,12 +20,7 @@ from django.views import View
|
|
|
20
20
|
# way the two modules depend on one another.
|
|
21
21
|
from django_components.component_registry import registry # NOQA
|
|
22
22
|
from django_components.component_registry import AlreadyRegistered, ComponentRegistry, NotRegistered, register # NOQA
|
|
23
|
-
from django_components.context import
|
|
24
|
-
capture_root_context,
|
|
25
|
-
get_root_context,
|
|
26
|
-
set_root_context,
|
|
27
|
-
set_slot_component_association,
|
|
28
|
-
)
|
|
23
|
+
from django_components.context import make_isolated_context_copy, prepare_context, set_slot_component_association
|
|
29
24
|
from django_components.logger import logger, trace_msg
|
|
30
25
|
from django_components.middleware import is_dependency_middleware_active
|
|
31
26
|
from django_components.node import walk_nodelist
|
|
@@ -268,6 +263,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
268
263
|
# NOTE: This if/else is important to avoid nested Contexts,
|
|
269
264
|
# See https://github.com/EmilStenstrom/django-components/issues/414
|
|
270
265
|
context = context_data if isinstance(context_data, Context) else Context(context_data)
|
|
266
|
+
prepare_context(context, outer_context=self.outer_context or Context())
|
|
271
267
|
template = self.get_template(context)
|
|
272
268
|
|
|
273
269
|
# Associate the slots with this component for this context
|
|
@@ -347,10 +343,6 @@ class ComponentNode(Node):
|
|
|
347
343
|
resolved_component_name = self.name_fexp.resolve(context)
|
|
348
344
|
component_cls: Type[Component] = registry.get(resolved_component_name)
|
|
349
345
|
|
|
350
|
-
# If this is the outer-/top-most component node, then save the outer context,
|
|
351
|
-
# so it can be used by nested Slots.
|
|
352
|
-
capture_root_context(context)
|
|
353
|
-
|
|
354
346
|
# Resolve FilterExpressions and Variables that were passed as args to the
|
|
355
347
|
# component, then call component's context method
|
|
356
348
|
# to get values to insert into the context
|
|
@@ -380,12 +372,7 @@ class ComponentNode(Node):
|
|
|
380
372
|
|
|
381
373
|
# Prevent outer context from leaking into the template of the component
|
|
382
374
|
if self.isolated_context:
|
|
383
|
-
|
|
384
|
-
# original context so variables in slots can be rendered using
|
|
385
|
-
# the original context.
|
|
386
|
-
root_ctx = get_root_context(context)
|
|
387
|
-
context = context.new()
|
|
388
|
-
set_root_context(context, root_ctx)
|
|
375
|
+
context = make_isolated_context_copy(context)
|
|
389
376
|
|
|
390
377
|
with context.update(component_context):
|
|
391
378
|
rendered_component = component.render(context)
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This file centralizes various ways we use Django's Context class
|
|
3
|
+
pass data across components, nodes, slots, and contexts.
|
|
4
|
+
|
|
5
|
+
You can think of the Context as our storage system.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from typing import TYPE_CHECKING, Optional
|
|
9
|
+
|
|
10
|
+
from django.template import Context
|
|
11
|
+
|
|
12
|
+
from django_components.logger import trace_msg
|
|
13
|
+
from django_components.utils import find_last_index
|
|
14
|
+
|
|
15
|
+
if TYPE_CHECKING:
|
|
16
|
+
from django_components.slots import FillContent
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
_FILLED_SLOTS_CONTENT_CONTEXT_KEY = "_DJANGO_COMPONENTS_FILLED_SLOTS"
|
|
20
|
+
_OUTER_ROOT_CTX_CONTEXT_KEY = "_DJANGO_COMPONENTS_OUTER_ROOT_CTX"
|
|
21
|
+
_SLOT_COMPONENT_ASSOC_KEY = "_DJANGO_COMPONENTS_SLOT_COMP_ASSOC"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def prepare_context(context: Context, outer_context: Optional[Context]) -> None:
|
|
25
|
+
"""Initialize the internal context state."""
|
|
26
|
+
# This is supposed to run ALWAYS at Component.render
|
|
27
|
+
if outer_context is not None:
|
|
28
|
+
set_outer_root_context(context, outer_context)
|
|
29
|
+
|
|
30
|
+
# Initialize mapping dicts within this rendering run.
|
|
31
|
+
# This is shared across the whole render chain, thus we set it only once.
|
|
32
|
+
if _SLOT_COMPONENT_ASSOC_KEY not in context:
|
|
33
|
+
context[_SLOT_COMPONENT_ASSOC_KEY] = {}
|
|
34
|
+
if _FILLED_SLOTS_CONTENT_CONTEXT_KEY not in context:
|
|
35
|
+
context[_FILLED_SLOTS_CONTENT_CONTEXT_KEY] = {}
|
|
36
|
+
|
|
37
|
+
# If we're inside a forloop, we need to make a disposable copy of slot -> comp
|
|
38
|
+
# mapping, which can be modified in the loop. We do so by copying it onto the latest
|
|
39
|
+
# context layer.
|
|
40
|
+
#
|
|
41
|
+
# This is necessary, because otherwise if we have a nested loop with a same
|
|
42
|
+
# component used recursively, the inner slot -> comp mapping would leak into the outer.
|
|
43
|
+
#
|
|
44
|
+
# NOTE: If you ever need to debug this, insert a print/debug statement into
|
|
45
|
+
# `django.template.defaulttags.ForNode.render` to inspect the context object
|
|
46
|
+
# inside the for loop.
|
|
47
|
+
if "forloop" in context:
|
|
48
|
+
context.dicts[-1][_SLOT_COMPONENT_ASSOC_KEY] = context[_SLOT_COMPONENT_ASSOC_KEY].copy()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def make_isolated_context_copy(context: Context) -> Context:
|
|
52
|
+
# Even if contexts are isolated, we still need to pass down the
|
|
53
|
+
# metadata so variables in slots can be rendered using the correct context.
|
|
54
|
+
root_ctx = get_outer_root_context(context)
|
|
55
|
+
slot_assoc = context.get(_SLOT_COMPONENT_ASSOC_KEY, {})
|
|
56
|
+
slot_fills = context.get(_FILLED_SLOTS_CONTENT_CONTEXT_KEY, {})
|
|
57
|
+
|
|
58
|
+
context_copy = context.new()
|
|
59
|
+
context_copy[_SLOT_COMPONENT_ASSOC_KEY] = slot_assoc
|
|
60
|
+
context_copy[_FILLED_SLOTS_CONTENT_CONTEXT_KEY] = slot_fills
|
|
61
|
+
set_outer_root_context(context_copy, root_ctx)
|
|
62
|
+
copy_forloop_context(context, context_copy)
|
|
63
|
+
|
|
64
|
+
return context_copy
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def get_slot_fill(context: Context, component_id: str, slot_name: str) -> Optional["FillContent"]:
|
|
68
|
+
"""
|
|
69
|
+
Use this function to obtain a slot fill from the current context.
|
|
70
|
+
|
|
71
|
+
See `set_slot_fill` for more details.
|
|
72
|
+
"""
|
|
73
|
+
trace_msg("GET", "FILL", slot_name, component_id)
|
|
74
|
+
slot_key = f"{component_id}__{slot_name}"
|
|
75
|
+
return context[_FILLED_SLOTS_CONTENT_CONTEXT_KEY].get(slot_key, None)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def set_slot_fill(context: Context, component_id: str, slot_name: str, value: "FillContent") -> None:
|
|
79
|
+
"""
|
|
80
|
+
Use this function to set a slot fill for the current context.
|
|
81
|
+
|
|
82
|
+
Note that we make use of the fact that Django's Context is a stack - we can push and pop
|
|
83
|
+
extra contexts on top others.
|
|
84
|
+
"""
|
|
85
|
+
trace_msg("SET", "FILL", slot_name, component_id)
|
|
86
|
+
slot_key = f"{component_id}__{slot_name}"
|
|
87
|
+
context[_FILLED_SLOTS_CONTENT_CONTEXT_KEY][slot_key] = value
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def get_outer_root_context(context: Context) -> Optional[Context]:
|
|
91
|
+
"""
|
|
92
|
+
Use this function to get the outer root context.
|
|
93
|
+
|
|
94
|
+
See `set_outer_root_context` for more details.
|
|
95
|
+
"""
|
|
96
|
+
return context.get(_OUTER_ROOT_CTX_CONTEXT_KEY)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def set_outer_root_context(context: Context, outer_ctx: Optional[Context]) -> None:
|
|
100
|
+
"""
|
|
101
|
+
Use this function to set the outer root context.
|
|
102
|
+
|
|
103
|
+
When we consider a component's template, then outer context is the context
|
|
104
|
+
that was available just outside of the component's template (AKA it was in
|
|
105
|
+
the PARENT template).
|
|
106
|
+
|
|
107
|
+
Once we have the outer context, next we get the outer ROOT context. This is
|
|
108
|
+
the context that was available at the top level of the PARENT template.
|
|
109
|
+
|
|
110
|
+
We pass through this context to allow to configure how slot fills should be
|
|
111
|
+
rendered using the `SLOT_CONTEXT_BEHAVIOR` setting.
|
|
112
|
+
"""
|
|
113
|
+
if outer_ctx and len(outer_ctx.dicts) > 1:
|
|
114
|
+
outer_root_context: Context = outer_ctx.new()
|
|
115
|
+
# NOTE_1:
|
|
116
|
+
# - Index 0 are the defaults set in BaseContext
|
|
117
|
+
# - Index 1 is the context generated by `Component.get_context_data`
|
|
118
|
+
# of the parent's component
|
|
119
|
+
# - All later indices (2, 3, ...) are extra layers added by the rendering
|
|
120
|
+
# logic (each Node usually adds it's own context layer)
|
|
121
|
+
outer_root_context.push(outer_ctx.dicts[1])
|
|
122
|
+
else:
|
|
123
|
+
outer_root_context = Context()
|
|
124
|
+
|
|
125
|
+
# Include the mappings.
|
|
126
|
+
if _SLOT_COMPONENT_ASSOC_KEY in context:
|
|
127
|
+
outer_root_context[_SLOT_COMPONENT_ASSOC_KEY] = context[_SLOT_COMPONENT_ASSOC_KEY]
|
|
128
|
+
if _FILLED_SLOTS_CONTENT_CONTEXT_KEY in context:
|
|
129
|
+
outer_root_context[_FILLED_SLOTS_CONTENT_CONTEXT_KEY] = context[_FILLED_SLOTS_CONTENT_CONTEXT_KEY]
|
|
130
|
+
|
|
131
|
+
context[_OUTER_ROOT_CTX_CONTEXT_KEY] = outer_root_context
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def set_slot_component_association(
|
|
135
|
+
context: Context,
|
|
136
|
+
slot_id: str,
|
|
137
|
+
component_id: str,
|
|
138
|
+
) -> None:
|
|
139
|
+
"""
|
|
140
|
+
Set association between a Slot and a Component in the current context.
|
|
141
|
+
|
|
142
|
+
We use SlotNodes to render slot fills. SlotNodes are created only at Template
|
|
143
|
+
parse time.
|
|
144
|
+
However, when we refer to components with slots in (another) template (using
|
|
145
|
+
`{% component %}`), we can render the same component multiple time. So we can
|
|
146
|
+
have multiple FillNodes intended to be used with the same SlotNode.
|
|
147
|
+
|
|
148
|
+
So how do we tell the SlotNode which FillNode to render? We do so by tagging
|
|
149
|
+
the ComponentNode and FillNodes with a unique component_id, which ties them
|
|
150
|
+
together. And then we tell SlotNode which component_id to use to be able to
|
|
151
|
+
find the correct Component/Fill.
|
|
152
|
+
|
|
153
|
+
We don't want to store this info on the Nodes themselves, as we need to treat
|
|
154
|
+
them as immutable due to caching of Templates by Django.
|
|
155
|
+
|
|
156
|
+
Hence, we use the Context to store the associations of SlotNode <-> Component
|
|
157
|
+
for the current context stack.
|
|
158
|
+
"""
|
|
159
|
+
context[_SLOT_COMPONENT_ASSOC_KEY][slot_id] = component_id
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def get_slot_component_association(context: Context, slot_id: str) -> str:
|
|
163
|
+
"""
|
|
164
|
+
Given a slot ID, get the component ID that this slot is associated with
|
|
165
|
+
in this context.
|
|
166
|
+
|
|
167
|
+
See `set_slot_component_association` for more details.
|
|
168
|
+
"""
|
|
169
|
+
return context[_SLOT_COMPONENT_ASSOC_KEY][slot_id]
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def copy_forloop_context(from_context: Context, to_context: Context) -> None:
|
|
173
|
+
"""Forward the info about the current loop"""
|
|
174
|
+
# Note that the ForNode (which implements for loop behavior) does not
|
|
175
|
+
# only add the `forloop` key, but also keys corresponding to the loop elements
|
|
176
|
+
# So if the loop syntax is `{% for my_val in my_lists %}`, then ForNode also
|
|
177
|
+
# sets a `my_val` key.
|
|
178
|
+
# For this reason, instead of copying individual keys, we copy the whole stack layer
|
|
179
|
+
# set by ForNode.
|
|
180
|
+
if "forloop" in from_context:
|
|
181
|
+
forloop_dict_index = find_last_index(from_context.dicts, lambda d: "forloop" in d)
|
|
182
|
+
to_context.update(from_context.dicts[forloop_dict_index])
|
|
@@ -10,7 +10,13 @@ from django.template.exceptions import TemplateSyntaxError
|
|
|
10
10
|
from django.utils.safestring import SafeString, mark_safe
|
|
11
11
|
|
|
12
12
|
from django_components.app_settings import SlotContextBehavior, app_settings
|
|
13
|
-
from django_components.context import
|
|
13
|
+
from django_components.context import (
|
|
14
|
+
copy_forloop_context,
|
|
15
|
+
get_outer_root_context,
|
|
16
|
+
get_slot_component_association,
|
|
17
|
+
get_slot_fill,
|
|
18
|
+
set_slot_fill,
|
|
19
|
+
)
|
|
14
20
|
from django_components.logger import trace_msg
|
|
15
21
|
from django_components.node import nodelist_has_content
|
|
16
22
|
from django_components.utils import gen_id
|
|
@@ -139,14 +145,16 @@ class SlotNode(Node):
|
|
|
139
145
|
|
|
140
146
|
See SlotContextBehavior for the description of each option.
|
|
141
147
|
"""
|
|
142
|
-
root_ctx =
|
|
148
|
+
root_ctx = get_outer_root_context(context) or Context()
|
|
143
149
|
|
|
144
150
|
if app_settings.SLOT_CONTEXT_BEHAVIOR == SlotContextBehavior.ALLOW_OVERRIDE:
|
|
145
151
|
return context
|
|
146
152
|
elif app_settings.SLOT_CONTEXT_BEHAVIOR == SlotContextBehavior.ISOLATED:
|
|
147
|
-
|
|
153
|
+
new_context: Context = copy(root_ctx)
|
|
154
|
+
copy_forloop_context(context, new_context)
|
|
155
|
+
return new_context
|
|
148
156
|
elif app_settings.SLOT_CONTEXT_BEHAVIOR == SlotContextBehavior.PREFER_ROOT:
|
|
149
|
-
new_context
|
|
157
|
+
new_context = copy(context)
|
|
150
158
|
new_context.update(root_ctx.flatten())
|
|
151
159
|
return new_context
|
|
152
160
|
else:
|
|
@@ -483,8 +491,8 @@ def _report_slot_errors(
|
|
|
483
491
|
for fill_name in unmatched_fills:
|
|
484
492
|
fuzzy_slot_name_matches = difflib.get_close_matches(fill_name, unfilled_slots, n=1, cutoff=0.7)
|
|
485
493
|
msg = (
|
|
486
|
-
f"Component '{registered_name}' passed fill "
|
|
487
|
-
f"
|
|
494
|
+
f"Component '{registered_name}' passed fill that refers to undefined slot:"
|
|
495
|
+
f" '{fill_name}'."
|
|
488
496
|
f"\nUnfilled slot names are: {sorted(unfilled_slots)}."
|
|
489
497
|
)
|
|
490
498
|
if fuzzy_slot_name_matches:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import glob
|
|
2
2
|
from pathlib import Path
|
|
3
|
-
from typing import List, NamedTuple, Optional
|
|
3
|
+
from typing import Any, Callable, List, NamedTuple, Optional
|
|
4
4
|
|
|
5
5
|
from django.template.engine import Engine
|
|
6
6
|
|
|
@@ -49,3 +49,10 @@ def gen_id(length: int = 5) -> str:
|
|
|
49
49
|
|
|
50
50
|
# Pad the ID with `0`s up to 4 digits, e.g. `0007`
|
|
51
51
|
return f"{_id:04}"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def find_last_index(lst: List, predicate: Callable[[Any], bool]) -> Any:
|
|
55
|
+
for r_idx, elem in enumerate(reversed(lst)):
|
|
56
|
+
if predicate(elem):
|
|
57
|
+
return len(lst) - 1 - r_idx
|
|
58
|
+
return -1
|
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.68
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
5
|
Author-email: Emil Stenström <emil@emilstenstrom.se>
|
|
6
6
|
License: MIT
|
|
7
7
|
Project-URL: Homepage, https://github.com/EmilStenstrom/django-components/
|
|
8
8
|
Keywords: django,components,css,js,html
|
|
9
9
|
Classifier: Framework :: Django
|
|
10
|
-
Classifier: Framework :: Django :: 3.2
|
|
11
|
-
Classifier: Framework :: Django :: 4.0
|
|
12
|
-
Classifier: Framework :: Django :: 4.1
|
|
13
10
|
Classifier: Framework :: Django :: 4.2
|
|
14
11
|
Classifier: Framework :: Django :: 5.0
|
|
15
12
|
Classifier: Operating System :: OS Independent
|
|
@@ -23,7 +20,7 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
23
20
|
Requires-Python: <4.0,>=3.8
|
|
24
21
|
Description-Content-Type: text/markdown
|
|
25
22
|
License-File: LICENSE
|
|
26
|
-
Requires-Dist: Django>=
|
|
23
|
+
Requires-Dist: Django>=4.2
|
|
27
24
|
|
|
28
25
|
# <img src="logo/logo-black-on-white.svg" alt="django-components" style="max-width: 100%; background: white; color: black;">
|
|
29
26
|
<a href="https://github.com/EmilStenstrom/django-components/actions?query=workflow%3A%22Run+tests%22"><img align="right" src="https://github.com/EmilStenstrom/django-components/workflows/Run%20tests/badge.svg" alt="Show test status"></a>
|
|
@@ -145,7 +142,7 @@ STATICFILES_DIRS = [
|
|
|
145
142
|
|
|
146
143
|
### Optional
|
|
147
144
|
|
|
148
|
-
To avoid loading the app in each template using ``` {% load
|
|
145
|
+
To avoid loading the app in each template using ``` {% load component_tags %} ```, you can add the tag as a 'builtin' in settings.py
|
|
149
146
|
|
|
150
147
|
```python
|
|
151
148
|
TEMPLATES = [
|
|
@@ -171,10 +168,10 @@ Django-components supports all supported combinations versions of [Django](https
|
|
|
171
168
|
|
|
172
169
|
| Python version | Django version |
|
|
173
170
|
|----------------|--------------------------|
|
|
174
|
-
| 3.8 |
|
|
175
|
-
| 3.9 |
|
|
176
|
-
| 3.10 |
|
|
177
|
-
| 3.11 | 4.
|
|
171
|
+
| 3.8 | 4.2 |
|
|
172
|
+
| 3.9 | 4.2 |
|
|
173
|
+
| 3.10 | 4.2, 5.0 |
|
|
174
|
+
| 3.11 | 4.2, 5.0 |
|
|
178
175
|
| 3.12 | 4.2, 5.0 |
|
|
179
176
|
|
|
180
177
|
## Create your first component
|
|
@@ -507,7 +504,7 @@ Then, to use this component as a view, you should create a `urls.py` file in you
|
|
|
507
504
|
```python
|
|
508
505
|
# In a file called [project root]/components/urls.py
|
|
509
506
|
from django.urls import path
|
|
510
|
-
from calendar import Calendar
|
|
507
|
+
from components.calendar.calendar import Calendar
|
|
511
508
|
|
|
512
509
|
urlpatterns = [
|
|
513
510
|
path("calendar/", Calendar.as_view()),
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Django>=4.2
|
|
@@ -6,7 +6,7 @@ from django.urls import include, path
|
|
|
6
6
|
|
|
7
7
|
# isort: off
|
|
8
8
|
from .django_test_setup import settings
|
|
9
|
-
from .testutils import
|
|
9
|
+
from .testutils import BaseTestCase
|
|
10
10
|
|
|
11
11
|
# isort: on
|
|
12
12
|
|
|
@@ -18,7 +18,7 @@ urlpatterns = [
|
|
|
18
18
|
]
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
class TestAutodiscover(
|
|
21
|
+
class TestAutodiscover(BaseTestCase):
|
|
22
22
|
def setUp(self):
|
|
23
23
|
settings.SETTINGS_MODULE = "tests.test_autodiscover" # noqa
|
|
24
24
|
|
|
@@ -38,7 +38,7 @@ class TestAutodiscover(SimpleTestCase):
|
|
|
38
38
|
self.assertEqual(imported_components_count, 1)
|
|
39
39
|
|
|
40
40
|
|
|
41
|
-
class TestLoaderSettingsModule(
|
|
41
|
+
class TestLoaderSettingsModule(BaseTestCase):
|
|
42
42
|
def tearDown(self) -> None:
|
|
43
43
|
del settings.SETTINGS_MODULE # noqa
|
|
44
44
|
|
|
@@ -106,7 +106,7 @@ class TestLoaderSettingsModule(SimpleTestCase):
|
|
|
106
106
|
)
|
|
107
107
|
|
|
108
108
|
|
|
109
|
-
class TestBaseDir(
|
|
109
|
+
class TestBaseDir(BaseTestCase):
|
|
110
110
|
def setUp(self):
|
|
111
111
|
settings.BASE_DIR = Path(__file__).parent.resolve() / "test_structures" / "test_structure_1" # noqa
|
|
112
112
|
settings.SETTINGS_MODULE = "tests_fake.test_autodiscover_fake" # noqa
|
|
@@ -125,7 +125,7 @@ class TestBaseDir(SimpleTestCase):
|
|
|
125
125
|
self.assertEqual(sorted(dirs), sorted(expected))
|
|
126
126
|
|
|
127
127
|
|
|
128
|
-
class TestFilepathToPythonModule(
|
|
128
|
+
class TestFilepathToPythonModule(BaseTestCase):
|
|
129
129
|
def test_prepares_path(self):
|
|
130
130
|
self.assertEqual(
|
|
131
131
|
_filepath_to_python_module(Path("tests.py")),
|
|
@@ -8,14 +8,14 @@ from django.test import override_settings
|
|
|
8
8
|
|
|
9
9
|
# isort: off
|
|
10
10
|
from .django_test_setup import * # NOQA
|
|
11
|
-
from .testutils import
|
|
11
|
+
from .testutils import BaseTestCase
|
|
12
12
|
|
|
13
13
|
# isort: on
|
|
14
14
|
|
|
15
15
|
from django_components import component
|
|
16
16
|
|
|
17
17
|
|
|
18
|
-
class ComponentTest(
|
|
18
|
+
class ComponentTest(BaseTestCase):
|
|
19
19
|
def test_empty_component(self):
|
|
20
20
|
class EmptyComponent(component.Component):
|
|
21
21
|
pass
|
|
@@ -260,7 +260,7 @@ class ComponentTest(SimpleTestCase):
|
|
|
260
260
|
)
|
|
261
261
|
|
|
262
262
|
|
|
263
|
-
class InlineComponentTest(
|
|
263
|
+
class InlineComponentTest(BaseTestCase):
|
|
264
264
|
def test_inline_html_component(self):
|
|
265
265
|
class InlineHTMLComponent(component.Component):
|
|
266
266
|
template = "<div class='inline'>Hello Inline</div>"
|
|
@@ -382,7 +382,7 @@ class InlineComponentTest(SimpleTestCase):
|
|
|
382
382
|
)
|
|
383
383
|
|
|
384
384
|
|
|
385
|
-
class ComponentMediaTests(
|
|
385
|
+
class ComponentMediaTests(BaseTestCase):
|
|
386
386
|
def test_component_media_with_strings(self):
|
|
387
387
|
class SimpleComponent(component.Component):
|
|
388
388
|
class Media:
|
|
@@ -491,7 +491,7 @@ class ComponentMediaTests(SimpleTestCase):
|
|
|
491
491
|
)
|
|
492
492
|
|
|
493
493
|
|
|
494
|
-
class ComponentIsolationTests(
|
|
494
|
+
class ComponentIsolationTests(BaseTestCase):
|
|
495
495
|
def setUp(self):
|
|
496
496
|
class SlottedComponent(component.Component):
|
|
497
497
|
template_name = "slotted_template.html"
|
|
@@ -539,7 +539,7 @@ class ComponentIsolationTests(SimpleTestCase):
|
|
|
539
539
|
)
|
|
540
540
|
|
|
541
541
|
|
|
542
|
-
class SlotBehaviorTests(
|
|
542
|
+
class SlotBehaviorTests(BaseTestCase):
|
|
543
543
|
def setUp(self):
|
|
544
544
|
class SlottedComponent(component.Component):
|
|
545
545
|
template_name = "slotted_template.html"
|
|
@@ -7,7 +7,7 @@ from django.urls import include, path
|
|
|
7
7
|
|
|
8
8
|
# isort: off
|
|
9
9
|
from .django_test_setup import * # noqa
|
|
10
|
-
from .testutils import
|
|
10
|
+
from .testutils import BaseTestCase
|
|
11
11
|
|
|
12
12
|
# isort: on
|
|
13
13
|
|
|
@@ -110,7 +110,7 @@ class CustomClient(Client):
|
|
|
110
110
|
super().__init__(*args, **kwargs)
|
|
111
111
|
|
|
112
112
|
|
|
113
|
-
class TestComponentAsView(
|
|
113
|
+
class TestComponentAsView(BaseTestCase):
|
|
114
114
|
def setUp(self):
|
|
115
115
|
self.client = CustomClient()
|
|
116
116
|
|
|
@@ -6,7 +6,7 @@ from django.test import override_settings
|
|
|
6
6
|
from django_components import component
|
|
7
7
|
|
|
8
8
|
from .django_test_setup import * # NOQA
|
|
9
|
-
from .testutils import
|
|
9
|
+
from .testutils import BaseTestCase
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
class SimpleComponent(component.Component):
|
|
@@ -77,7 +77,7 @@ component.registry.register(name="simple_component", component=SimpleComponent)
|
|
|
77
77
|
component.registry.register(name="outer_context_component", component=OuterContextComponent)
|
|
78
78
|
|
|
79
79
|
|
|
80
|
-
class ContextTests(
|
|
80
|
+
class ContextTests(BaseTestCase):
|
|
81
81
|
def test_nested_component_context_shadows_parent_with_unfilled_slots_and_component_tag(
|
|
82
82
|
self,
|
|
83
83
|
):
|
|
@@ -198,7 +198,7 @@ class ContextTests(SimpleTestCase):
|
|
|
198
198
|
self.assertNotIn("<h1>Shadowing variable = NOT SHADOWED</h1>", rendered, rendered)
|
|
199
199
|
|
|
200
200
|
|
|
201
|
-
class ParentArgsTests(
|
|
201
|
+
class ParentArgsTests(BaseTestCase):
|
|
202
202
|
def test_parent_args_can_be_drawn_from_context(self):
|
|
203
203
|
template = Template(
|
|
204
204
|
"{% load component_tags %}{% component_dependencies %}"
|
|
@@ -241,7 +241,7 @@ class ParentArgsTests(SimpleTestCase):
|
|
|
241
241
|
self.assertNotIn("<h1>Shadowing variable = NOT SHADOWED</h1>", rendered, rendered)
|
|
242
242
|
|
|
243
243
|
|
|
244
|
-
class ContextCalledOnceTests(
|
|
244
|
+
class ContextCalledOnceTests(BaseTestCase):
|
|
245
245
|
def test_one_context_call_with_simple_component(self):
|
|
246
246
|
template = Template(
|
|
247
247
|
"{% load component_tags %}{% component_dependencies %}"
|
|
@@ -302,7 +302,7 @@ class ContextCalledOnceTests(SimpleTestCase):
|
|
|
302
302
|
)
|
|
303
303
|
|
|
304
304
|
|
|
305
|
-
class ComponentsCanAccessOuterContext(
|
|
305
|
+
class ComponentsCanAccessOuterContext(BaseTestCase):
|
|
306
306
|
def test_simple_component_can_use_outer_context(self):
|
|
307
307
|
template = Template(
|
|
308
308
|
"{% load component_tags %}{% component_dependencies %}"
|
|
@@ -312,7 +312,7 @@ class ComponentsCanAccessOuterContext(SimpleTestCase):
|
|
|
312
312
|
self.assertIn("outer_value", rendered, rendered)
|
|
313
313
|
|
|
314
314
|
|
|
315
|
-
class IsolatedContextTests(
|
|
315
|
+
class IsolatedContextTests(BaseTestCase):
|
|
316
316
|
def test_simple_component_can_pass_outer_context_in_args(self):
|
|
317
317
|
template = Template(
|
|
318
318
|
"{% load component_tags %}{% component_dependencies %}"
|
|
@@ -330,7 +330,7 @@ class IsolatedContextTests(SimpleTestCase):
|
|
|
330
330
|
self.assertNotIn("outer_value", rendered, rendered)
|
|
331
331
|
|
|
332
332
|
|
|
333
|
-
class IsolatedContextSettingTests(
|
|
333
|
+
class IsolatedContextSettingTests(BaseTestCase):
|
|
334
334
|
def setUp(self):
|
|
335
335
|
self.patcher = patch(
|
|
336
336
|
"django_components.app_settings.AppSettings.CONTEXT_BEHAVIOR",
|
|
@@ -385,7 +385,7 @@ class IsolatedContextSettingTests(SimpleTestCase):
|
|
|
385
385
|
self.assertNotIn("outer_value", rendered, rendered)
|
|
386
386
|
|
|
387
387
|
|
|
388
|
-
class OuterContextPropertyTests(
|
|
388
|
+
class OuterContextPropertyTests(BaseTestCase):
|
|
389
389
|
@override_settings(
|
|
390
390
|
COMPONENTS={"context_behavior": "global"},
|
|
391
391
|
)
|
|
@@ -9,8 +9,7 @@ from django_components.middleware import ComponentDependencyMiddleware
|
|
|
9
9
|
|
|
10
10
|
from .django_test_setup import * # NOQA
|
|
11
11
|
from .test_templatetags import SimpleComponent
|
|
12
|
-
from .testutils import
|
|
13
|
-
from .testutils import create_and_process_template_response
|
|
12
|
+
from .testutils import BaseTestCase, create_and_process_template_response
|
|
14
13
|
|
|
15
14
|
|
|
16
15
|
class SimpleComponentAlternate(component.Component):
|
|
@@ -44,7 +43,7 @@ class MultistyleComponent(component.Component):
|
|
|
44
43
|
|
|
45
44
|
|
|
46
45
|
@override_settings(COMPONENTS={"RENDER_DEPENDENCIES": True})
|
|
47
|
-
class ComponentMediaRenderingTests(
|
|
46
|
+
class ComponentMediaRenderingTests(BaseTestCase):
|
|
48
47
|
def setUp(self):
|
|
49
48
|
# NOTE: component.registry is global, so need to clear before each test
|
|
50
49
|
component.registry.clear()
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
from django.conf import settings
|
|
2
2
|
|
|
3
3
|
from .django_test_setup import * # NOQA
|
|
4
|
-
from .testutils import
|
|
4
|
+
from .testutils import BaseTestCase
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
class ValidateWrongContextBehaviorValueTestCase(
|
|
7
|
+
class ValidateWrongContextBehaviorValueTestCase(BaseTestCase):
|
|
8
8
|
def setUp(self) -> None:
|
|
9
9
|
settings.COMPONENTS["context_behavior"] = "invalid_value"
|
|
10
10
|
return super().setUp()
|
|
@@ -20,7 +20,7 @@ class ValidateWrongContextBehaviorValueTestCase(SimpleTestCase):
|
|
|
20
20
|
app_settings.CONTEXT_BEHAVIOR
|
|
21
21
|
|
|
22
22
|
|
|
23
|
-
class ValidateCorrectContextBehaviorValueTestCase(
|
|
23
|
+
class ValidateCorrectContextBehaviorValueTestCase(BaseTestCase):
|
|
24
24
|
def setUp(self) -> None:
|
|
25
25
|
settings.COMPONENTS["context_behavior"] = "isolated"
|
|
26
26
|
return super().setUp()
|
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import re
|
|
2
2
|
import textwrap
|
|
3
|
-
from typing import Callable, Optional
|
|
3
|
+
from typing import Any, Callable, Dict, Optional
|
|
4
4
|
|
|
5
5
|
from django.template import Context, Template, TemplateSyntaxError
|
|
6
|
+
from django.test import override_settings
|
|
6
7
|
|
|
7
8
|
# isort: off
|
|
8
9
|
from .django_test_setup import * # NOQA
|
|
9
|
-
from .testutils import
|
|
10
|
+
from .testutils import BaseTestCase
|
|
10
11
|
|
|
11
12
|
# isort: on
|
|
12
13
|
|
|
@@ -85,7 +86,35 @@ class ComponentWithDefaultAndRequiredSlot(component.Component):
|
|
|
85
86
|
template_name = "template_with_default_and_required_slot.html"
|
|
86
87
|
|
|
87
88
|
|
|
88
|
-
class
|
|
89
|
+
class _ComplexChildComponent(component.Component):
|
|
90
|
+
template = """
|
|
91
|
+
{% load component_tags %}
|
|
92
|
+
<div>
|
|
93
|
+
{% slot "content" default %}
|
|
94
|
+
No slot!
|
|
95
|
+
{% endslot %}
|
|
96
|
+
</div>
|
|
97
|
+
"""
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class _ComplexParentComponent(component.Component):
|
|
101
|
+
template = """
|
|
102
|
+
{% load component_tags %}
|
|
103
|
+
ITEMS: {{ items }}
|
|
104
|
+
{% for item in items %}
|
|
105
|
+
<li>
|
|
106
|
+
{% component "complex_child" %}
|
|
107
|
+
{{ item.value }}
|
|
108
|
+
{% endcomponent %}
|
|
109
|
+
</li>
|
|
110
|
+
{% endfor %}
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
def get_context_data(self, items, *args, **kwargs) -> Dict[str, Any]:
|
|
114
|
+
return {"items": items}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
class ComponentTemplateTagTest(BaseTestCase):
|
|
89
118
|
def setUp(self):
|
|
90
119
|
# NOTE: component.registry is global, so need to clear before each test
|
|
91
120
|
component.registry.clear()
|
|
@@ -202,7 +231,7 @@ class ComponentTemplateTagTest(SimpleTestCase):
|
|
|
202
231
|
template.render(Context({}))
|
|
203
232
|
|
|
204
233
|
|
|
205
|
-
class ComponentSlottedTemplateTagTest(
|
|
234
|
+
class ComponentSlottedTemplateTagTest(BaseTestCase):
|
|
206
235
|
def setUp(self):
|
|
207
236
|
# NOTE: component.registry is global, so need to clear before each test
|
|
208
237
|
component.registry.clear()
|
|
@@ -522,7 +551,7 @@ class ComponentSlottedTemplateTagTest(SimpleTestCase):
|
|
|
522
551
|
raise e
|
|
523
552
|
|
|
524
553
|
|
|
525
|
-
class SlottedTemplateRegressionTests(
|
|
554
|
+
class SlottedTemplateRegressionTests(BaseTestCase):
|
|
526
555
|
def setUp(self):
|
|
527
556
|
# NOTE: component.registry is global, so need to clear before each test
|
|
528
557
|
component.registry.clear()
|
|
@@ -565,7 +594,7 @@ class SlottedTemplateRegressionTests(SimpleTestCase):
|
|
|
565
594
|
)
|
|
566
595
|
|
|
567
596
|
|
|
568
|
-
class MultiComponentTests(
|
|
597
|
+
class MultiComponentTests(BaseTestCase):
|
|
569
598
|
def setUp(self):
|
|
570
599
|
component.registry.clear()
|
|
571
600
|
|
|
@@ -623,7 +652,7 @@ class MultiComponentTests(SimpleTestCase):
|
|
|
623
652
|
self.assertHTMLEqual(rendered, self.expected_result("", second_slot_content))
|
|
624
653
|
|
|
625
654
|
|
|
626
|
-
class TemplateInstrumentationTest(
|
|
655
|
+
class TemplateInstrumentationTest(BaseTestCase):
|
|
627
656
|
saved_render_method: Callable # Assigned during setup.
|
|
628
657
|
|
|
629
658
|
@classmethod
|
|
@@ -685,7 +714,7 @@ class TemplateInstrumentationTest(SimpleTestCase):
|
|
|
685
714
|
self.assertIn("simple_template.html", templates_used)
|
|
686
715
|
|
|
687
716
|
|
|
688
|
-
class NestedSlotTests(
|
|
717
|
+
class NestedSlotTests(BaseTestCase):
|
|
689
718
|
class NestedComponent(component.Component):
|
|
690
719
|
template_name = "nested_slot_template.html"
|
|
691
720
|
|
|
@@ -744,7 +773,7 @@ class NestedSlotTests(SimpleTestCase):
|
|
|
744
773
|
self.assertHTMLEqual(rendered, "<p>Override</p>")
|
|
745
774
|
|
|
746
775
|
|
|
747
|
-
class ConditionalSlotTests(
|
|
776
|
+
class ConditionalSlotTests(BaseTestCase):
|
|
748
777
|
class ConditionalComponent(component.Component):
|
|
749
778
|
template_name = "conditional_template.html"
|
|
750
779
|
|
|
@@ -819,7 +848,7 @@ class ConditionalSlotTests(SimpleTestCase):
|
|
|
819
848
|
self.assertHTMLEqual(rendered, '<p id="a">Override A</p><p id="b">Override B</p>')
|
|
820
849
|
|
|
821
850
|
|
|
822
|
-
class SlotSuperTests(
|
|
851
|
+
class SlotSuperTests(BaseTestCase):
|
|
823
852
|
@classmethod
|
|
824
853
|
def setUpClass(cls):
|
|
825
854
|
super().setUpClass()
|
|
@@ -906,7 +935,7 @@ class SlotSuperTests(SimpleTestCase):
|
|
|
906
935
|
)
|
|
907
936
|
|
|
908
937
|
|
|
909
|
-
class TemplateSyntaxErrorTests(
|
|
938
|
+
class TemplateSyntaxErrorTests(BaseTestCase):
|
|
910
939
|
@classmethod
|
|
911
940
|
def setUpClass(cls):
|
|
912
941
|
super().setUpClass()
|
|
@@ -1013,12 +1042,14 @@ class TemplateSyntaxErrorTests(SimpleTestCase):
|
|
|
1013
1042
|
).render(Context({}))
|
|
1014
1043
|
|
|
1015
1044
|
|
|
1016
|
-
class ComponentNestingTests(
|
|
1045
|
+
class ComponentNestingTests(BaseTestCase):
|
|
1017
1046
|
@classmethod
|
|
1018
1047
|
def setUpClass(cls) -> None:
|
|
1019
1048
|
super().setUpClass()
|
|
1020
1049
|
component.registry.register("dashboard", _DashboardComponent)
|
|
1021
1050
|
component.registry.register("calendar", _CalendarComponent)
|
|
1051
|
+
component.registry.register("complex_child", _ComplexChildComponent)
|
|
1052
|
+
component.registry.register("complex_parent", _ComplexParentComponent)
|
|
1022
1053
|
|
|
1023
1054
|
@classmethod
|
|
1024
1055
|
def tearDownClass(cls) -> None:
|
|
@@ -1052,6 +1083,100 @@ class ComponentNestingTests(SimpleTestCase):
|
|
|
1052
1083
|
"""
|
|
1053
1084
|
self.assertHTMLEqual(rendered, expected)
|
|
1054
1085
|
|
|
1086
|
+
@override_settings(
|
|
1087
|
+
COMPONENTS={
|
|
1088
|
+
"context_behavior": "isolated",
|
|
1089
|
+
"slot_context_behavior": "isolated",
|
|
1090
|
+
}
|
|
1091
|
+
)
|
|
1092
|
+
def test_component_nesting_slot_inside_component_fill_isolated(self):
|
|
1093
|
+
template = Template(
|
|
1094
|
+
"""
|
|
1095
|
+
{% load component_tags %}
|
|
1096
|
+
{% component "dashboard" %}{% endcomponent %}
|
|
1097
|
+
"""
|
|
1098
|
+
)
|
|
1099
|
+
rendered = template.render(Context({"items": [1, 2, 3]}))
|
|
1100
|
+
expected = """
|
|
1101
|
+
<div class="dashboard-component">
|
|
1102
|
+
<div class="calendar-component">
|
|
1103
|
+
<h1>
|
|
1104
|
+
Welcome to your dashboard!
|
|
1105
|
+
</h1>
|
|
1106
|
+
<main>
|
|
1107
|
+
Here are your to-do items for today:
|
|
1108
|
+
</main>
|
|
1109
|
+
</div>
|
|
1110
|
+
<ol>
|
|
1111
|
+
</ol>
|
|
1112
|
+
</div>
|
|
1113
|
+
"""
|
|
1114
|
+
self.assertHTMLEqual(rendered, expected)
|
|
1115
|
+
|
|
1116
|
+
@override_settings(
|
|
1117
|
+
COMPONENTS={
|
|
1118
|
+
"context_behavior": "isolated",
|
|
1119
|
+
"slot_context_behavior": "isolated",
|
|
1120
|
+
}
|
|
1121
|
+
)
|
|
1122
|
+
def test_component_nesting_slot_inside_component_fill_isolated_2(self):
|
|
1123
|
+
template = Template(
|
|
1124
|
+
"""
|
|
1125
|
+
{% load component_tags %}
|
|
1126
|
+
{% component "dashboard" %}
|
|
1127
|
+
{% fill "header" %}
|
|
1128
|
+
Whoa!
|
|
1129
|
+
{% endfill %}
|
|
1130
|
+
{% endcomponent %}
|
|
1131
|
+
"""
|
|
1132
|
+
)
|
|
1133
|
+
rendered = template.render(Context({"items": [1, 2, 3]}))
|
|
1134
|
+
expected = """
|
|
1135
|
+
<div class="dashboard-component">
|
|
1136
|
+
<div class="calendar-component">
|
|
1137
|
+
<h1>
|
|
1138
|
+
Whoa!
|
|
1139
|
+
</h1>
|
|
1140
|
+
<main>
|
|
1141
|
+
Here are your to-do items for today:
|
|
1142
|
+
</main>
|
|
1143
|
+
</div>
|
|
1144
|
+
<ol>
|
|
1145
|
+
</ol>
|
|
1146
|
+
</div>
|
|
1147
|
+
"""
|
|
1148
|
+
self.assertHTMLEqual(rendered, expected)
|
|
1149
|
+
|
|
1150
|
+
@override_settings(
|
|
1151
|
+
COMPONENTS={
|
|
1152
|
+
"context_behavior": "isolated",
|
|
1153
|
+
"slot_context_behavior": "isolated",
|
|
1154
|
+
}
|
|
1155
|
+
)
|
|
1156
|
+
def test_component_nesting_deep_slot_inside_component_fill_isolated(self):
|
|
1157
|
+
|
|
1158
|
+
template = Template(
|
|
1159
|
+
"""
|
|
1160
|
+
{% load component_tags %}
|
|
1161
|
+
{% component "complex_parent" items=items %}{% endcomponent %}
|
|
1162
|
+
"""
|
|
1163
|
+
)
|
|
1164
|
+
items = [{"value": 1}, {"value": 2}, {"value": 3}]
|
|
1165
|
+
rendered = template.render(Context({"items": items}))
|
|
1166
|
+
expected = """
|
|
1167
|
+
ITEMS: [{'value': 1}, {'value': 2}, {'value': 3}]
|
|
1168
|
+
<li>
|
|
1169
|
+
<div> 1 </div>
|
|
1170
|
+
</li>
|
|
1171
|
+
<li>
|
|
1172
|
+
<div> 2 </div>
|
|
1173
|
+
</li>
|
|
1174
|
+
<li>
|
|
1175
|
+
<div> 3 </div>
|
|
1176
|
+
</li>
|
|
1177
|
+
"""
|
|
1178
|
+
self.assertHTMLEqual(rendered, expected)
|
|
1179
|
+
|
|
1055
1180
|
def test_component_nesting_component_with_fill_and_super(self):
|
|
1056
1181
|
template = Template(
|
|
1057
1182
|
"""
|
|
@@ -1081,7 +1206,7 @@ class ComponentNestingTests(SimpleTestCase):
|
|
|
1081
1206
|
self.assertHTMLEqual(rendered, expected)
|
|
1082
1207
|
|
|
1083
1208
|
|
|
1084
|
-
class ConditionalIfFilledSlotsTests(
|
|
1209
|
+
class ConditionalIfFilledSlotsTests(BaseTestCase):
|
|
1085
1210
|
class ComponentWithConditionalSlots(component.Component):
|
|
1086
1211
|
template_name = "template_with_conditional_slots.html"
|
|
1087
1212
|
|
|
@@ -1197,7 +1322,7 @@ class ConditionalIfFilledSlotsTests(SimpleTestCase):
|
|
|
1197
1322
|
self.assertHTMLEqual(rendered, expected)
|
|
1198
1323
|
|
|
1199
1324
|
|
|
1200
|
-
class RegressionTests(
|
|
1325
|
+
class RegressionTests(BaseTestCase):
|
|
1201
1326
|
"""Ensure we don't break the same thing AGAIN."""
|
|
1202
1327
|
|
|
1203
1328
|
def setUp(self):
|
|
@@ -1244,7 +1369,7 @@ class RegressionTests(SimpleTestCase):
|
|
|
1244
1369
|
self.assertHTMLEqual(rendered, expected)
|
|
1245
1370
|
|
|
1246
1371
|
|
|
1247
|
-
class IterationFillTest(
|
|
1372
|
+
class IterationFillTest(BaseTestCase):
|
|
1248
1373
|
"""Tests a behaviour of {% fill .. %} tag which is inside a template {% for .. %} loop."""
|
|
1249
1374
|
|
|
1250
1375
|
class ComponentSimpleSlotInALoop(django_components.component.Component):
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
from typing import List
|
|
1
2
|
from unittest.mock import Mock
|
|
2
3
|
|
|
3
|
-
from django.template import Context
|
|
4
|
+
from django.template import Context, Node
|
|
4
5
|
from django.template.response import TemplateResponse
|
|
5
|
-
from django.test import SimpleTestCase
|
|
6
|
+
from django.test import SimpleTestCase
|
|
6
7
|
|
|
7
8
|
from django_components.middleware import ComponentDependencyMiddleware
|
|
8
9
|
|
|
@@ -11,21 +12,8 @@ response_stash = None
|
|
|
11
12
|
middleware = ComponentDependencyMiddleware(get_response=lambda _: response_stash)
|
|
12
13
|
|
|
13
14
|
|
|
14
|
-
class
|
|
15
|
-
|
|
16
|
-
left = left.replace(' type="text/javascript"', "")
|
|
17
|
-
left = left.replace(' type="text/css"', "")
|
|
18
|
-
right = right.replace(' type="text/javascript"', "")
|
|
19
|
-
right = right.replace(' type="text/css"', "")
|
|
20
|
-
super(Django30CompatibleSimpleTestCase, self).assertHTMLEqual(left, right)
|
|
21
|
-
|
|
22
|
-
def assertInHTML(self, needle, haystack, count=None, msg_prefix=""):
|
|
23
|
-
haystack = haystack.replace(' type="text/javascript"', "")
|
|
24
|
-
haystack = haystack.replace(' type="text/css"', "")
|
|
25
|
-
super().assertInHTML(needle, haystack, count, msg_prefix)
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
class Django30CompatibleTestCase(Django30CompatibleSimpleTestCase, TestCase):
|
|
15
|
+
# TODO: Use this class to manage component registry cleanup before/after tests.
|
|
16
|
+
class BaseTestCase(SimpleTestCase):
|
|
29
17
|
pass
|
|
30
18
|
|
|
31
19
|
|
|
@@ -45,3 +33,22 @@ def create_and_process_template_response(template, context=None, use_middleware=
|
|
|
45
33
|
else:
|
|
46
34
|
response.render()
|
|
47
35
|
return response.content.decode("utf-8")
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def print_nodes(nodes: List[Node], indent=0) -> None:
|
|
39
|
+
"""
|
|
40
|
+
Render a Nodelist, inlining child nodes with extra on separate lines and with
|
|
41
|
+
extra indentation.
|
|
42
|
+
"""
|
|
43
|
+
for node in nodes:
|
|
44
|
+
child_nodes: List[Node] = []
|
|
45
|
+
for attr in node.child_nodelists:
|
|
46
|
+
attr_child_nodes = getattr(node, attr, None) or []
|
|
47
|
+
if attr_child_nodes:
|
|
48
|
+
child_nodes.extend(attr_child_nodes)
|
|
49
|
+
|
|
50
|
+
repr = str(node)
|
|
51
|
+
repr = "\n".join([(" " * 4 * indent) + line for line in repr.split("\n")])
|
|
52
|
+
print(repr)
|
|
53
|
+
if child_nodes:
|
|
54
|
+
print_nodes(child_nodes, indent=indent + 1)
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"""
|
|
2
|
-
This file centralizes various ways we use Django's Context class
|
|
3
|
-
pass data across components, nodes, slots, and contexts.
|
|
4
|
-
|
|
5
|
-
You can think of the Context as our storage system.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from copy import copy
|
|
9
|
-
from typing import TYPE_CHECKING, Optional
|
|
10
|
-
|
|
11
|
-
from django.template import Context
|
|
12
|
-
|
|
13
|
-
from django_components.logger import trace_msg
|
|
14
|
-
|
|
15
|
-
if TYPE_CHECKING:
|
|
16
|
-
from django_components.slots import FillContent
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
_FILLED_SLOTS_CONTENT_CONTEXT_KEY = "_DJANGO_COMPONENTS_FILLED_SLOTS"
|
|
20
|
-
_OUTER_CONTEXT_CONTEXT_KEY = "_DJANGO_COMPONENTS_OUTER_CONTEXT"
|
|
21
|
-
_SLOT_COMPONENT_ASSOC_KEY = "_DJANGO_COMPONENTS_SLOT_COMP_ASSOC"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
def get_slot_fill(context: Context, component_id: str, slot_name: str) -> Optional["FillContent"]:
|
|
25
|
-
"""
|
|
26
|
-
Use this function to obtain a slot fill from the current context.
|
|
27
|
-
|
|
28
|
-
See `set_slot_fill` for more details.
|
|
29
|
-
"""
|
|
30
|
-
trace_msg("GET", "FILL", slot_name, component_id)
|
|
31
|
-
slot_key = (_FILLED_SLOTS_CONTENT_CONTEXT_KEY, component_id, slot_name)
|
|
32
|
-
return context.get(slot_key, None)
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def set_slot_fill(context: Context, component_id: str, slot_name: str, value: "FillContent") -> None:
|
|
36
|
-
"""
|
|
37
|
-
Use this function to set a slot fill for the current context.
|
|
38
|
-
|
|
39
|
-
Note that we make use of the fact that Django's Context is a stack - we can push and pop
|
|
40
|
-
extra contexts on top others.
|
|
41
|
-
|
|
42
|
-
For the slot fills to be pushed/popped wth stack layer, they need to have keys defined
|
|
43
|
-
directly on the Context object.
|
|
44
|
-
"""
|
|
45
|
-
trace_msg("SET", "FILL", slot_name, component_id)
|
|
46
|
-
slot_key = (_FILLED_SLOTS_CONTENT_CONTEXT_KEY, component_id, slot_name)
|
|
47
|
-
context[slot_key] = value
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
def get_root_context(context: Context) -> Optional[Context]:
|
|
51
|
-
"""
|
|
52
|
-
Use this function to get the root context.
|
|
53
|
-
|
|
54
|
-
Root context is the top-most context, AKA the context that was passed to
|
|
55
|
-
the initial `Template.render()`.
|
|
56
|
-
We pass through the root context to allow configure how slot fills should be rendered.
|
|
57
|
-
|
|
58
|
-
See the `SLOT_CONTEXT_BEHAVIOR` setting.
|
|
59
|
-
"""
|
|
60
|
-
return context.get(_OUTER_CONTEXT_CONTEXT_KEY)
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
def set_root_context(context: Context, root_ctx: Context) -> None:
|
|
64
|
-
"""
|
|
65
|
-
Use this function to set the root context.
|
|
66
|
-
|
|
67
|
-
Root context is the top-most context, AKA the context that was passed to
|
|
68
|
-
the initial `Template.render()`.
|
|
69
|
-
We pass through the root context to allow configure how slot fills should be rendered.
|
|
70
|
-
|
|
71
|
-
See the `SLOT_CONTEXT_BEHAVIOR` setting.
|
|
72
|
-
"""
|
|
73
|
-
context.push({_OUTER_CONTEXT_CONTEXT_KEY: root_ctx})
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
def capture_root_context(context: Context) -> None:
|
|
77
|
-
"""
|
|
78
|
-
Set the root context if it was not set before.
|
|
79
|
-
|
|
80
|
-
Root context is the top-most context, AKA the context that was passed to
|
|
81
|
-
the initial `Template.render()`.
|
|
82
|
-
We pass through the root context to allow configure how slot fills should be rendered.
|
|
83
|
-
|
|
84
|
-
See the `SLOT_CONTEXT_BEHAVIOR` setting.
|
|
85
|
-
"""
|
|
86
|
-
root_ctx_already_defined = _OUTER_CONTEXT_CONTEXT_KEY in context
|
|
87
|
-
if not root_ctx_already_defined:
|
|
88
|
-
set_root_context(context, copy(context))
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
def set_slot_component_association(context: Context, slot_id: str, component_id: str) -> None:
|
|
92
|
-
"""
|
|
93
|
-
Set association between a Slot and a Component in the current context.
|
|
94
|
-
|
|
95
|
-
We use SlotNodes to render slot fills. SlotNodes are created only at Template parse time.
|
|
96
|
-
However, when we are using components with slots in (another) template, we can render
|
|
97
|
-
the same component multiple time. So we can have multiple FillNodes intended to be used
|
|
98
|
-
with the same SlotNode.
|
|
99
|
-
|
|
100
|
-
So how do we tell the SlotNode which FillNode to render? We do so by tagging the ComponentNode
|
|
101
|
-
and FillNodes with a unique component_id, which ties them together. And then we tell SlotNode
|
|
102
|
-
which component_id to use to be able to find the correct Component/Fill.
|
|
103
|
-
|
|
104
|
-
We don't want to store this info on the Nodes themselves, as we need to treat them as
|
|
105
|
-
immutable due to caching of Templates by Django.
|
|
106
|
-
|
|
107
|
-
Hence, we use the Context to store the associations of SlotNode <-> Component for
|
|
108
|
-
the current context stack.
|
|
109
|
-
"""
|
|
110
|
-
key = (_SLOT_COMPONENT_ASSOC_KEY, slot_id)
|
|
111
|
-
context[key] = component_id
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def get_slot_component_association(context: Context, slot_id: str) -> str:
|
|
115
|
-
"""
|
|
116
|
-
Given a slot ID, get the component ID that this slot is associated with
|
|
117
|
-
in this context.
|
|
118
|
-
|
|
119
|
-
See `set_slot_component_association` for more details.
|
|
120
|
-
"""
|
|
121
|
-
key = (_SLOT_COMPONENT_ASSOC_KEY, slot_id)
|
|
122
|
-
return context[key]
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
Django>=3.2
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.67 → django_components-0.68}/src/django_components/component_registry.py
RENAMED
|
File without changes
|
|
File without changes
|
{django_components-0.67 → django_components-0.68}/src/django_components/management/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.67 → django_components-0.68}/src/django_components/safer_staticfiles/apps.py
RENAMED
|
File without changes
|
|
File without changes
|
{django_components-0.67 → django_components-0.68}/src/django_components/templatetags/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.67 → django_components-0.68}/src/django_components.egg-info/SOURCES.txt
RENAMED
|
File without changes
|
|
File without changes
|
{django_components-0.67 → django_components-0.68}/src/django_components.egg-info/top_level.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|