django-components 0.66__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.66 → django_components-0.68}/PKG-INFO +31 -11
- django_components-0.66/src/django_components.egg-info/PKG-INFO → django_components-0.68/README.md +29 -33
- {django_components-0.66 → django_components-0.68}/pyproject.toml +2 -5
- django_components-0.68/src/django_components/app_settings.py +146 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/component.py +58 -32
- django_components-0.68/src/django_components/context.py +182 -0
- django_components-0.68/src/django_components/logger.py +93 -0
- django_components-0.68/src/django_components/node.py +38 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/slots.py +187 -154
- {django_components-0.66 → django_components-0.68}/src/django_components/templatetags/component_tags.py +39 -4
- {django_components-0.66 → django_components-0.68}/src/django_components/utils.py +22 -1
- django_components-0.66/README.md → django_components-0.68/src/django_components.egg-info/PKG-INFO +53 -6
- {django_components-0.66 → django_components-0.68}/src/django_components.egg-info/SOURCES.txt +2 -0
- django_components-0.68/src/django_components.egg-info/requires.txt +1 -0
- {django_components-0.66 → django_components-0.68}/tests/test_autodiscover.py +5 -5
- {django_components-0.66 → django_components-0.68}/tests/test_component.py +210 -5
- {django_components-0.66 → django_components-0.68}/tests/test_component_as_view.py +2 -2
- {django_components-0.66 → django_components-0.68}/tests/test_context.py +25 -10
- {django_components-0.66 → django_components-0.68}/tests/test_dependency_rendering.py +2 -3
- {django_components-0.66 → django_components-0.68}/tests/test_settings.py +3 -3
- {django_components-0.66 → django_components-0.68}/tests/test_templatetags.py +164 -39
- {django_components-0.66 → django_components-0.68}/tests/testutils.py +24 -17
- django_components-0.66/src/django_components/app_settings.py +0 -41
- django_components-0.66/src/django_components/logger.py +0 -3
- django_components-0.66/src/django_components.egg-info/requires.txt +0 -1
- {django_components-0.66 → django_components-0.68}/LICENSE +0 -0
- {django_components-0.66 → django_components-0.68}/setup.cfg +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/__init__.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/apps.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/component_registry.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/management/__init__.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/management/commands/__init__.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/management/commands/startcomponent.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/management/commands/upgradecomponent.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/middleware.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/py.typed +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/safer_staticfiles/apps.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/template_loader.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/templatetags/__init__.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components/types.py +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.66 → django_components-0.68}/src/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.66 → django_components-0.68}/tests/test_registry.py +0 -0
- {django_components-0.66 → 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>
|
|
@@ -47,6 +44,8 @@ Read on to learn about the details!
|
|
|
47
44
|
|
|
48
45
|
## Release notes
|
|
49
46
|
|
|
47
|
+
**Version 0.67** CHANGED the default way how context variables are resolved in slots. See the [documentation](#isolate-components-slots) for more details.
|
|
48
|
+
|
|
50
49
|
🚨📢 **Version 0.5** CHANGES THE SYNTAX for components. `component_block` is now `component`, and `component` blocks need an ending `endcomponent` tag. The new `python manage.py upgradecomponent` command can be used to upgrade a directory (use --path argument to point to each dir) of components to the new syntax automatically.
|
|
51
50
|
|
|
52
51
|
This change is done to simplify the API in anticipation of a 1.0 release of django_components. After 1.0 we intend to be stricter with big changes like this in point releases.
|
|
@@ -143,7 +142,7 @@ STATICFILES_DIRS = [
|
|
|
143
142
|
|
|
144
143
|
### Optional
|
|
145
144
|
|
|
146
|
-
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
|
|
147
146
|
|
|
148
147
|
```python
|
|
149
148
|
TEMPLATES = [
|
|
@@ -169,10 +168,10 @@ Django-components supports all supported combinations versions of [Django](https
|
|
|
169
168
|
|
|
170
169
|
| Python version | Django version |
|
|
171
170
|
|----------------|--------------------------|
|
|
172
|
-
| 3.8 |
|
|
173
|
-
| 3.9 |
|
|
174
|
-
| 3.10 |
|
|
175
|
-
| 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 |
|
|
176
175
|
| 3.12 | 4.2, 5.0 |
|
|
177
176
|
|
|
178
177
|
## Create your first component
|
|
@@ -505,7 +504,7 @@ Then, to use this component as a view, you should create a `urls.py` file in you
|
|
|
505
504
|
```python
|
|
506
505
|
# In a file called [project root]/components/urls.py
|
|
507
506
|
from django.urls import path
|
|
508
|
-
from calendar import Calendar
|
|
507
|
+
from components.calendar.calendar import Calendar
|
|
509
508
|
|
|
510
509
|
urlpatterns = [
|
|
511
510
|
path("calendar/", Calendar.as_view()),
|
|
@@ -731,6 +730,27 @@ COMPONENTS = {
|
|
|
731
730
|
}
|
|
732
731
|
```
|
|
733
732
|
|
|
733
|
+
### Isolate components' slots
|
|
734
|
+
|
|
735
|
+
What variables should be available from inside a component slot?
|
|
736
|
+
|
|
737
|
+
By default, variables inside component slots are preferentially taken from the root context.
|
|
738
|
+
This is similar to [how Vue renders slots](https://vuejs.org/guide/components/slots.html#render-scope),
|
|
739
|
+
except that, if variable is not found in the root, then the surrounding context is searched too.
|
|
740
|
+
|
|
741
|
+
You can change this with the `slot_contet_behavior` setting. Options are:
|
|
742
|
+
- `"prefer_root"` - Default - as described above
|
|
743
|
+
- `"isolated"` - Same behavior as Vue - variables are taken ONLY from the root context
|
|
744
|
+
- `"allow_override"` - slot context variables are taken from its surroundings (default before v0.67)
|
|
745
|
+
|
|
746
|
+
```python
|
|
747
|
+
COMPONENTS = {
|
|
748
|
+
"slot_context_behavior": "isolated",
|
|
749
|
+
}
|
|
750
|
+
```
|
|
751
|
+
|
|
752
|
+
For further details and examples, see [SlotContextBehavior](https://github.com/EmilStenstrom/django-components/blob/master/src/django_components/app_settings.py#L12).
|
|
753
|
+
|
|
734
754
|
## Logging and debugging
|
|
735
755
|
|
|
736
756
|
Django components supports [logging with Django](https://docs.djangoproject.com/en/5.0/howto/logging/#logging-how-to). This can help with troubleshooting.
|
django_components-0.66/src/django_components.egg-info/PKG-INFO → django_components-0.68/README.md
RENAMED
|
@@ -1,30 +1,3 @@
|
|
|
1
|
-
Metadata-Version: 2.1
|
|
2
|
-
Name: django_components
|
|
3
|
-
Version: 0.66
|
|
4
|
-
Summary: A way to create simple reusable template components in Django.
|
|
5
|
-
Author-email: Emil Stenström <emil@emilstenstrom.se>
|
|
6
|
-
License: MIT
|
|
7
|
-
Project-URL: Homepage, https://github.com/EmilStenstrom/django-components/
|
|
8
|
-
Keywords: django,components,css,js,html
|
|
9
|
-
Classifier: Framework :: Django
|
|
10
|
-
Classifier: Framework :: Django :: 3.2
|
|
11
|
-
Classifier: Framework :: Django :: 4.0
|
|
12
|
-
Classifier: Framework :: Django :: 4.1
|
|
13
|
-
Classifier: Framework :: Django :: 4.2
|
|
14
|
-
Classifier: Framework :: Django :: 5.0
|
|
15
|
-
Classifier: Operating System :: OS Independent
|
|
16
|
-
Classifier: Programming Language :: Python
|
|
17
|
-
Classifier: Programming Language :: Python :: 3
|
|
18
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
-
Requires-Python: <4.0,>=3.8
|
|
24
|
-
Description-Content-Type: text/markdown
|
|
25
|
-
License-File: LICENSE
|
|
26
|
-
Requires-Dist: Django>=3.2
|
|
27
|
-
|
|
28
1
|
# <img src="logo/logo-black-on-white.svg" alt="django-components" style="max-width: 100%; background: white; color: black;">
|
|
29
2
|
<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>
|
|
30
3
|
<a href="https://pepy.tech/project/django-components"><img align="right" src="https://pepy.tech/badge/django-components" alt="Show download stats"></a>
|
|
@@ -47,6 +20,8 @@ Read on to learn about the details!
|
|
|
47
20
|
|
|
48
21
|
## Release notes
|
|
49
22
|
|
|
23
|
+
**Version 0.67** CHANGED the default way how context variables are resolved in slots. See the [documentation](#isolate-components-slots) for more details.
|
|
24
|
+
|
|
50
25
|
🚨📢 **Version 0.5** CHANGES THE SYNTAX for components. `component_block` is now `component`, and `component` blocks need an ending `endcomponent` tag. The new `python manage.py upgradecomponent` command can be used to upgrade a directory (use --path argument to point to each dir) of components to the new syntax automatically.
|
|
51
26
|
|
|
52
27
|
This change is done to simplify the API in anticipation of a 1.0 release of django_components. After 1.0 we intend to be stricter with big changes like this in point releases.
|
|
@@ -143,7 +118,7 @@ STATICFILES_DIRS = [
|
|
|
143
118
|
|
|
144
119
|
### Optional
|
|
145
120
|
|
|
146
|
-
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
|
|
147
122
|
|
|
148
123
|
```python
|
|
149
124
|
TEMPLATES = [
|
|
@@ -169,10 +144,10 @@ Django-components supports all supported combinations versions of [Django](https
|
|
|
169
144
|
|
|
170
145
|
| Python version | Django version |
|
|
171
146
|
|----------------|--------------------------|
|
|
172
|
-
| 3.8 |
|
|
173
|
-
| 3.9 |
|
|
174
|
-
| 3.10 |
|
|
175
|
-
| 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 |
|
|
176
151
|
| 3.12 | 4.2, 5.0 |
|
|
177
152
|
|
|
178
153
|
## Create your first component
|
|
@@ -505,7 +480,7 @@ Then, to use this component as a view, you should create a `urls.py` file in you
|
|
|
505
480
|
```python
|
|
506
481
|
# In a file called [project root]/components/urls.py
|
|
507
482
|
from django.urls import path
|
|
508
|
-
from calendar import Calendar
|
|
483
|
+
from components.calendar.calendar import Calendar
|
|
509
484
|
|
|
510
485
|
urlpatterns = [
|
|
511
486
|
path("calendar/", Calendar.as_view()),
|
|
@@ -731,6 +706,27 @@ COMPONENTS = {
|
|
|
731
706
|
}
|
|
732
707
|
```
|
|
733
708
|
|
|
709
|
+
### Isolate components' slots
|
|
710
|
+
|
|
711
|
+
What variables should be available from inside a component slot?
|
|
712
|
+
|
|
713
|
+
By default, variables inside component slots are preferentially taken from the root context.
|
|
714
|
+
This is similar to [how Vue renders slots](https://vuejs.org/guide/components/slots.html#render-scope),
|
|
715
|
+
except that, if variable is not found in the root, then the surrounding context is searched too.
|
|
716
|
+
|
|
717
|
+
You can change this with the `slot_contet_behavior` setting. Options are:
|
|
718
|
+
- `"prefer_root"` - Default - as described above
|
|
719
|
+
- `"isolated"` - Same behavior as Vue - variables are taken ONLY from the root context
|
|
720
|
+
- `"allow_override"` - slot context variables are taken from its surroundings (default before v0.67)
|
|
721
|
+
|
|
722
|
+
```python
|
|
723
|
+
COMPONENTS = {
|
|
724
|
+
"slot_context_behavior": "isolated",
|
|
725
|
+
}
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
For further details and examples, see [SlotContextBehavior](https://github.com/EmilStenstrom/django-components/blob/master/src/django_components/app_settings.py#L12).
|
|
729
|
+
|
|
734
730
|
## Logging and debugging
|
|
735
731
|
|
|
736
732
|
Django components supports [logging with Django](https://docs.djangoproject.com/en/5.0/howto/logging/#logging-how-to). This can help with troubleshooting.
|
|
@@ -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
|
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
from enum import Enum
|
|
2
|
+
from typing import Dict, List
|
|
3
|
+
|
|
4
|
+
from django.conf import settings
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class ContextBehavior(str, Enum):
|
|
8
|
+
GLOBAL = "global"
|
|
9
|
+
ISOLATED = "isolated"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class SlotContextBehavior(str, Enum):
|
|
13
|
+
ALLOW_OVERRIDE = "allow_override"
|
|
14
|
+
"""
|
|
15
|
+
Components CAN override the slot context variables passed from the outer scopes.
|
|
16
|
+
Contexts of deeper components take precedence over shallower ones.
|
|
17
|
+
|
|
18
|
+
Example:
|
|
19
|
+
|
|
20
|
+
Given this template
|
|
21
|
+
|
|
22
|
+
```txt
|
|
23
|
+
{% component 'my_comp' %}
|
|
24
|
+
{{ my_var }}
|
|
25
|
+
{% endcomponent %}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
and this context passed to the render function (AKA root context)
|
|
29
|
+
```py
|
|
30
|
+
{ "my_var": 123 }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Then if component "my_comp" defines context
|
|
34
|
+
```py
|
|
35
|
+
{ "my_var": 456 }
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Then since "my_comp" overrides the varialbe "my_var", so `{{ my_var }}` will equal `456`.
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
PREFER_ROOT = "prefer_root"
|
|
42
|
+
"""
|
|
43
|
+
This is the same as "allow_override", except any variables defined in the root context
|
|
44
|
+
take precedence over anything else.
|
|
45
|
+
|
|
46
|
+
So if a variable is found in the root context, then root context is used.
|
|
47
|
+
Otherwise, the context of the component where the slot fill is located is used.
|
|
48
|
+
|
|
49
|
+
Example:
|
|
50
|
+
|
|
51
|
+
Given this template
|
|
52
|
+
|
|
53
|
+
```txt
|
|
54
|
+
{% component 'my_comp' %}
|
|
55
|
+
{{ my_var_one }}
|
|
56
|
+
{{ my_var_two }}
|
|
57
|
+
{% endcomponent %}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
and this context passed to the render function (AKA root context)
|
|
61
|
+
```py
|
|
62
|
+
{ "my_var_one": 123 }
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Then if component "my_comp" defines context
|
|
66
|
+
```py
|
|
67
|
+
{ "my_var": 456, "my_var_two": "abc" }
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Then the rendered `{{ my_var_one }}` will equal to `123`, and `{{ my_var_two }}`
|
|
71
|
+
will equal to "abc".
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
ISOLATED = "isolated"
|
|
75
|
+
"""
|
|
76
|
+
This setting makes the slots behave similar to Vue or React, where
|
|
77
|
+
the slot uses EXCLUSIVELY the root context, and nested components CANNOT
|
|
78
|
+
override context variables inside the slots.
|
|
79
|
+
|
|
80
|
+
Example:
|
|
81
|
+
|
|
82
|
+
Given this template
|
|
83
|
+
|
|
84
|
+
```txt
|
|
85
|
+
{% component 'my_comp' %}
|
|
86
|
+
{{ my_var }}
|
|
87
|
+
{% endcomponent %}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
and this context passed to the render function (AKA root context)
|
|
91
|
+
```py
|
|
92
|
+
{ "my_var": 123 }
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Then if component "my_comp" defines context
|
|
96
|
+
```py
|
|
97
|
+
{ "my_var": 456 }
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Then the rendered `{{ my_var }}` will equal `123`.
|
|
101
|
+
"""
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class AppSettings:
|
|
105
|
+
@property
|
|
106
|
+
def settings(self) -> Dict:
|
|
107
|
+
return getattr(settings, "COMPONENTS", {})
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def AUTODISCOVER(self) -> bool:
|
|
111
|
+
return self.settings.get("autodiscover", True)
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def LIBRARIES(self) -> List:
|
|
115
|
+
return self.settings.get("libraries", [])
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def TEMPLATE_CACHE_SIZE(self) -> int:
|
|
119
|
+
return self.settings.get("template_cache_size", 128)
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def CONTEXT_BEHAVIOR(self) -> ContextBehavior:
|
|
123
|
+
raw_value = self.settings.get("context_behavior", ContextBehavior.GLOBAL.value)
|
|
124
|
+
return self._validate_context_behavior(raw_value)
|
|
125
|
+
|
|
126
|
+
def _validate_context_behavior(self, raw_value: ContextBehavior) -> ContextBehavior:
|
|
127
|
+
try:
|
|
128
|
+
return ContextBehavior(raw_value)
|
|
129
|
+
except ValueError:
|
|
130
|
+
valid_values = [behavior.value for behavior in ContextBehavior]
|
|
131
|
+
raise ValueError(f"Invalid context behavior: {raw_value}. Valid options are {valid_values}")
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def SLOT_CONTEXT_BEHAVIOR(self) -> SlotContextBehavior:
|
|
135
|
+
raw_value = self.settings.get("slot_context_behavior", SlotContextBehavior.PREFER_ROOT.value)
|
|
136
|
+
return self._validate_slot_context_behavior(raw_value)
|
|
137
|
+
|
|
138
|
+
def _validate_slot_context_behavior(self, raw_value: SlotContextBehavior) -> SlotContextBehavior:
|
|
139
|
+
try:
|
|
140
|
+
return SlotContextBehavior(raw_value)
|
|
141
|
+
except ValueError:
|
|
142
|
+
valid_values = [behavior.value for behavior in SlotContextBehavior]
|
|
143
|
+
raise ValueError(f"Invalid slot context behavior: {raw_value}. Valid options are {valid_values}")
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
app_settings = AppSettings()
|
|
@@ -2,7 +2,7 @@ import inspect
|
|
|
2
2
|
import os
|
|
3
3
|
import sys
|
|
4
4
|
from pathlib import Path
|
|
5
|
-
from typing import Any, ClassVar, Dict,
|
|
5
|
+
from typing import Any, ClassVar, Dict, List, Mapping, MutableMapping, Optional, Tuple, Type, Union
|
|
6
6
|
|
|
7
7
|
from django.core.exceptions import ImproperlyConfigured
|
|
8
8
|
from django.forms.widgets import Media, MediaDefiningClass
|
|
@@ -20,17 +20,19 @@ 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.
|
|
23
|
+
from django_components.context import make_isolated_context_copy, prepare_context, set_slot_component_association
|
|
24
|
+
from django_components.logger import logger, trace_msg
|
|
24
25
|
from django_components.middleware import is_dependency_middleware_active
|
|
26
|
+
from django_components.node import walk_nodelist
|
|
25
27
|
from django_components.slots import (
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
NamedFillNode,
|
|
28
|
+
DEFAULT_SLOT_KEY,
|
|
29
|
+
FillContent,
|
|
30
|
+
FillNode,
|
|
30
31
|
SlotName,
|
|
32
|
+
SlotNode,
|
|
31
33
|
render_component_template_with_slots,
|
|
32
34
|
)
|
|
33
|
-
from django_components.utils import search
|
|
35
|
+
from django_components.utils import gen_id, search
|
|
34
36
|
|
|
35
37
|
RENDERED_COMMENT_TEMPLATE = "<!-- _RENDERED {name} -->"
|
|
36
38
|
|
|
@@ -185,12 +187,14 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
185
187
|
def __init__(
|
|
186
188
|
self,
|
|
187
189
|
registered_name: Optional[str] = None,
|
|
190
|
+
component_id: Optional[str] = None,
|
|
188
191
|
outer_context: Optional[Context] = None,
|
|
189
|
-
fill_content:
|
|
192
|
+
fill_content: Dict[str, FillContent] = {},
|
|
190
193
|
):
|
|
191
194
|
self.registered_name: Optional[str] = registered_name
|
|
192
195
|
self.outer_context: Context = outer_context or Context()
|
|
193
196
|
self.fill_content = fill_content
|
|
197
|
+
self.component_id = component_id or gen_id()
|
|
194
198
|
|
|
195
199
|
def __init_subclass__(cls, **kwargs: Any) -> None:
|
|
196
200
|
cls.class_hash = hash(inspect.getfile(cls) + cls.__name__)
|
|
@@ -230,6 +234,12 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
230
234
|
return mark_safe(f"<script>{self.js}</script>")
|
|
231
235
|
return mark_safe("\n".join(self.media.render_js()))
|
|
232
236
|
|
|
237
|
+
# NOTE: When the template is taken from a file (AKA
|
|
238
|
+
# specified via `template_name`), then we leverage
|
|
239
|
+
# Django's template caching. This means that the same
|
|
240
|
+
# instance of Template is reused. This is important to keep
|
|
241
|
+
# in mind, because the implication is that we should
|
|
242
|
+
# treat Templates AND their nodelists as IMMUTABLE.
|
|
233
243
|
def get_template(self, context: Mapping) -> Template:
|
|
234
244
|
template_string = self.get_template_string(context)
|
|
235
245
|
if template_string is not None:
|
|
@@ -246,23 +256,35 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
246
256
|
|
|
247
257
|
def render(
|
|
248
258
|
self,
|
|
249
|
-
context_data: Dict[str, Any],
|
|
259
|
+
context_data: Union[Dict[str, Any], Context],
|
|
250
260
|
slots_data: Optional[Dict[SlotName, str]] = None,
|
|
251
261
|
escape_slots_content: bool = True,
|
|
252
262
|
) -> str:
|
|
253
263
|
# NOTE: This if/else is important to avoid nested Contexts,
|
|
254
264
|
# See https://github.com/EmilStenstrom/django-components/issues/414
|
|
255
265
|
context = context_data if isinstance(context_data, Context) else Context(context_data)
|
|
266
|
+
prepare_context(context, outer_context=self.outer_context or Context())
|
|
256
267
|
template = self.get_template(context)
|
|
257
268
|
|
|
269
|
+
# Associate the slots with this component for this context
|
|
270
|
+
# This allows us to look up component-specific slot fills.
|
|
271
|
+
def on_node(node: Node) -> None:
|
|
272
|
+
if isinstance(node, SlotNode):
|
|
273
|
+
trace_msg("ASSOC", "SLOT", node.name, node.node_id, component_id=self.component_id)
|
|
274
|
+
set_slot_component_association(context, node.node_id, self.component_id)
|
|
275
|
+
|
|
276
|
+
walk_nodelist(template.nodelist, on_node)
|
|
277
|
+
|
|
258
278
|
if slots_data:
|
|
259
279
|
self._fill_slots(slots_data, escape_slots_content)
|
|
260
280
|
|
|
261
|
-
return render_component_template_with_slots(
|
|
281
|
+
return render_component_template_with_slots(
|
|
282
|
+
self.component_id, template, context, self.fill_content, self.registered_name
|
|
283
|
+
)
|
|
262
284
|
|
|
263
285
|
def render_to_response(
|
|
264
286
|
self,
|
|
265
|
-
context_data: Dict[str, Any],
|
|
287
|
+
context_data: Union[Dict[str, Any], Context],
|
|
266
288
|
slots_data: Optional[Dict[SlotName, str]] = None,
|
|
267
289
|
escape_slots_content: bool = True,
|
|
268
290
|
*args: Any,
|
|
@@ -280,14 +302,13 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
280
302
|
escape_content: bool = True,
|
|
281
303
|
) -> None:
|
|
282
304
|
"""Fill component slots outside of template rendering."""
|
|
283
|
-
self.fill_content =
|
|
284
|
-
(
|
|
285
|
-
slot_name,
|
|
305
|
+
self.fill_content = {
|
|
306
|
+
slot_name: FillContent(
|
|
286
307
|
TextNode(escape(content) if escape_content else content),
|
|
287
308
|
None,
|
|
288
309
|
)
|
|
289
310
|
for (slot_name, content) in slots_data.items()
|
|
290
|
-
|
|
311
|
+
}
|
|
291
312
|
|
|
292
313
|
|
|
293
314
|
class ComponentNode(Node):
|
|
@@ -299,20 +320,16 @@ class ComponentNode(Node):
|
|
|
299
320
|
context_args: List[FilterExpression],
|
|
300
321
|
context_kwargs: Mapping[str, FilterExpression],
|
|
301
322
|
isolated_context: bool = False,
|
|
302
|
-
fill_nodes:
|
|
323
|
+
fill_nodes: Optional[List[FillNode]] = None,
|
|
324
|
+
component_id: Optional[str] = None,
|
|
303
325
|
) -> None:
|
|
326
|
+
self.component_id = component_id or gen_id()
|
|
304
327
|
self.name_fexp = name_fexp
|
|
305
328
|
self.context_args = context_args or []
|
|
306
329
|
self.context_kwargs = context_kwargs or {}
|
|
307
330
|
self.isolated_context = isolated_context
|
|
308
|
-
self.fill_nodes = fill_nodes
|
|
309
|
-
self.nodelist =
|
|
310
|
-
|
|
311
|
-
def _create_nodelist(self, fill_nodes: Union[Iterable[Node], ImplicitFillNode]) -> NodeList:
|
|
312
|
-
if isinstance(fill_nodes, ImplicitFillNode):
|
|
313
|
-
return NodeList([fill_nodes])
|
|
314
|
-
else:
|
|
315
|
-
return NodeList(fill_nodes)
|
|
331
|
+
self.fill_nodes = fill_nodes or []
|
|
332
|
+
self.nodelist = NodeList(fill_nodes)
|
|
316
333
|
|
|
317
334
|
def __repr__(self) -> str:
|
|
318
335
|
return "<ComponentNode: {}. Contents: {!r}>".format(
|
|
@@ -321,6 +338,8 @@ class ComponentNode(Node):
|
|
|
321
338
|
)
|
|
322
339
|
|
|
323
340
|
def render(self, context: Context) -> str:
|
|
341
|
+
trace_msg("RENDR", "COMP", self.name_fexp, self.component_id)
|
|
342
|
+
|
|
324
343
|
resolved_component_name = self.name_fexp.resolve(context)
|
|
325
344
|
component_cls: Type[Component] = registry.get(resolved_component_name)
|
|
326
345
|
|
|
@@ -330,37 +349,44 @@ class ComponentNode(Node):
|
|
|
330
349
|
resolved_context_args = [safe_resolve(arg, context) for arg in self.context_args]
|
|
331
350
|
resolved_context_kwargs = {key: safe_resolve(kwarg, context) for key, kwarg in self.context_kwargs.items()}
|
|
332
351
|
|
|
333
|
-
|
|
334
|
-
|
|
352
|
+
is_default_slot = len(self.fill_nodes) == 1 and self.fill_nodes[0].is_implicit
|
|
353
|
+
if is_default_slot:
|
|
354
|
+
fill_content: Dict[str, FillContent] = {DEFAULT_SLOT_KEY: FillContent(self.fill_nodes[0].nodelist, None)}
|
|
335
355
|
else:
|
|
336
|
-
fill_content =
|
|
356
|
+
fill_content = {}
|
|
337
357
|
for fill_node in self.fill_nodes:
|
|
338
358
|
# Note that outer component context is used to resolve variables in
|
|
339
359
|
# fill tag.
|
|
340
360
|
resolved_name = fill_node.name_fexp.resolve(context)
|
|
341
361
|
resolved_fill_alias = fill_node.resolve_alias(context, resolved_component_name)
|
|
342
|
-
fill_content
|
|
362
|
+
fill_content[resolved_name] = FillContent(fill_node.nodelist, resolved_fill_alias)
|
|
343
363
|
|
|
344
364
|
component: Component = component_cls(
|
|
345
365
|
registered_name=resolved_component_name,
|
|
346
366
|
outer_context=context,
|
|
347
367
|
fill_content=fill_content,
|
|
368
|
+
component_id=self.component_id,
|
|
348
369
|
)
|
|
349
370
|
|
|
350
371
|
component_context: dict = component.get_context_data(*resolved_context_args, **resolved_context_kwargs)
|
|
351
372
|
|
|
373
|
+
# Prevent outer context from leaking into the template of the component
|
|
352
374
|
if self.isolated_context:
|
|
353
|
-
context = context
|
|
375
|
+
context = make_isolated_context_copy(context)
|
|
376
|
+
|
|
354
377
|
with context.update(component_context):
|
|
355
378
|
rendered_component = component.render(context)
|
|
356
379
|
|
|
357
380
|
if is_dependency_middleware_active():
|
|
358
|
-
|
|
381
|
+
output = RENDERED_COMMENT_TEMPLATE.format(name=resolved_component_name) + rendered_component
|
|
359
382
|
else:
|
|
360
|
-
|
|
383
|
+
output = rendered_component
|
|
384
|
+
|
|
385
|
+
trace_msg("RENDR", "COMP", self.name_fexp, self.component_id, "...Done!")
|
|
386
|
+
return output
|
|
361
387
|
|
|
362
388
|
|
|
363
389
|
def safe_resolve(context_item: FilterExpression, context: Context) -> Any:
|
|
364
|
-
"""Resolve FilterExpressions and Variables in context if possible.
|
|
390
|
+
"""Resolve FilterExpressions and Variables in context if possible. Return other items unchanged."""
|
|
365
391
|
|
|
366
392
|
return context_item.resolve(context) if hasattr(context_item, "resolve") else context_item
|