django-components 0.50__tar.gz → 0.52__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.50 → django_components-0.52}/PKG-INFO +15 -6
- {django_components-0.50 → django_components-0.52}/README.md +14 -5
- {django_components-0.50 → django_components-0.52}/django_components/__init__.py +3 -1
- {django_components-0.50 → django_components-0.52}/django_components/component.py +13 -13
- {django_components-0.50 → django_components-0.52}/django_components/template_loader.py +1 -1
- {django_components-0.50 → django_components-0.52}/django_components/templatetags/component_tags.py +24 -13
- django_components-0.52/django_components/types.py +21 -0
- {django_components-0.50 → django_components-0.52}/django_components.egg-info/PKG-INFO +15 -6
- {django_components-0.50 → django_components-0.52}/django_components.egg-info/SOURCES.txt +1 -0
- {django_components-0.50 → django_components-0.52}/pyproject.toml +7 -0
- {django_components-0.50 → django_components-0.52}/setup.py +4 -1
- {django_components-0.50 → django_components-0.52}/tests/test_context.py +4 -0
- {django_components-0.50 → django_components-0.52}/tests/test_templatetags.py +2 -2
- {django_components-0.50 → django_components-0.52}/LICENSE +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/app_settings.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/apps.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/component_registry.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/management/__init__.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/management/commands/__init__.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/management/commands/startcomponent.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/management/commands/upgradecomponent.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/middleware.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/safer_staticfiles/__init__.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/safer_staticfiles/apps.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components/templatetags/__init__.py +0 -0
- {django_components-0.50 → django_components-0.52}/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.50 → django_components-0.52}/django_components.egg-info/requires.txt +0 -0
- {django_components-0.50 → django_components-0.52}/django_components.egg-info/top_level.txt +0 -0
- {django_components-0.50 → django_components-0.52}/setup.cfg +0 -0
- {django_components-0.50 → django_components-0.52}/tests/components/__init__.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/components/single_file.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/components/urls.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/test_autodiscover.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/test_component.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/test_component_as_view.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/test_dependency_rendering.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/test_registry.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/test_settings.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/test_startcomponent_command.py +0 -0
- {django_components-0.50 → django_components-0.52}/tests/testutils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.52
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
5
|
Home-page: https://github.com/EmilStenstrom/django-components/
|
|
6
6
|
Author: Emil Stenström
|
|
@@ -49,7 +49,7 @@ Read on to learn about the details!
|
|
|
49
49
|
|
|
50
50
|
## Release notes
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
🚨📢 **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.
|
|
53
53
|
|
|
54
54
|
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.
|
|
55
55
|
|
|
@@ -139,7 +139,7 @@ Modify STATICFILES_DIRS (or add it if you don't have it) so django can find your
|
|
|
139
139
|
```python
|
|
140
140
|
STATICFILES_DIRS = [
|
|
141
141
|
...,
|
|
142
|
-
BASE_DIR
|
|
142
|
+
os.path.join(BASE_DIR, "components"),
|
|
143
143
|
]
|
|
144
144
|
```
|
|
145
145
|
|
|
@@ -284,6 +284,7 @@ Components can also be defined in a single file, which is useful for small compo
|
|
|
284
284
|
```python
|
|
285
285
|
# In a file called [project root]/components/calendar.py
|
|
286
286
|
from django_components import component
|
|
287
|
+
from django_components import types as t
|
|
287
288
|
|
|
288
289
|
@component.register("calendar")
|
|
289
290
|
class Calendar(component.Component):
|
|
@@ -292,16 +293,16 @@ class Calendar(component.Component):
|
|
|
292
293
|
"date": date,
|
|
293
294
|
}
|
|
294
295
|
|
|
295
|
-
template = """
|
|
296
|
+
template: t.django_html = """
|
|
296
297
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
|
297
298
|
"""
|
|
298
299
|
|
|
299
|
-
css = """
|
|
300
|
+
css: t.css = """
|
|
300
301
|
.calendar-component { width: 200px; background: pink; }
|
|
301
302
|
.calendar-component span { font-weight: bold; }
|
|
302
303
|
"""
|
|
303
304
|
|
|
304
|
-
js = """
|
|
305
|
+
js: t.js = """
|
|
305
306
|
(function(){
|
|
306
307
|
if (document.querySelector(".calendar-component")) {
|
|
307
308
|
document.querySelector(".calendar-component").onclick = function(){ alert("Clicked calendar!"); };
|
|
@@ -312,6 +313,8 @@ class Calendar(component.Component):
|
|
|
312
313
|
|
|
313
314
|
This makes it easy to create small components without having to create a separate template, CSS, and JS file.
|
|
314
315
|
|
|
316
|
+
Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS.
|
|
317
|
+
|
|
315
318
|
## Using slots in templates
|
|
316
319
|
|
|
317
320
|
_New in version 0.26_:
|
|
@@ -781,6 +784,12 @@ python manage.py startcomponent my_component --dry-run
|
|
|
781
784
|
|
|
782
785
|
This will simulate the creation of `my_component` without creating any files.
|
|
783
786
|
|
|
787
|
+
## Community examples
|
|
788
|
+
|
|
789
|
+
One of our goals with `django-components` is to make it easy to share components between projects. If you have a set of components that you think would be useful to others, please open a pull request to add them to the list below.
|
|
790
|
+
|
|
791
|
+
- [django-htmx-components](https://github.com/iwanalabs/django-htmx-components): A set of components for use with [htmx](https://htmx.org/). Try out the [live demo](https://dhc.iwanalabs.com/).
|
|
792
|
+
|
|
784
793
|
## Install locally and run the tests
|
|
785
794
|
|
|
786
795
|
Start by forking the project by clicking the **Fork button** up in the right corner in the GitHub . This makes a copy of the repository in your own name. Now you can clone this repository locally and start adding features:
|
|
@@ -20,7 +20,7 @@ Read on to learn about the details!
|
|
|
20
20
|
|
|
21
21
|
## Release notes
|
|
22
22
|
|
|
23
|
-
|
|
23
|
+
🚨📢 **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.
|
|
24
24
|
|
|
25
25
|
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.
|
|
26
26
|
|
|
@@ -110,7 +110,7 @@ Modify STATICFILES_DIRS (or add it if you don't have it) so django can find your
|
|
|
110
110
|
```python
|
|
111
111
|
STATICFILES_DIRS = [
|
|
112
112
|
...,
|
|
113
|
-
BASE_DIR
|
|
113
|
+
os.path.join(BASE_DIR, "components"),
|
|
114
114
|
]
|
|
115
115
|
```
|
|
116
116
|
|
|
@@ -255,6 +255,7 @@ Components can also be defined in a single file, which is useful for small compo
|
|
|
255
255
|
```python
|
|
256
256
|
# In a file called [project root]/components/calendar.py
|
|
257
257
|
from django_components import component
|
|
258
|
+
from django_components import types as t
|
|
258
259
|
|
|
259
260
|
@component.register("calendar")
|
|
260
261
|
class Calendar(component.Component):
|
|
@@ -263,16 +264,16 @@ class Calendar(component.Component):
|
|
|
263
264
|
"date": date,
|
|
264
265
|
}
|
|
265
266
|
|
|
266
|
-
template = """
|
|
267
|
+
template: t.django_html = """
|
|
267
268
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
|
268
269
|
"""
|
|
269
270
|
|
|
270
|
-
css = """
|
|
271
|
+
css: t.css = """
|
|
271
272
|
.calendar-component { width: 200px; background: pink; }
|
|
272
273
|
.calendar-component span { font-weight: bold; }
|
|
273
274
|
"""
|
|
274
275
|
|
|
275
|
-
js = """
|
|
276
|
+
js: t.js = """
|
|
276
277
|
(function(){
|
|
277
278
|
if (document.querySelector(".calendar-component")) {
|
|
278
279
|
document.querySelector(".calendar-component").onclick = function(){ alert("Clicked calendar!"); };
|
|
@@ -283,6 +284,8 @@ class Calendar(component.Component):
|
|
|
283
284
|
|
|
284
285
|
This makes it easy to create small components without having to create a separate template, CSS, and JS file.
|
|
285
286
|
|
|
287
|
+
Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS.
|
|
288
|
+
|
|
286
289
|
## Using slots in templates
|
|
287
290
|
|
|
288
291
|
_New in version 0.26_:
|
|
@@ -752,6 +755,12 @@ python manage.py startcomponent my_component --dry-run
|
|
|
752
755
|
|
|
753
756
|
This will simulate the creation of `my_component` without creating any files.
|
|
754
757
|
|
|
758
|
+
## Community examples
|
|
759
|
+
|
|
760
|
+
One of our goals with `django-components` is to make it easy to share components between projects. If you have a set of components that you think would be useful to others, please open a pull request to add them to the list below.
|
|
761
|
+
|
|
762
|
+
- [django-htmx-components](https://github.com/iwanalabs/django-htmx-components): A set of components for use with [htmx](https://htmx.org/). Try out the [live demo](https://dhc.iwanalabs.com/).
|
|
763
|
+
|
|
755
764
|
## Install locally and run the tests
|
|
756
765
|
|
|
757
766
|
Start by forking the project by clicking the **Fork button** up in the right corner in the GitHub . This makes a copy of the repository in your own name. Now you can clone this repository locally and start adding features:
|
|
@@ -37,6 +37,8 @@ def import_file(path):
|
|
|
37
37
|
MODULE_PATH = path
|
|
38
38
|
MODULE_NAME = Path(path).stem
|
|
39
39
|
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
|
|
40
|
+
if spec is None:
|
|
41
|
+
raise ValueError(f"Cannot import file '{path}' - invalid path")
|
|
40
42
|
module = importlib.util.module_from_spec(spec)
|
|
41
43
|
sys.modules[spec.name] = module
|
|
42
|
-
spec.loader.exec_module(module)
|
|
44
|
+
spec.loader.exec_module(module) # type: ignore
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import difflib
|
|
2
2
|
import inspect
|
|
3
3
|
from collections import ChainMap
|
|
4
|
-
from typing import Any, ClassVar, Dict, Iterable, Optional, Set, Tuple, Union
|
|
4
|
+
from typing import Any, ClassVar, Dict, Iterable, List, Optional, Set, Tuple, Union
|
|
5
5
|
|
|
6
6
|
from django.core.exceptions import ImproperlyConfigured
|
|
7
7
|
from django.forms.widgets import Media, MediaDefiningClass
|
|
@@ -40,7 +40,7 @@ from django_components.templatetags.component_tags import (
|
|
|
40
40
|
class SimplifiedInterfaceMediaDefiningClass(MediaDefiningClass):
|
|
41
41
|
def __new__(mcs, name, bases, attrs):
|
|
42
42
|
if "Media" in attrs:
|
|
43
|
-
media = attrs["Media"]
|
|
43
|
+
media: Component.Media = attrs["Media"]
|
|
44
44
|
|
|
45
45
|
# Allow: class Media: css = "style.css"
|
|
46
46
|
if hasattr(media, "css") and isinstance(media.css, str):
|
|
@@ -54,7 +54,7 @@ class SimplifiedInterfaceMediaDefiningClass(MediaDefiningClass):
|
|
|
54
54
|
if hasattr(media, "css") and isinstance(media.css, dict):
|
|
55
55
|
for media_type, path_list in media.css.items():
|
|
56
56
|
if isinstance(path_list, str):
|
|
57
|
-
media.css[media_type] = [path_list]
|
|
57
|
+
media.css[media_type] = [path_list] # type: ignore
|
|
58
58
|
|
|
59
59
|
# Allow: class Media: js = "script.js"
|
|
60
60
|
if hasattr(media, "js") and isinstance(media.js, str):
|
|
@@ -67,20 +67,20 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
67
67
|
# Either template_name or template must be set on subclass OR subclass must implement get_template() with
|
|
68
68
|
# non-null return.
|
|
69
69
|
template_name: ClassVar[Optional[str]] = None
|
|
70
|
-
template:
|
|
71
|
-
js:
|
|
72
|
-
css:
|
|
70
|
+
template: Optional[str] = None
|
|
71
|
+
js: Optional[str] = None
|
|
72
|
+
css: Optional[str] = None
|
|
73
73
|
media: Media
|
|
74
74
|
|
|
75
75
|
class Media:
|
|
76
|
-
css =
|
|
77
|
-
js
|
|
76
|
+
css: Optional[Union[str, List[str], Dict[str, str], Dict[str, List[str]]]] = None
|
|
77
|
+
js: Optional[Union[str, List[str]]] = None
|
|
78
78
|
|
|
79
79
|
def __init__(
|
|
80
80
|
self,
|
|
81
81
|
registered_name: Optional[str] = None,
|
|
82
82
|
outer_context: Optional[Context] = None,
|
|
83
|
-
fill_content: Union[DefaultFillContent, Iterable[NamedFillContent]] = (),
|
|
83
|
+
fill_content: Union[DefaultFillContent, Iterable[NamedFillContent]] = (), # type: ignore
|
|
84
84
|
):
|
|
85
85
|
self.registered_name: Optional[str] = registered_name
|
|
86
86
|
self.outer_context: Context = outer_context or Context()
|
|
@@ -142,7 +142,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
142
142
|
self,
|
|
143
143
|
context_data: Dict[str, Any],
|
|
144
144
|
slots_data: Optional[Dict[SlotName, str]] = None,
|
|
145
|
-
escape_slots_content:
|
|
145
|
+
escape_slots_content: bool = True,
|
|
146
146
|
) -> str:
|
|
147
147
|
context = Context(context_data)
|
|
148
148
|
template = self.get_template(context)
|
|
@@ -160,7 +160,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
160
160
|
self,
|
|
161
161
|
context_data: Dict[str, Any],
|
|
162
162
|
slots_data: Optional[Dict[SlotName, str]] = None,
|
|
163
|
-
escape_slots_content:
|
|
163
|
+
escape_slots_content: bool = True,
|
|
164
164
|
*args,
|
|
165
165
|
**kwargs,
|
|
166
166
|
):
|
|
@@ -173,7 +173,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
173
173
|
def _fill_slots(
|
|
174
174
|
self,
|
|
175
175
|
slots_data: Dict[SlotName, str],
|
|
176
|
-
escape_content: bool,
|
|
176
|
+
escape_content: bool = True,
|
|
177
177
|
):
|
|
178
178
|
"""Fill component slots outside of template rendering."""
|
|
179
179
|
self.fill_content = [
|
|
@@ -195,7 +195,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
195
195
|
named_fills_content = {}
|
|
196
196
|
else:
|
|
197
197
|
default_fill_content = None
|
|
198
|
-
named_fills_content = {name: (nodelist, alias) for name, nodelist, alias in self.fill_content}
|
|
198
|
+
named_fills_content = {name: (nodelist, alias) for name, nodelist, alias in list(self.fill_content)}
|
|
199
199
|
|
|
200
200
|
# If value is `None`, then slot is unfilled.
|
|
201
201
|
slot_name2fill_content: Dict[SlotName, Optional[FillContent]] = {}
|
|
@@ -15,7 +15,7 @@ class Loader(FilesystemLoader):
|
|
|
15
15
|
directories = set(get_app_template_dirs(component_dir))
|
|
16
16
|
|
|
17
17
|
if hasattr(settings, "BASE_DIR"):
|
|
18
|
-
path = (settings.BASE_DIR / component_dir).resolve()
|
|
18
|
+
path = (Path(settings.BASE_DIR) / component_dir).resolve()
|
|
19
19
|
if path.is_dir():
|
|
20
20
|
directories.add(path)
|
|
21
21
|
|
{django_components-0.50 → django_components-0.52}/django_components/templatetags/component_tags.py
RENAMED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import sys
|
|
2
|
-
from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple, Type, Union
|
|
2
|
+
from typing import TYPE_CHECKING, Iterable, List, Optional, Set, Tuple, Type, Union
|
|
3
3
|
|
|
4
4
|
if sys.version_info[:2] < (3, 9):
|
|
5
5
|
from typing import ChainMap
|
|
@@ -194,7 +194,7 @@ class SlotNode(Node, TemplateAwareNodeMixin):
|
|
|
194
194
|
|
|
195
195
|
extra_context = {}
|
|
196
196
|
try:
|
|
197
|
-
slot_fill_content:
|
|
197
|
+
slot_fill_content: FillContent = filled_slots_map[(self.name, self.template)]
|
|
198
198
|
except KeyError:
|
|
199
199
|
if self.is_required:
|
|
200
200
|
raise TemplateSyntaxError(
|
|
@@ -375,16 +375,17 @@ class ComponentNode(Node):
|
|
|
375
375
|
# Note that outer component context is used to resolve variables in
|
|
376
376
|
# fill tag.
|
|
377
377
|
resolved_name = fill_node.name_fexp.resolve(context)
|
|
378
|
+
resolved_alias: Optional[str]
|
|
378
379
|
if fill_node.alias_fexp:
|
|
379
|
-
resolved_alias
|
|
380
|
-
if not resolved_alias.isidentifier():
|
|
380
|
+
resolved_alias = fill_node.alias_fexp.resolve(context)
|
|
381
|
+
if resolved_alias and not resolved_alias.isidentifier():
|
|
381
382
|
raise TemplateSyntaxError(
|
|
382
383
|
f"Fill tag alias '{fill_node.alias_fexp.var}' in component "
|
|
383
384
|
f"{resolved_component_name} does not resolve to "
|
|
384
385
|
f"a valid Python identifier. Got: '{resolved_alias}'."
|
|
385
386
|
)
|
|
386
387
|
else:
|
|
387
|
-
resolved_alias
|
|
388
|
+
resolved_alias = None
|
|
388
389
|
fill_content.append((resolved_name, fill_node.nodelist, resolved_alias))
|
|
389
390
|
|
|
390
391
|
component: Component = component_cls(
|
|
@@ -426,14 +427,15 @@ def do_component(parser, token):
|
|
|
426
427
|
component_name, context_args, context_kwargs = parse_component_with_args(parser, bits, "component")
|
|
427
428
|
body: NodeList = parser.parse(parse_until=["endcomponent"])
|
|
428
429
|
parser.delete_first_token()
|
|
429
|
-
fill_nodes =
|
|
430
|
+
fill_nodes: Union[Iterable[NamedFillNode], ImplicitFillNode] = []
|
|
430
431
|
if block_has_content(body):
|
|
431
432
|
for parse_fn in (
|
|
432
433
|
try_parse_as_default_fill,
|
|
433
434
|
try_parse_as_named_fill_tag_set,
|
|
434
435
|
):
|
|
435
|
-
|
|
436
|
-
if
|
|
436
|
+
curr_fill_nodes = parse_fn(body)
|
|
437
|
+
if curr_fill_nodes:
|
|
438
|
+
fill_nodes = curr_fill_nodes
|
|
437
439
|
break
|
|
438
440
|
else:
|
|
439
441
|
raise TemplateSyntaxError(
|
|
@@ -457,7 +459,7 @@ def try_parse_as_named_fill_tag_set(
|
|
|
457
459
|
nodelist: NodeList,
|
|
458
460
|
) -> Optional[Iterable[NamedFillNode]]:
|
|
459
461
|
result = []
|
|
460
|
-
seen_name_fexps = set()
|
|
462
|
+
seen_name_fexps: Set[FilterExpression] = set()
|
|
461
463
|
for node in nodelist:
|
|
462
464
|
if isinstance(node, NamedFillNode):
|
|
463
465
|
if node.name_fexp in seen_name_fexps:
|
|
@@ -465,6 +467,7 @@ def try_parse_as_named_fill_tag_set(
|
|
|
465
467
|
f"Multiple fill tags cannot target the same slot name: "
|
|
466
468
|
f"Detected duplicate fill tag name '{node.name_fexp}'."
|
|
467
469
|
)
|
|
470
|
+
seen_name_fexps.add(node.name_fexp)
|
|
468
471
|
result.append(node)
|
|
469
472
|
elif isinstance(node, CommentNode):
|
|
470
473
|
pass
|
|
@@ -544,9 +547,13 @@ def do_if_filled_block(parser, token):
|
|
|
544
547
|
bits = token.split_contents()
|
|
545
548
|
starting_tag = bits[0]
|
|
546
549
|
slot_name, is_positive = parse_if_filled_bits(bits)
|
|
547
|
-
nodelist = parser.parse(("elif_filled", "else_filled", "endif_filled"))
|
|
550
|
+
nodelist: NodeList = parser.parse(("elif_filled", "else_filled", "endif_filled"))
|
|
548
551
|
branches: List[_IfSlotFilledBranchNode] = [
|
|
549
|
-
IfSlotFilledConditionBranchNode(
|
|
552
|
+
IfSlotFilledConditionBranchNode(
|
|
553
|
+
slot_name=slot_name, # type: ignore
|
|
554
|
+
nodelist=nodelist,
|
|
555
|
+
is_positive=is_positive,
|
|
556
|
+
)
|
|
550
557
|
]
|
|
551
558
|
|
|
552
559
|
token = parser.next_token()
|
|
@@ -555,9 +562,13 @@ def do_if_filled_block(parser, token):
|
|
|
555
562
|
while token.contents.startswith("elif_filled"):
|
|
556
563
|
bits = token.split_contents()
|
|
557
564
|
slot_name, is_positive = parse_if_filled_bits(bits)
|
|
558
|
-
nodelist
|
|
565
|
+
nodelist = parser.parse(("elif_filled", "else_filled", "endif_filled"))
|
|
559
566
|
branches.append(
|
|
560
|
-
IfSlotFilledConditionBranchNode(
|
|
567
|
+
IfSlotFilledConditionBranchNode(
|
|
568
|
+
slot_name=slot_name, # type: ignore
|
|
569
|
+
nodelist=nodelist,
|
|
570
|
+
is_positive=is_positive,
|
|
571
|
+
)
|
|
561
572
|
)
|
|
562
573
|
|
|
563
574
|
token = parser.next_token()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
try:
|
|
2
|
+
from typing import Annotated # type: ignore
|
|
3
|
+
except ImportError:
|
|
4
|
+
|
|
5
|
+
class Annotated: # type: ignore
|
|
6
|
+
def __init__(self, type_, *args, **kwargs):
|
|
7
|
+
self.type_ = type_
|
|
8
|
+
self.metadata = args, kwargs
|
|
9
|
+
|
|
10
|
+
def __repr__(self):
|
|
11
|
+
return f"Annotated[{self.type_}, {self.metadata[0]!r}, {self.metadata[1]!r}]"
|
|
12
|
+
|
|
13
|
+
def __getitem__(self, params):
|
|
14
|
+
if not isinstance(params, tuple):
|
|
15
|
+
params = (params,)
|
|
16
|
+
return Annotated(self.type_, *params, **self.metadata[1]) # type: ignore
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
css = Annotated[str, "css"]
|
|
20
|
+
django_html = Annotated[str, "django_html"]
|
|
21
|
+
js = Annotated[str, "js"]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: django_components
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.52
|
|
4
4
|
Summary: A way to create simple reusable template components in Django.
|
|
5
5
|
Home-page: https://github.com/EmilStenstrom/django-components/
|
|
6
6
|
Author: Emil Stenström
|
|
@@ -49,7 +49,7 @@ Read on to learn about the details!
|
|
|
49
49
|
|
|
50
50
|
## Release notes
|
|
51
51
|
|
|
52
|
-
|
|
52
|
+
🚨📢 **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.
|
|
53
53
|
|
|
54
54
|
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.
|
|
55
55
|
|
|
@@ -139,7 +139,7 @@ Modify STATICFILES_DIRS (or add it if you don't have it) so django can find your
|
|
|
139
139
|
```python
|
|
140
140
|
STATICFILES_DIRS = [
|
|
141
141
|
...,
|
|
142
|
-
BASE_DIR
|
|
142
|
+
os.path.join(BASE_DIR, "components"),
|
|
143
143
|
]
|
|
144
144
|
```
|
|
145
145
|
|
|
@@ -284,6 +284,7 @@ Components can also be defined in a single file, which is useful for small compo
|
|
|
284
284
|
```python
|
|
285
285
|
# In a file called [project root]/components/calendar.py
|
|
286
286
|
from django_components import component
|
|
287
|
+
from django_components import types as t
|
|
287
288
|
|
|
288
289
|
@component.register("calendar")
|
|
289
290
|
class Calendar(component.Component):
|
|
@@ -292,16 +293,16 @@ class Calendar(component.Component):
|
|
|
292
293
|
"date": date,
|
|
293
294
|
}
|
|
294
295
|
|
|
295
|
-
template = """
|
|
296
|
+
template: t.django_html = """
|
|
296
297
|
<div class="calendar-component">Today's date is <span>{{ date }}</span></div>
|
|
297
298
|
"""
|
|
298
299
|
|
|
299
|
-
css = """
|
|
300
|
+
css: t.css = """
|
|
300
301
|
.calendar-component { width: 200px; background: pink; }
|
|
301
302
|
.calendar-component span { font-weight: bold; }
|
|
302
303
|
"""
|
|
303
304
|
|
|
304
|
-
js = """
|
|
305
|
+
js: t.js = """
|
|
305
306
|
(function(){
|
|
306
307
|
if (document.querySelector(".calendar-component")) {
|
|
307
308
|
document.querySelector(".calendar-component").onclick = function(){ alert("Clicked calendar!"); };
|
|
@@ -312,6 +313,8 @@ class Calendar(component.Component):
|
|
|
312
313
|
|
|
313
314
|
This makes it easy to create small components without having to create a separate template, CSS, and JS file.
|
|
314
315
|
|
|
316
|
+
Note that the `t.django_html`, `t.css`, and `t.js` types are used to specify the type of the template, CSS, and JS files, respectively. This is not necessary, but if you're using VSCode with the [Python Inline Source Syntax Highlighting](https://marketplace.visualstudio.com/items?itemName=samwillis.python-inline-source) extension, it will give you syntax highlighting for the template, CSS, and JS.
|
|
317
|
+
|
|
315
318
|
## Using slots in templates
|
|
316
319
|
|
|
317
320
|
_New in version 0.26_:
|
|
@@ -781,6 +784,12 @@ python manage.py startcomponent my_component --dry-run
|
|
|
781
784
|
|
|
782
785
|
This will simulate the creation of `my_component` without creating any files.
|
|
783
786
|
|
|
787
|
+
## Community examples
|
|
788
|
+
|
|
789
|
+
One of our goals with `django-components` is to make it easy to share components between projects. If you have a set of components that you think would be useful to others, please open a pull request to add them to the list below.
|
|
790
|
+
|
|
791
|
+
- [django-htmx-components](https://github.com/iwanalabs/django-htmx-components): A set of components for use with [htmx](https://htmx.org/). Try out the [live demo](https://dhc.iwanalabs.com/).
|
|
792
|
+
|
|
784
793
|
## Install locally and run the tests
|
|
785
794
|
|
|
786
795
|
Start by forking the project by clicking the **Fork button** up in the right corner in the GitHub . This makes a copy of the repository in your own name. Now you can clone this repository locally and start adding features:
|
|
@@ -9,6 +9,7 @@ django_components/component.py
|
|
|
9
9
|
django_components/component_registry.py
|
|
10
10
|
django_components/middleware.py
|
|
11
11
|
django_components/template_loader.py
|
|
12
|
+
django_components/types.py
|
|
12
13
|
django_components.egg-info/PKG-INFO
|
|
13
14
|
django_components.egg-info/SOURCES.txt
|
|
14
15
|
django_components.egg-info/dependency_links.txt
|
|
@@ -3,11 +3,14 @@ import os
|
|
|
3
3
|
|
|
4
4
|
from setuptools import find_packages, setup
|
|
5
5
|
|
|
6
|
-
VERSION = "0.
|
|
6
|
+
VERSION = "0.52"
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name="django_components",
|
|
10
10
|
packages=find_packages(exclude=["tests"]),
|
|
11
|
+
package_data={
|
|
12
|
+
"django_components": ["py.typed"],
|
|
13
|
+
},
|
|
11
14
|
version=VERSION,
|
|
12
15
|
description="A way to create simple reusable template components in Django.",
|
|
13
16
|
long_description=open(os.path.join(os.path.dirname(__file__), "README.md"), encoding="utf8").read(),
|
|
@@ -48,6 +48,10 @@ class VariableDisplay(component.Component):
|
|
|
48
48
|
class IncrementerComponent(component.Component):
|
|
49
49
|
template_name = "incrementer.html"
|
|
50
50
|
|
|
51
|
+
def __init__(self, *args, **kwargs):
|
|
52
|
+
super().__init__(*args, **kwargs)
|
|
53
|
+
self.call_count = 0
|
|
54
|
+
|
|
51
55
|
def get_context_data(self, value=0):
|
|
52
56
|
value = int(value)
|
|
53
57
|
if hasattr(self, "call_count"):
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import re
|
|
2
2
|
import textwrap
|
|
3
|
-
from typing import Callable,
|
|
3
|
+
from typing import Callable, Optional
|
|
4
4
|
|
|
5
5
|
from django.template import Context, Template, TemplateSyntaxError
|
|
6
6
|
|
|
@@ -1250,7 +1250,7 @@ class IterationFillTest(SimpleTestCase):
|
|
|
1250
1250
|
class ComponentSimpleSlotInALoop(django_components.component.Component):
|
|
1251
1251
|
template_name = "template_with_slot_in_a_loop.html"
|
|
1252
1252
|
|
|
1253
|
-
def get_context_data(self, objects
|
|
1253
|
+
def get_context_data(self, objects, *args, **kwargs) -> dict:
|
|
1254
1254
|
return {
|
|
1255
1255
|
"objects": objects,
|
|
1256
1256
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.50 → django_components-0.52}/django_components/management/commands/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.50 → django_components-0.52}/django_components/safer_staticfiles/__init__.py
RENAMED
|
File without changes
|
{django_components-0.50 → django_components-0.52}/django_components/safer_staticfiles/apps.py
RENAMED
|
File without changes
|
{django_components-0.50 → django_components-0.52}/django_components/templatetags/__init__.py
RENAMED
|
File without changes
|
{django_components-0.50 → django_components-0.52}/django_components.egg-info/dependency_links.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
|