django-components 0.96__tar.gz → 0.97__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.96/src/django_components.egg-info → django_components-0.97}/PKG-INFO +36 -7
- {django_components-0.96 → django_components-0.97}/README.md +35 -6
- {django_components-0.96 → django_components-0.97}/pyproject.toml +1 -1
- {django_components-0.96 → django_components-0.97}/src/django_components/__init__.py +1 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/component.py +77 -18
- {django_components-0.96 → django_components-0.97}/src/django_components/slots.py +19 -13
- django_components-0.97/src/django_components/template.py +42 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/templatetags/component_tags.py +1 -1
- {django_components-0.96 → django_components-0.97}/src/django_components/utils.py +46 -1
- {django_components-0.96 → django_components-0.97/src/django_components.egg-info}/PKG-INFO +36 -7
- {django_components-0.96 → django_components-0.97}/src/django_components.egg-info/SOURCES.txt +2 -0
- {django_components-0.96 → django_components-0.97}/tests/test_component.py +32 -5
- django_components-0.97/tests/test_template.py +67 -0
- {django_components-0.96 → django_components-0.97}/tests/test_templatetags_slot_fill.py +1 -1
- {django_components-0.96 → django_components-0.97}/tests/testutils.py +4 -0
- {django_components-0.96 → django_components-0.97}/LICENSE +0 -0
- {django_components-0.96 → django_components-0.97}/setup.cfg +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/app_settings.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/apps.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/attributes.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/autodiscover.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/component_media.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/component_registry.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/components/__init__.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/components/dynamic.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/context.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/expression.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/library.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/logger.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/management/__init__.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/management/commands/__init__.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/management/commands/startcomponent.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/management/commands/upgradecomponent.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/middleware.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/node.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/provide.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/py.typed +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/safer_staticfiles/apps.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/tag_formatter.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/template_loader.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/template_parser.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/templatetags/__init__.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components/types.py +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components.egg-info/requires.txt +0 -0
- {django_components-0.96 → django_components-0.97}/src/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_attributes.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_autodiscover.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_command_startcomponent.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_component_as_view.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_component_media.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_context.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_dependency_rendering.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_expression.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_registry.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_safer_staticfiles.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_settings.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_tag_formatter.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_template_loader.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_template_parser.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_templatetags.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_templatetags_component.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_templatetags_provide.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_templatetags_templating.py +0 -0
- {django_components-0.96 → django_components-0.97}/tests/test_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.97
|
|
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
|
|
@@ -56,7 +56,7 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
56
56
|
<div class="calendar-component">Today's date is <span>2015-06-19</span></div>
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
[See the example project](
|
|
59
|
+
[See the example project](https://github.com/EmilStenstrom/django-components/tree/master/sampleproject) or read on to learn about the details!
|
|
60
60
|
|
|
61
61
|
## Table of Contents
|
|
62
62
|
|
|
@@ -95,6 +95,13 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
95
95
|
|
|
96
96
|
## Release notes
|
|
97
97
|
|
|
98
|
+
**Version 0.97**
|
|
99
|
+
- Fixed template caching. You can now also manually create cached templates with [`cached_template()`](#template_cache_size---tune-the-template-cache)
|
|
100
|
+
- The previously undocumented `get_template` was made private.
|
|
101
|
+
- In it's place, there's a new `get_template`, which supersedes `get_template_string` (will be removed in v1). The new `get_template` is the same as `get_template_string`, except
|
|
102
|
+
it allows to return either a string or a Template instance.
|
|
103
|
+
- You now must use only one of `template`, `get_template`, `template_name`, or `get_template_name`.
|
|
104
|
+
|
|
98
105
|
**Version 0.96**
|
|
99
106
|
- Run-time type validation for Python 3.11+ - If the `Component` class is typed, e.g. `Component[Args, Kwargs, ...]`, the args, kwargs, slots, and data are validated against the given types. (See [Runtime input validation with types](#runtime-input-validation-with-types))
|
|
100
107
|
- Render hooks - Set `on_render_before` and `on_render_after` methods on `Component` to intercept or modify the template or context before rendering, or the rendered result afterwards. (See [Component hooks](#component-hooks))
|
|
@@ -117,7 +124,7 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
117
124
|
🚨📢 **Version 0.92**
|
|
118
125
|
- BREAKING CHANGE: `Component` class is no longer a subclass of `View`. To configure the `View` class, set the `Component.View` nested class. HTTP methods like `get` or `post` can still be defined directly on `Component` class, and `Component.as_view()` internally calls `Component.View.as_view()`. (See [Modifying the View class](#modifying-the-view-class))
|
|
119
126
|
|
|
120
|
-
- The inputs (args, kwargs, slots, context, ...) that you pass to `Component.render()` can be accessed from within `get_context_data`, `
|
|
127
|
+
- The inputs (args, kwargs, slots, context, ...) that you pass to `Component.render()` can be accessed from within `get_context_data`, `get_template` and `get_template_name` via `self.input`. (See [Accessing data passed to the component](#accessing-data-passed-to-the-component))
|
|
121
128
|
|
|
122
129
|
- Typing: `Component` class supports generics that specify types for `Component.render` (See [Adding type hints with Generics](#adding-type-hints-with-generics))
|
|
123
130
|
|
|
@@ -243,7 +250,7 @@ python manage.py runserver
|
|
|
243
250
|
```
|
|
244
251
|
|
|
245
252
|
For a step-by-step guide on deploying production server with static files,
|
|
246
|
-
[see the demo project](
|
|
253
|
+
[see the demo project](https://github.com/EmilStenstrom/django-components/tree/master/sampleproject).
|
|
247
254
|
|
|
248
255
|
## Installation
|
|
249
256
|
|
|
@@ -407,11 +414,13 @@ from django_components import Component, register
|
|
|
407
414
|
@register("calendar")
|
|
408
415
|
class Calendar(Component):
|
|
409
416
|
# Templates inside `[your apps]/components` dir and `[project root]/components` dir
|
|
410
|
-
# will be automatically found.
|
|
411
|
-
# you can override method `get_template_name` instead of specifying `template_name`.
|
|
417
|
+
# will be automatically found.
|
|
412
418
|
#
|
|
413
419
|
# `template_name` can be relative to dir where `calendar.py` is, or relative to STATICFILES_DIRS
|
|
414
420
|
template_name = "template.html"
|
|
421
|
+
# Or
|
|
422
|
+
def get_template_name(context):
|
|
423
|
+
return f"template-{context['name']}.html"
|
|
415
424
|
|
|
416
425
|
# This component takes one parameter, a date string to show in the template
|
|
417
426
|
def get_context_data(self, date):
|
|
@@ -1766,7 +1775,7 @@ When you call `Component.render` or `Component.render_to_response`, the inputs t
|
|
|
1766
1775
|
This means that you can use `self.input` inside:
|
|
1767
1776
|
- `get_context_data`
|
|
1768
1777
|
- `get_template_name`
|
|
1769
|
-
- `
|
|
1778
|
+
- `get_template`
|
|
1770
1779
|
|
|
1771
1780
|
`self.input` is defined only for the duration of `Component.render`, and raises `RuntimeError` when called outside of this.
|
|
1772
1781
|
|
|
@@ -3194,6 +3203,26 @@ COMPONENTS = {
|
|
|
3194
3203
|
}
|
|
3195
3204
|
```
|
|
3196
3205
|
|
|
3206
|
+
If you want add templates to the cache yourself, you can use `cached_template()`:
|
|
3207
|
+
|
|
3208
|
+
```py
|
|
3209
|
+
from django_components import cached_template
|
|
3210
|
+
|
|
3211
|
+
cached_template("Variable: {{ variable }}")
|
|
3212
|
+
|
|
3213
|
+
# You can optionally specify Template class, and other Template inputs:
|
|
3214
|
+
class MyTemplate(Template):
|
|
3215
|
+
pass
|
|
3216
|
+
|
|
3217
|
+
cached_template(
|
|
3218
|
+
"Variable: {{ variable }}",
|
|
3219
|
+
template_cls=MyTemplate,
|
|
3220
|
+
name=...
|
|
3221
|
+
origin=...
|
|
3222
|
+
engine=...
|
|
3223
|
+
)
|
|
3224
|
+
```
|
|
3225
|
+
|
|
3197
3226
|
### `context_behavior` - Make components isolated (or not)
|
|
3198
3227
|
|
|
3199
3228
|
> NOTE: `context_behavior` and `slot_context_behavior` options were merged in v0.70.
|
|
@@ -32,7 +32,7 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
32
32
|
<div class="calendar-component">Today's date is <span>2015-06-19</span></div>
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
[See the example project](
|
|
35
|
+
[See the example project](https://github.com/EmilStenstrom/django-components/tree/master/sampleproject) or read on to learn about the details!
|
|
36
36
|
|
|
37
37
|
## Table of Contents
|
|
38
38
|
|
|
@@ -71,6 +71,13 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
71
71
|
|
|
72
72
|
## Release notes
|
|
73
73
|
|
|
74
|
+
**Version 0.97**
|
|
75
|
+
- Fixed template caching. You can now also manually create cached templates with [`cached_template()`](#template_cache_size---tune-the-template-cache)
|
|
76
|
+
- The previously undocumented `get_template` was made private.
|
|
77
|
+
- In it's place, there's a new `get_template`, which supersedes `get_template_string` (will be removed in v1). The new `get_template` is the same as `get_template_string`, except
|
|
78
|
+
it allows to return either a string or a Template instance.
|
|
79
|
+
- You now must use only one of `template`, `get_template`, `template_name`, or `get_template_name`.
|
|
80
|
+
|
|
74
81
|
**Version 0.96**
|
|
75
82
|
- Run-time type validation for Python 3.11+ - If the `Component` class is typed, e.g. `Component[Args, Kwargs, ...]`, the args, kwargs, slots, and data are validated against the given types. (See [Runtime input validation with types](#runtime-input-validation-with-types))
|
|
76
83
|
- Render hooks - Set `on_render_before` and `on_render_after` methods on `Component` to intercept or modify the template or context before rendering, or the rendered result afterwards. (See [Component hooks](#component-hooks))
|
|
@@ -93,7 +100,7 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
93
100
|
🚨📢 **Version 0.92**
|
|
94
101
|
- BREAKING CHANGE: `Component` class is no longer a subclass of `View`. To configure the `View` class, set the `Component.View` nested class. HTTP methods like `get` or `post` can still be defined directly on `Component` class, and `Component.as_view()` internally calls `Component.View.as_view()`. (See [Modifying the View class](#modifying-the-view-class))
|
|
95
102
|
|
|
96
|
-
- The inputs (args, kwargs, slots, context, ...) that you pass to `Component.render()` can be accessed from within `get_context_data`, `
|
|
103
|
+
- The inputs (args, kwargs, slots, context, ...) that you pass to `Component.render()` can be accessed from within `get_context_data`, `get_template` and `get_template_name` via `self.input`. (See [Accessing data passed to the component](#accessing-data-passed-to-the-component))
|
|
97
104
|
|
|
98
105
|
- Typing: `Component` class supports generics that specify types for `Component.render` (See [Adding type hints with Generics](#adding-type-hints-with-generics))
|
|
99
106
|
|
|
@@ -219,7 +226,7 @@ python manage.py runserver
|
|
|
219
226
|
```
|
|
220
227
|
|
|
221
228
|
For a step-by-step guide on deploying production server with static files,
|
|
222
|
-
[see the demo project](
|
|
229
|
+
[see the demo project](https://github.com/EmilStenstrom/django-components/tree/master/sampleproject).
|
|
223
230
|
|
|
224
231
|
## Installation
|
|
225
232
|
|
|
@@ -383,11 +390,13 @@ from django_components import Component, register
|
|
|
383
390
|
@register("calendar")
|
|
384
391
|
class Calendar(Component):
|
|
385
392
|
# Templates inside `[your apps]/components` dir and `[project root]/components` dir
|
|
386
|
-
# will be automatically found.
|
|
387
|
-
# you can override method `get_template_name` instead of specifying `template_name`.
|
|
393
|
+
# will be automatically found.
|
|
388
394
|
#
|
|
389
395
|
# `template_name` can be relative to dir where `calendar.py` is, or relative to STATICFILES_DIRS
|
|
390
396
|
template_name = "template.html"
|
|
397
|
+
# Or
|
|
398
|
+
def get_template_name(context):
|
|
399
|
+
return f"template-{context['name']}.html"
|
|
391
400
|
|
|
392
401
|
# This component takes one parameter, a date string to show in the template
|
|
393
402
|
def get_context_data(self, date):
|
|
@@ -1742,7 +1751,7 @@ When you call `Component.render` or `Component.render_to_response`, the inputs t
|
|
|
1742
1751
|
This means that you can use `self.input` inside:
|
|
1743
1752
|
- `get_context_data`
|
|
1744
1753
|
- `get_template_name`
|
|
1745
|
-
- `
|
|
1754
|
+
- `get_template`
|
|
1746
1755
|
|
|
1747
1756
|
`self.input` is defined only for the duration of `Component.render`, and raises `RuntimeError` when called outside of this.
|
|
1748
1757
|
|
|
@@ -3170,6 +3179,26 @@ COMPONENTS = {
|
|
|
3170
3179
|
}
|
|
3171
3180
|
```
|
|
3172
3181
|
|
|
3182
|
+
If you want add templates to the cache yourself, you can use `cached_template()`:
|
|
3183
|
+
|
|
3184
|
+
```py
|
|
3185
|
+
from django_components import cached_template
|
|
3186
|
+
|
|
3187
|
+
cached_template("Variable: {{ variable }}")
|
|
3188
|
+
|
|
3189
|
+
# You can optionally specify Template class, and other Template inputs:
|
|
3190
|
+
class MyTemplate(Template):
|
|
3191
|
+
pass
|
|
3192
|
+
|
|
3193
|
+
cached_template(
|
|
3194
|
+
"Variable: {{ variable }}",
|
|
3195
|
+
template_cls=MyTemplate,
|
|
3196
|
+
name=...
|
|
3197
|
+
origin=...
|
|
3198
|
+
engine=...
|
|
3199
|
+
)
|
|
3200
|
+
```
|
|
3201
|
+
|
|
3173
3202
|
### `context_behavior` - Make components isolated (or not)
|
|
3174
3203
|
|
|
3175
3204
|
> NOTE: `context_behavior` and `slot_context_behavior` options were merged in v0.70.
|
|
@@ -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.97"
|
|
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"]
|
|
@@ -36,6 +36,7 @@ from django_components.tag_formatter import (
|
|
|
36
36
|
component_formatter as component_formatter,
|
|
37
37
|
component_shorthand_formatter as component_shorthand_formatter,
|
|
38
38
|
)
|
|
39
|
+
from django_components.template import cached_template as cached_template
|
|
39
40
|
import django_components.types as types
|
|
40
41
|
from django_components.types import (
|
|
41
42
|
EmptyTuple as EmptyTuple,
|
|
@@ -63,6 +63,7 @@ from django_components.slots import (
|
|
|
63
63
|
resolve_fill_nodes,
|
|
64
64
|
resolve_slots,
|
|
65
65
|
)
|
|
66
|
+
from django_components.template import cached_template
|
|
66
67
|
from django_components.utils import gen_id, validate_typed_dict, validate_typed_tuple
|
|
67
68
|
|
|
68
69
|
# TODO_REMOVE_IN_V1 - Users should use top-level import instead
|
|
@@ -154,10 +155,42 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
|
|
|
154
155
|
# non-null return.
|
|
155
156
|
_class_hash: ClassVar[int]
|
|
156
157
|
|
|
157
|
-
template_name:
|
|
158
|
-
"""
|
|
159
|
-
template
|
|
160
|
-
|
|
158
|
+
template_name: Optional[str] = None
|
|
159
|
+
"""
|
|
160
|
+
Filepath to the Django template associated with this component.
|
|
161
|
+
|
|
162
|
+
The filepath must be relative to either the file where the component class was defined,
|
|
163
|
+
or one of the roots of `STATIFILES_DIRS`.
|
|
164
|
+
|
|
165
|
+
Only one of `template_name`, `get_template_name`, `template` or `get_template` must be defined.
|
|
166
|
+
"""
|
|
167
|
+
|
|
168
|
+
def get_template_name(self, context: Context) -> Optional[str]:
|
|
169
|
+
"""
|
|
170
|
+
Filepath to the Django template associated with this component.
|
|
171
|
+
|
|
172
|
+
The filepath must be relative to either the file where the component class was defined,
|
|
173
|
+
or one of the roots of `STATIFILES_DIRS`.
|
|
174
|
+
|
|
175
|
+
Only one of `template_name`, `get_template_name`, `template` or `get_template` must be defined.
|
|
176
|
+
"""
|
|
177
|
+
return None
|
|
178
|
+
|
|
179
|
+
template: Optional[Union[str, Template]] = None
|
|
180
|
+
"""
|
|
181
|
+
Inlined Django template associated with this component. Can be a plain string or a Template instance.
|
|
182
|
+
|
|
183
|
+
Only one of `template_name`, `get_template_name`, `template` or `get_template` must be defined.
|
|
184
|
+
"""
|
|
185
|
+
|
|
186
|
+
def get_template(self, context: Context) -> Optional[Union[str, Template]]:
|
|
187
|
+
"""
|
|
188
|
+
Inlined Django template associated with this component. Can be a plain string or a Template instance.
|
|
189
|
+
|
|
190
|
+
Only one of `template_name`, `get_template_name`, `template` or `get_template` must be defined.
|
|
191
|
+
"""
|
|
192
|
+
return None
|
|
193
|
+
|
|
161
194
|
js: Optional[str] = None
|
|
162
195
|
"""Inlined JS associated with this component."""
|
|
163
196
|
css: Optional[str] = None
|
|
@@ -274,28 +307,55 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
|
|
|
274
307
|
def get_context_data(self, *args: Any, **kwargs: Any) -> DataType:
|
|
275
308
|
return cast(DataType, {})
|
|
276
309
|
|
|
277
|
-
def get_template_name(self, context: Context) -> Optional[str]:
|
|
278
|
-
return self.template_name
|
|
279
|
-
|
|
280
|
-
def get_template_string(self, context: Context) -> Optional[str]:
|
|
281
|
-
return self.template
|
|
282
|
-
|
|
283
310
|
# NOTE: When the template is taken from a file (AKA specified via `template_name`),
|
|
284
311
|
# then we leverage Django's template caching. This means that the same instance
|
|
285
312
|
# of Template is reused. This is important to keep in mind, because the implication
|
|
286
313
|
# is that we should treat Templates AND their nodelists as IMMUTABLE.
|
|
287
|
-
def
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
314
|
+
def _get_template(self, context: Context) -> Template:
|
|
315
|
+
# Resolve template name
|
|
316
|
+
template_name = self.template_name
|
|
317
|
+
if self.template_name is not None:
|
|
318
|
+
if self.get_template_name(context) is not None:
|
|
319
|
+
raise ImproperlyConfigured(
|
|
320
|
+
"Received non-null value from both 'template_name' and 'get_template_name' in"
|
|
321
|
+
f" Component {type(self).__name__}. Only one of the two must be set."
|
|
322
|
+
)
|
|
323
|
+
else:
|
|
324
|
+
template_name = self.get_template_name(context)
|
|
325
|
+
|
|
326
|
+
# Resolve template str
|
|
327
|
+
template_input = self.template
|
|
328
|
+
if self.template is not None:
|
|
329
|
+
if self.get_template(context) is not None:
|
|
330
|
+
raise ImproperlyConfigured(
|
|
331
|
+
"Received non-null value from both 'template' and 'get_template' in"
|
|
332
|
+
f" Component {type(self).__name__}. Only one of the two must be set."
|
|
333
|
+
)
|
|
334
|
+
else:
|
|
335
|
+
# TODO_REMOVE_IN_V1 - Remove `self.get_template_string` in v1
|
|
336
|
+
template_getter = getattr(self, "get_template_string", self.get_template)
|
|
337
|
+
template_input = template_getter(context)
|
|
338
|
+
|
|
339
|
+
if template_name is not None and template_input is not None:
|
|
340
|
+
raise ImproperlyConfigured(
|
|
341
|
+
f"Received both 'template_name' and 'template' in Component {type(self).__name__}."
|
|
342
|
+
" Only one of the two must be set."
|
|
343
|
+
)
|
|
291
344
|
|
|
292
|
-
template_name = self.get_template_name(context)
|
|
293
345
|
if template_name is not None:
|
|
294
346
|
return get_template(template_name).template
|
|
295
347
|
|
|
348
|
+
elif template_input is not None:
|
|
349
|
+
# We got template string, so we convert it to Template
|
|
350
|
+
if isinstance(template_input, str):
|
|
351
|
+
template: Template = cached_template(template_input)
|
|
352
|
+
else:
|
|
353
|
+
template = template_input
|
|
354
|
+
|
|
355
|
+
return template
|
|
356
|
+
|
|
296
357
|
raise ImproperlyConfigured(
|
|
297
358
|
f"Either 'template_name' or 'template' must be set for Component {type(self).__name__}."
|
|
298
|
-
f"Note: this attribute is not required if you are overriding the class's `get_template*()` methods."
|
|
299
359
|
)
|
|
300
360
|
|
|
301
361
|
def render_dependencies(self) -> SafeString:
|
|
@@ -606,7 +666,6 @@ class Component(Generic[ArgsType, KwargsType, DataType, SlotsType], metaclass=Co
|
|
|
606
666
|
self.on_render_before(context, template)
|
|
607
667
|
|
|
608
668
|
rendered_component = template.render(context)
|
|
609
|
-
|
|
610
669
|
new_output = self.on_render_after(context, template, rendered_component)
|
|
611
670
|
rendered_component = new_output if new_output is not None else rendered_component
|
|
612
671
|
|
|
@@ -884,7 +943,7 @@ def _prepare_template(
|
|
|
884
943
|
# an error when we try to use `{% include %}` tag inside the template?
|
|
885
944
|
# See https://github.com/EmilStenstrom/django-components/issues/580
|
|
886
945
|
# And https://github.com/EmilStenstrom/django-components/issues/634
|
|
887
|
-
template = component.
|
|
946
|
+
template = component._get_template(context)
|
|
888
947
|
_monkeypatch_template(template)
|
|
889
948
|
|
|
890
949
|
# Set `Template._dc_is_component_nested` based on whether we're currently INSIDE
|
|
@@ -3,6 +3,7 @@ import json
|
|
|
3
3
|
import re
|
|
4
4
|
from collections import deque
|
|
5
5
|
from dataclasses import dataclass
|
|
6
|
+
from functools import lru_cache
|
|
6
7
|
from typing import (
|
|
7
8
|
TYPE_CHECKING,
|
|
8
9
|
Any,
|
|
@@ -27,7 +28,7 @@ from django.template.defaulttags import CommentNode
|
|
|
27
28
|
from django.template.exceptions import TemplateSyntaxError
|
|
28
29
|
from django.utils.safestring import SafeString, mark_safe
|
|
29
30
|
|
|
30
|
-
from django_components.app_settings import ContextBehavior
|
|
31
|
+
from django_components.app_settings import ContextBehavior, app_settings
|
|
31
32
|
from django_components.context import (
|
|
32
33
|
_FILLED_SLOTS_CONTENT_CONTEXT_KEY,
|
|
33
34
|
_INJECT_CONTEXT_KEY_PREFIX,
|
|
@@ -37,6 +38,7 @@ from django_components.context import (
|
|
|
37
38
|
from django_components.expression import RuntimeKwargs, is_identifier
|
|
38
39
|
from django_components.logger import trace_msg
|
|
39
40
|
from django_components.node import BaseNode, NodeTraverse, nodelist_has_content, walk_nodelist
|
|
41
|
+
from django_components.utils import lazy_cache
|
|
40
42
|
|
|
41
43
|
if TYPE_CHECKING:
|
|
42
44
|
from django_components.component_registry import ComponentRegistry
|
|
@@ -332,9 +334,13 @@ class FillNode(BaseNode):
|
|
|
332
334
|
return value
|
|
333
335
|
|
|
334
336
|
|
|
337
|
+
# NOTE: There may be more components per template, so using `app_settings.TEMPLATE_CACHE_SIZE`
|
|
338
|
+
# is not entirely correct. However, for now it's not worth it adding a separate setting
|
|
339
|
+
# to control this cache separately. So we use `TEMPLATE_CACHE_SIZE` so the cache is bounded.
|
|
340
|
+
@lazy_cache(lambda: lru_cache(maxsize=app_settings.TEMPLATE_CACHE_SIZE))
|
|
335
341
|
def parse_slot_fill_nodes_from_component_nodelist(
|
|
336
|
-
|
|
337
|
-
|
|
342
|
+
nodes: Tuple[Node, ...],
|
|
343
|
+
ignored_nodes: Tuple[Type[Node]],
|
|
338
344
|
) -> List[FillNode]:
|
|
339
345
|
"""
|
|
340
346
|
Given a component body (`django.template.NodeList`), find all slot fills,
|
|
@@ -355,12 +361,12 @@ def parse_slot_fill_nodes_from_component_nodelist(
|
|
|
355
361
|
and `fill "second_fill"`.
|
|
356
362
|
"""
|
|
357
363
|
fill_nodes: List[FillNode] = []
|
|
358
|
-
if nodelist_has_content(
|
|
364
|
+
if nodelist_has_content(nodes):
|
|
359
365
|
for parse_fn in (
|
|
360
366
|
_try_parse_as_default_fill,
|
|
361
367
|
_try_parse_as_named_fill_tag_set,
|
|
362
368
|
):
|
|
363
|
-
curr_fill_nodes = parse_fn(
|
|
369
|
+
curr_fill_nodes = parse_fn(nodes, ignored_nodes)
|
|
364
370
|
if curr_fill_nodes:
|
|
365
371
|
fill_nodes = curr_fill_nodes
|
|
366
372
|
break
|
|
@@ -375,12 +381,12 @@ def parse_slot_fill_nodes_from_component_nodelist(
|
|
|
375
381
|
|
|
376
382
|
|
|
377
383
|
def _try_parse_as_named_fill_tag_set(
|
|
378
|
-
|
|
379
|
-
|
|
384
|
+
nodes: Tuple[Node, ...],
|
|
385
|
+
ignored_nodes: Tuple[Type[Node]],
|
|
380
386
|
) -> List[FillNode]:
|
|
381
387
|
result = []
|
|
382
388
|
seen_names: Set[str] = set()
|
|
383
|
-
for node in
|
|
389
|
+
for node in nodes:
|
|
384
390
|
if isinstance(node, FillNode):
|
|
385
391
|
# If the fill name was defined statically, then check for no duplicates.
|
|
386
392
|
maybe_fill_name = node.kwargs.kwargs.get(SLOT_NAME_KWARG)
|
|
@@ -402,15 +408,15 @@ def _try_parse_as_named_fill_tag_set(
|
|
|
402
408
|
|
|
403
409
|
|
|
404
410
|
def _try_parse_as_default_fill(
|
|
405
|
-
|
|
406
|
-
|
|
411
|
+
nodes: Tuple[Node, ...],
|
|
412
|
+
ignored_nodes: Tuple[Type[Node]],
|
|
407
413
|
) -> List[FillNode]:
|
|
408
|
-
nodes_stack: List[Node] = list(
|
|
414
|
+
nodes_stack: List[Node] = list(nodes)
|
|
409
415
|
while nodes_stack:
|
|
410
416
|
node = nodes_stack.pop()
|
|
411
417
|
if isinstance(node, FillNode):
|
|
412
418
|
return []
|
|
413
|
-
elif isinstance(node,
|
|
419
|
+
elif isinstance(node, ignored_nodes):
|
|
414
420
|
# Stop searching here, as fill tags are permitted inside component blocks
|
|
415
421
|
# embedded within a default fill node.
|
|
416
422
|
continue
|
|
@@ -419,7 +425,7 @@ def _try_parse_as_default_fill(
|
|
|
419
425
|
else:
|
|
420
426
|
return [
|
|
421
427
|
FillNode(
|
|
422
|
-
nodelist=
|
|
428
|
+
nodelist=NodeList(nodes),
|
|
423
429
|
kwargs=RuntimeKwargs(
|
|
424
430
|
{
|
|
425
431
|
# Wrap the default slot name in quotes so it's treated as FilterExpression
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
from functools import lru_cache
|
|
2
|
+
from typing import Any, Optional, Type, TypeVar
|
|
3
|
+
|
|
4
|
+
from django.template import Origin, Template
|
|
5
|
+
from django.template.base import UNKNOWN_SOURCE
|
|
6
|
+
|
|
7
|
+
from django_components.app_settings import app_settings
|
|
8
|
+
from django_components.utils import lazy_cache
|
|
9
|
+
|
|
10
|
+
TTemplate = TypeVar("TTemplate", bound=Template)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# Lazily initialize the cache. The cached function takes only the parts that can
|
|
14
|
+
# affect how the template string is processed - Template class, template string, and engine
|
|
15
|
+
@lazy_cache(lambda: lru_cache(maxsize=app_settings.TEMPLATE_CACHE_SIZE))
|
|
16
|
+
def _create_template(
|
|
17
|
+
template_cls: Type[TTemplate],
|
|
18
|
+
template_string: str,
|
|
19
|
+
engine: Optional[Any] = None,
|
|
20
|
+
) -> TTemplate:
|
|
21
|
+
return template_cls(template_string, engine=engine)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
# Central logic for creating Templates from string, so we can cache the results
|
|
25
|
+
def cached_template(
|
|
26
|
+
template_string: str,
|
|
27
|
+
template_cls: Optional[Type[Template]] = None,
|
|
28
|
+
origin: Optional[Origin] = None,
|
|
29
|
+
name: Optional[str] = None,
|
|
30
|
+
engine: Optional[Any] = None,
|
|
31
|
+
) -> Template:
|
|
32
|
+
"""Create a Template instance that will be cached as per the `TEMPLATE_CACHE_SIZE` setting."""
|
|
33
|
+
template = _create_template(template_cls or Template, template_string, engine)
|
|
34
|
+
|
|
35
|
+
# Assign the origin and name separately, so the caching doesn't depend on them
|
|
36
|
+
# Since we might be accessing a template from cache, we want to define these only once
|
|
37
|
+
if not getattr(template, "_dc_cached", False):
|
|
38
|
+
template.origin = origin or Origin(UNKNOWN_SOURCE)
|
|
39
|
+
template.name = name
|
|
40
|
+
template._dc_cached = True
|
|
41
|
+
|
|
42
|
+
return template
|
|
@@ -246,7 +246,7 @@ def component(parser: Parser, token: Token, registry: ComponentRegistry, tag_nam
|
|
|
246
246
|
trace_msg("PARSE", "COMP", result.component_name, tag.id)
|
|
247
247
|
|
|
248
248
|
body = tag.parse_body()
|
|
249
|
-
fill_nodes = parse_slot_fill_nodes_from_component_nodelist(body, ComponentNode)
|
|
249
|
+
fill_nodes = parse_slot_fill_nodes_from_component_nodelist(tuple(body), ignored_nodes=(ComponentNode,))
|
|
250
250
|
|
|
251
251
|
# Tag all fill nodes as children of this particular component instance
|
|
252
252
|
for node in fill_nodes:
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import functools
|
|
1
2
|
import sys
|
|
2
3
|
import typing
|
|
3
4
|
from pathlib import Path
|
|
4
|
-
from typing import Any, Callable, List, Mapping, Sequence, Tuple, Union, get_type_hints
|
|
5
|
+
from typing import Any, Callable, List, Mapping, Sequence, Tuple, TypeVar, Union, cast, get_type_hints
|
|
5
6
|
|
|
6
7
|
from django.utils.autoreload import autoreload_started
|
|
7
8
|
|
|
@@ -166,3 +167,47 @@ def validate_typed_dict(value: Mapping[str, Any], dict_type: Any, prefix: str, k
|
|
|
166
167
|
# `Component 'name' got unexpected slot keys 'invalid_key'`
|
|
167
168
|
# `Component 'name' got unexpected data keys 'invalid_key'`
|
|
168
169
|
raise TypeError(f"{prefix} got unexpected {kind} keys {formatted_keys}")
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
TFunc = TypeVar("TFunc", bound=Callable)
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
def lazy_cache(
|
|
176
|
+
make_cache: Callable[[], Callable[[Callable], Callable]],
|
|
177
|
+
) -> Callable[[TFunc], TFunc]:
|
|
178
|
+
"""
|
|
179
|
+
Decorator that caches the given function similarly to `functools.lru_cache`.
|
|
180
|
+
But the cache is instantiated only at first invocation.
|
|
181
|
+
|
|
182
|
+
`cache` argument is a function that generates the cache function,
|
|
183
|
+
e.g. `functools.lru_cache()`.
|
|
184
|
+
"""
|
|
185
|
+
_cached_fn = None
|
|
186
|
+
|
|
187
|
+
def decorator(fn: TFunc) -> TFunc:
|
|
188
|
+
@functools.wraps(fn)
|
|
189
|
+
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
190
|
+
# Lazily initialize the cache
|
|
191
|
+
nonlocal _cached_fn
|
|
192
|
+
if not _cached_fn:
|
|
193
|
+
# E.g. `lambda: functools.lru_cache(maxsize=app_settings.TEMPLATE_CACHE_SIZE)`
|
|
194
|
+
cache = make_cache()
|
|
195
|
+
_cached_fn = cache(fn)
|
|
196
|
+
|
|
197
|
+
return _cached_fn(*args, **kwargs)
|
|
198
|
+
|
|
199
|
+
# Allow to access the LRU cache methods
|
|
200
|
+
# See https://stackoverflow.com/a/37654201/9788634
|
|
201
|
+
wrapper.cache_info = lambda: _cached_fn.cache_info() # type: ignore
|
|
202
|
+
wrapper.cache_clear = lambda: _cached_fn.cache_clear() # type: ignore
|
|
203
|
+
|
|
204
|
+
# And allow to remove the cache instance (mostly for tests)
|
|
205
|
+
def cache_remove() -> None:
|
|
206
|
+
nonlocal _cached_fn
|
|
207
|
+
_cached_fn = None
|
|
208
|
+
|
|
209
|
+
wrapper.cache_remove = cache_remove # type: ignore
|
|
210
|
+
|
|
211
|
+
return cast(TFunc, wrapper)
|
|
212
|
+
|
|
213
|
+
return decorator
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.97
|
|
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
|
|
@@ -56,7 +56,7 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
56
56
|
<div class="calendar-component">Today's date is <span>2015-06-19</span></div>
|
|
57
57
|
```
|
|
58
58
|
|
|
59
|
-
[See the example project](
|
|
59
|
+
[See the example project](https://github.com/EmilStenstrom/django-components/tree/master/sampleproject) or read on to learn about the details!
|
|
60
60
|
|
|
61
61
|
## Table of Contents
|
|
62
62
|
|
|
@@ -95,6 +95,13 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
95
95
|
|
|
96
96
|
## Release notes
|
|
97
97
|
|
|
98
|
+
**Version 0.97**
|
|
99
|
+
- Fixed template caching. You can now also manually create cached templates with [`cached_template()`](#template_cache_size---tune-the-template-cache)
|
|
100
|
+
- The previously undocumented `get_template` was made private.
|
|
101
|
+
- In it's place, there's a new `get_template`, which supersedes `get_template_string` (will be removed in v1). The new `get_template` is the same as `get_template_string`, except
|
|
102
|
+
it allows to return either a string or a Template instance.
|
|
103
|
+
- You now must use only one of `template`, `get_template`, `template_name`, or `get_template_name`.
|
|
104
|
+
|
|
98
105
|
**Version 0.96**
|
|
99
106
|
- Run-time type validation for Python 3.11+ - If the `Component` class is typed, e.g. `Component[Args, Kwargs, ...]`, the args, kwargs, slots, and data are validated against the given types. (See [Runtime input validation with types](#runtime-input-validation-with-types))
|
|
100
107
|
- Render hooks - Set `on_render_before` and `on_render_after` methods on `Component` to intercept or modify the template or context before rendering, or the rendered result afterwards. (See [Component hooks](#component-hooks))
|
|
@@ -117,7 +124,7 @@ And this is what gets rendered (plus the CSS and Javascript you've specified):
|
|
|
117
124
|
🚨📢 **Version 0.92**
|
|
118
125
|
- BREAKING CHANGE: `Component` class is no longer a subclass of `View`. To configure the `View` class, set the `Component.View` nested class. HTTP methods like `get` or `post` can still be defined directly on `Component` class, and `Component.as_view()` internally calls `Component.View.as_view()`. (See [Modifying the View class](#modifying-the-view-class))
|
|
119
126
|
|
|
120
|
-
- The inputs (args, kwargs, slots, context, ...) that you pass to `Component.render()` can be accessed from within `get_context_data`, `
|
|
127
|
+
- The inputs (args, kwargs, slots, context, ...) that you pass to `Component.render()` can be accessed from within `get_context_data`, `get_template` and `get_template_name` via `self.input`. (See [Accessing data passed to the component](#accessing-data-passed-to-the-component))
|
|
121
128
|
|
|
122
129
|
- Typing: `Component` class supports generics that specify types for `Component.render` (See [Adding type hints with Generics](#adding-type-hints-with-generics))
|
|
123
130
|
|
|
@@ -243,7 +250,7 @@ python manage.py runserver
|
|
|
243
250
|
```
|
|
244
251
|
|
|
245
252
|
For a step-by-step guide on deploying production server with static files,
|
|
246
|
-
[see the demo project](
|
|
253
|
+
[see the demo project](https://github.com/EmilStenstrom/django-components/tree/master/sampleproject).
|
|
247
254
|
|
|
248
255
|
## Installation
|
|
249
256
|
|
|
@@ -407,11 +414,13 @@ from django_components import Component, register
|
|
|
407
414
|
@register("calendar")
|
|
408
415
|
class Calendar(Component):
|
|
409
416
|
# Templates inside `[your apps]/components` dir and `[project root]/components` dir
|
|
410
|
-
# will be automatically found.
|
|
411
|
-
# you can override method `get_template_name` instead of specifying `template_name`.
|
|
417
|
+
# will be automatically found.
|
|
412
418
|
#
|
|
413
419
|
# `template_name` can be relative to dir where `calendar.py` is, or relative to STATICFILES_DIRS
|
|
414
420
|
template_name = "template.html"
|
|
421
|
+
# Or
|
|
422
|
+
def get_template_name(context):
|
|
423
|
+
return f"template-{context['name']}.html"
|
|
415
424
|
|
|
416
425
|
# This component takes one parameter, a date string to show in the template
|
|
417
426
|
def get_context_data(self, date):
|
|
@@ -1766,7 +1775,7 @@ When you call `Component.render` or `Component.render_to_response`, the inputs t
|
|
|
1766
1775
|
This means that you can use `self.input` inside:
|
|
1767
1776
|
- `get_context_data`
|
|
1768
1777
|
- `get_template_name`
|
|
1769
|
-
- `
|
|
1778
|
+
- `get_template`
|
|
1770
1779
|
|
|
1771
1780
|
`self.input` is defined only for the duration of `Component.render`, and raises `RuntimeError` when called outside of this.
|
|
1772
1781
|
|
|
@@ -3194,6 +3203,26 @@ COMPONENTS = {
|
|
|
3194
3203
|
}
|
|
3195
3204
|
```
|
|
3196
3205
|
|
|
3206
|
+
If you want add templates to the cache yourself, you can use `cached_template()`:
|
|
3207
|
+
|
|
3208
|
+
```py
|
|
3209
|
+
from django_components import cached_template
|
|
3210
|
+
|
|
3211
|
+
cached_template("Variable: {{ variable }}")
|
|
3212
|
+
|
|
3213
|
+
# You can optionally specify Template class, and other Template inputs:
|
|
3214
|
+
class MyTemplate(Template):
|
|
3215
|
+
pass
|
|
3216
|
+
|
|
3217
|
+
cached_template(
|
|
3218
|
+
"Variable: {{ variable }}",
|
|
3219
|
+
template_cls=MyTemplate,
|
|
3220
|
+
name=...
|
|
3221
|
+
origin=...
|
|
3222
|
+
engine=...
|
|
3223
|
+
)
|
|
3224
|
+
```
|
|
3225
|
+
|
|
3197
3226
|
### `context_behavior` - Make components isolated (or not)
|
|
3198
3227
|
|
|
3199
3228
|
> NOTE: `context_behavior` and `slot_context_behavior` options were merged in v0.70.
|
{django_components-0.96 → django_components-0.97}/src/django_components.egg-info/SOURCES.txt
RENAMED
|
@@ -19,6 +19,7 @@ src/django_components/provide.py
|
|
|
19
19
|
src/django_components/py.typed
|
|
20
20
|
src/django_components/slots.py
|
|
21
21
|
src/django_components/tag_formatter.py
|
|
22
|
+
src/django_components/template.py
|
|
22
23
|
src/django_components/template_loader.py
|
|
23
24
|
src/django_components/template_parser.py
|
|
24
25
|
src/django_components/types.py
|
|
@@ -51,6 +52,7 @@ tests/test_registry.py
|
|
|
51
52
|
tests/test_safer_staticfiles.py
|
|
52
53
|
tests/test_settings.py
|
|
53
54
|
tests/test_tag_formatter.py
|
|
55
|
+
tests/test_template.py
|
|
54
56
|
tests/test_template_loader.py
|
|
55
57
|
tests/test_template_parser.py
|
|
56
58
|
tests/test_templatetags.py
|
|
@@ -76,6 +76,35 @@ else:
|
|
|
76
76
|
optional: NotRequired[int]
|
|
77
77
|
|
|
78
78
|
|
|
79
|
+
# TODO_REMOVE_IN_V1 - Superseded by `self.get_template` in v1
|
|
80
|
+
class ComponentOldTemplateApiTest(BaseTestCase):
|
|
81
|
+
@parametrize_context_behavior(["django", "isolated"])
|
|
82
|
+
def test_get_template_string(self):
|
|
83
|
+
class SimpleComponent(Component):
|
|
84
|
+
def get_template_string(self, context):
|
|
85
|
+
content: types.django_html = """
|
|
86
|
+
Variable: <strong>{{ variable }}</strong>
|
|
87
|
+
"""
|
|
88
|
+
return content
|
|
89
|
+
|
|
90
|
+
def get_context_data(self, variable=None):
|
|
91
|
+
return {
|
|
92
|
+
"variable": variable,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
class Media:
|
|
96
|
+
css = "style.css"
|
|
97
|
+
js = "script.js"
|
|
98
|
+
|
|
99
|
+
rendered = SimpleComponent.render(kwargs={"variable": "test"})
|
|
100
|
+
self.assertHTMLEqual(
|
|
101
|
+
rendered,
|
|
102
|
+
"""
|
|
103
|
+
Variable: <strong>test</strong>
|
|
104
|
+
""",
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
|
|
79
108
|
class ComponentTest(BaseTestCase):
|
|
80
109
|
class ParentComponent(Component):
|
|
81
110
|
template: types.django_html = """
|
|
@@ -123,7 +152,7 @@ class ComponentTest(BaseTestCase):
|
|
|
123
152
|
pass
|
|
124
153
|
|
|
125
154
|
with self.assertRaises(ImproperlyConfigured):
|
|
126
|
-
EmptyComponent("empty_component").
|
|
155
|
+
EmptyComponent("empty_component")._get_template(Context({}))
|
|
127
156
|
|
|
128
157
|
@parametrize_context_behavior(["django", "isolated"])
|
|
129
158
|
def test_template_string_static_inlined(self):
|
|
@@ -152,7 +181,7 @@ class ComponentTest(BaseTestCase):
|
|
|
152
181
|
@parametrize_context_behavior(["django", "isolated"])
|
|
153
182
|
def test_template_string_dynamic(self):
|
|
154
183
|
class SimpleComponent(Component):
|
|
155
|
-
def
|
|
184
|
+
def get_template(self, context):
|
|
156
185
|
content: types.django_html = """
|
|
157
186
|
Variable: <strong>{{ variable }}</strong>
|
|
158
187
|
"""
|
|
@@ -225,7 +254,7 @@ class ComponentTest(BaseTestCase):
|
|
|
225
254
|
)
|
|
226
255
|
|
|
227
256
|
@parametrize_context_behavior(["django", "isolated"])
|
|
228
|
-
def
|
|
257
|
+
def test_allows_to_return_template(self):
|
|
229
258
|
class TestComponent(Component):
|
|
230
259
|
def get_context_data(self, variable, **attrs):
|
|
231
260
|
return {
|
|
@@ -1037,7 +1066,6 @@ class ComponentRenderTest(BaseTestCase):
|
|
|
1037
1066
|
|
|
1038
1067
|
|
|
1039
1068
|
class ComponentHookTest(BaseTestCase):
|
|
1040
|
-
@parametrize_context_behavior(["django", "isolated"])
|
|
1041
1069
|
def test_on_render_before(self):
|
|
1042
1070
|
class SimpleComponent(Component):
|
|
1043
1071
|
template: types.django_html = """
|
|
@@ -1075,7 +1103,6 @@ class ComponentHookTest(BaseTestCase):
|
|
|
1075
1103
|
)
|
|
1076
1104
|
|
|
1077
1105
|
# Check that modifying the context or template does nothing
|
|
1078
|
-
@parametrize_context_behavior(["django", "isolated"])
|
|
1079
1106
|
def test_on_render_after(self):
|
|
1080
1107
|
captured_content = None
|
|
1081
1108
|
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
from django.template import Context, Template
|
|
2
|
+
from django.test import override_settings
|
|
3
|
+
|
|
4
|
+
from django_components import Component, cached_template, types
|
|
5
|
+
|
|
6
|
+
from .django_test_setup import setup_test_config
|
|
7
|
+
from .testutils import BaseTestCase
|
|
8
|
+
|
|
9
|
+
setup_test_config({"autodiscover": False})
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class TemplateCacheTest(BaseTestCase):
|
|
13
|
+
def test_cached_template(self):
|
|
14
|
+
template_1 = cached_template("Variable: <strong>{{ variable }}</strong>")
|
|
15
|
+
template_1._test_id = "123"
|
|
16
|
+
|
|
17
|
+
template_2 = cached_template("Variable: <strong>{{ variable }}</strong>")
|
|
18
|
+
|
|
19
|
+
self.assertEqual(template_2._test_id, "123")
|
|
20
|
+
|
|
21
|
+
def test_cached_template_accepts_class(self):
|
|
22
|
+
class MyTemplate(Template):
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
template = cached_template("Variable: <strong>{{ variable }}</strong>", MyTemplate)
|
|
26
|
+
self.assertIsInstance(template, MyTemplate)
|
|
27
|
+
|
|
28
|
+
@override_settings(COMPONENTS={"template_cache_size": 2})
|
|
29
|
+
def test_cache_discards_old_entries(self):
|
|
30
|
+
template_1 = cached_template("Variable: <strong>{{ variable }}</strong>")
|
|
31
|
+
template_1._test_id = "123"
|
|
32
|
+
|
|
33
|
+
template_2 = cached_template("Variable2")
|
|
34
|
+
template_2._test_id = "456"
|
|
35
|
+
|
|
36
|
+
# Templates 1 and 2 should still be available
|
|
37
|
+
template_1_copy = cached_template("Variable: <strong>{{ variable }}</strong>")
|
|
38
|
+
self.assertEqual(template_1_copy._test_id, "123")
|
|
39
|
+
|
|
40
|
+
template_2_copy = cached_template("Variable2")
|
|
41
|
+
self.assertEqual(template_2_copy._test_id, "456")
|
|
42
|
+
|
|
43
|
+
# But once we add the third template, template 1 should go
|
|
44
|
+
cached_template("Variable3")
|
|
45
|
+
|
|
46
|
+
template_1_copy2 = cached_template("Variable: <strong>{{ variable }}</strong>")
|
|
47
|
+
self.assertEqual(hasattr(template_1_copy2, "_test_id"), False)
|
|
48
|
+
|
|
49
|
+
def test_component_template_is_cached(self):
|
|
50
|
+
class SimpleComponent(Component):
|
|
51
|
+
def get_template(self, context):
|
|
52
|
+
content: types.django_html = """
|
|
53
|
+
Variable: <strong>{{ variable }}</strong>
|
|
54
|
+
"""
|
|
55
|
+
return content
|
|
56
|
+
|
|
57
|
+
def get_context_data(self, variable=None):
|
|
58
|
+
return {
|
|
59
|
+
"variable": variable,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
comp = SimpleComponent()
|
|
63
|
+
template_1 = comp._get_template(Context({}))
|
|
64
|
+
template_1._test_id = "123"
|
|
65
|
+
|
|
66
|
+
template_2 = comp._get_template(Context({}))
|
|
67
|
+
self.assertEqual(template_2._test_id, "123")
|
|
@@ -434,7 +434,7 @@ class ComponentSlottedTemplateTagTest(BaseTestCase):
|
|
|
434
434
|
@parametrize_context_behavior(["django", "isolated"])
|
|
435
435
|
def test_component_template_cannot_have_multiple_default_slots(self):
|
|
436
436
|
class BadComponent(Component):
|
|
437
|
-
def get_template(self, context
|
|
437
|
+
def get_template(self, context):
|
|
438
438
|
template_str: types.django_html = """
|
|
439
439
|
{% load django_components %}
|
|
440
440
|
<div>
|
|
@@ -24,6 +24,10 @@ class BaseTestCase(SimpleTestCase):
|
|
|
24
24
|
super().tearDown()
|
|
25
25
|
registry.clear()
|
|
26
26
|
|
|
27
|
+
from django_components.template import _create_template
|
|
28
|
+
|
|
29
|
+
_create_template.cache_remove() # type: ignore[attr-defined]
|
|
30
|
+
|
|
27
31
|
|
|
28
32
|
request = Mock()
|
|
29
33
|
mock_template = Mock()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.96 → django_components-0.97}/src/django_components/component_registry.py
RENAMED
|
File without changes
|
{django_components-0.96 → django_components-0.97}/src/django_components/components/__init__.py
RENAMED
|
File without changes
|
{django_components-0.96 → django_components-0.97}/src/django_components/components/dynamic.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.96 → django_components-0.97}/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
|
|
File without changes
|
{django_components-0.96 → django_components-0.97}/src/django_components/safer_staticfiles/apps.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.96 → django_components-0.97}/src/django_components/templatetags/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.96 → django_components-0.97}/src/django_components.egg-info/requires.txt
RENAMED
|
File without changes
|
{django_components-0.96 → django_components-0.97}/src/django_components.egg-info/top_level.txt
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
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|