django-components 0.60__tar.gz → 0.62__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.60 → django_components-0.62}/PKG-INFO +1 -1
- {django_components-0.60 → django_components-0.62}/pyproject.toml +14 -2
- {django_components-0.60 → django_components-0.62}/setup.py +3 -2
- {django_components-0.60 → django_components-0.62/src}/django_components/__init__.py +6 -5
- {django_components-0.60 → django_components-0.62/src}/django_components/app_settings.py +7 -6
- {django_components-0.60 → django_components-0.62/src}/django_components/apps.py +1 -1
- {django_components-0.60 → django_components-0.62/src}/django_components/component.py +40 -20
- {django_components-0.60 → django_components-0.62/src}/django_components/component_registry.py +19 -11
- {django_components-0.60 → django_components-0.62/src}/django_components/management/commands/startcomponent.py +4 -3
- {django_components-0.60 → django_components-0.62/src}/django_components/management/commands/upgradecomponent.py +4 -3
- {django_components-0.60 → django_components-0.62/src}/django_components/middleware.py +14 -8
- {django_components-0.60 → django_components-0.62/src}/django_components/template_loader.py +3 -3
- {django_components-0.60 → django_components-0.62/src}/django_components/templatetags/component_tags.py +48 -46
- {django_components-0.60 → django_components-0.62/src}/django_components/types.py +7 -3
- django_components-0.62/src/django_components/utils.py +32 -0
- {django_components-0.60 → django_components-0.62/src}/django_components.egg-info/PKG-INFO +1 -1
- django_components-0.62/src/django_components.egg-info/SOURCES.txt +38 -0
- {django_components-0.60 → django_components-0.62/src}/django_components.egg-info/top_level.txt +0 -1
- django_components-0.60/django_components.egg-info/SOURCES.txt +0 -39
- django_components-0.60/tests/components/single_file.py +0 -26
- django_components-0.60/tests/components/urls.py +0 -9
- {django_components-0.60 → django_components-0.62}/LICENSE +0 -0
- {django_components-0.60 → django_components-0.62}/README.md +0 -0
- {django_components-0.60 → django_components-0.62}/setup.cfg +0 -0
- {django_components-0.60 → django_components-0.62/src}/django_components/logger.py +0 -0
- {django_components-0.60 → django_components-0.62/src}/django_components/management/__init__.py +0 -0
- {django_components-0.60 → django_components-0.62/src}/django_components/management/commands/__init__.py +0 -0
- /django_components-0.60/django_components/safer_staticfiles/__init__.py → /django_components-0.62/src/django_components/py.typed +0 -0
- {django_components-0.60/django_components/templatetags → django_components-0.62/src/django_components/safer_staticfiles}/__init__.py +0 -0
- {django_components-0.60 → django_components-0.62/src}/django_components/safer_staticfiles/apps.py +0 -0
- {django_components-0.60/tests/components → django_components-0.62/src/django_components/templatetags}/__init__.py +0 -0
- {django_components-0.60 → django_components-0.62/src}/django_components.egg-info/dependency_links.txt +0 -0
- {django_components-0.60 → django_components-0.62/src}/django_components.egg-info/requires.txt +0 -0
- {django_components-0.60 → django_components-0.62}/tests/test_autodiscover.py +0 -0
- {django_components-0.60 → django_components-0.62}/tests/test_component.py +0 -0
- {django_components-0.60 → django_components-0.62}/tests/test_component_as_view.py +0 -0
- {django_components-0.60 → django_components-0.62}/tests/test_context.py +0 -0
- {django_components-0.60 → django_components-0.62}/tests/test_dependency_rendering.py +0 -0
- {django_components-0.60 → django_components-0.62}/tests/test_registry.py +0 -0
- {django_components-0.60 → django_components-0.62}/tests/test_settings.py +0 -0
- {django_components-0.60 → django_components-0.62}/tests/test_startcomponent_command.py +0 -0
- {django_components-0.60 → django_components-0.62}/tests/test_templatetags.py +0 -0
- {django_components-0.60 → django_components-0.62}/tests/testutils.py +0 -0
|
@@ -35,11 +35,23 @@ exclude = [
|
|
|
35
35
|
'.env',
|
|
36
36
|
'.venv',
|
|
37
37
|
'.tox',
|
|
38
|
+
'build',
|
|
38
39
|
]
|
|
39
40
|
|
|
40
41
|
[tool.mypy]
|
|
41
42
|
check_untyped_defs = true
|
|
42
43
|
ignore_missing_imports = true
|
|
43
44
|
exclude = [
|
|
44
|
-
'test_structures'
|
|
45
|
-
|
|
45
|
+
'test_structures',
|
|
46
|
+
'build',
|
|
47
|
+
]
|
|
48
|
+
|
|
49
|
+
[[tool.mypy.overrides]]
|
|
50
|
+
module = "django_components.*"
|
|
51
|
+
disallow_untyped_defs = true
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
testpaths = [
|
|
56
|
+
"tests"
|
|
57
|
+
]
|
|
@@ -3,11 +3,12 @@ import os
|
|
|
3
3
|
|
|
4
4
|
from setuptools import find_packages, setup
|
|
5
5
|
|
|
6
|
-
VERSION = "0.
|
|
6
|
+
VERSION = "0.62"
|
|
7
7
|
|
|
8
8
|
setup(
|
|
9
9
|
name="django_components",
|
|
10
|
-
packages=find_packages(exclude=["tests"]),
|
|
10
|
+
packages=find_packages(where="src", exclude=["tests", "tests.*"]),
|
|
11
|
+
package_dir={"": "src"},
|
|
11
12
|
package_data={
|
|
12
13
|
"django_components": ["py.typed"],
|
|
13
14
|
},
|
|
@@ -2,17 +2,18 @@ import importlib
|
|
|
2
2
|
import importlib.util
|
|
3
3
|
import sys
|
|
4
4
|
from pathlib import Path
|
|
5
|
+
from typing import Union
|
|
5
6
|
|
|
6
7
|
import django
|
|
7
8
|
from django.utils.module_loading import autodiscover_modules
|
|
8
9
|
|
|
9
|
-
from django_components.utils
|
|
10
|
+
from django_components.utils import search
|
|
10
11
|
|
|
11
12
|
if django.VERSION < (3, 2):
|
|
12
13
|
default_app_config = "django_components.apps.ComponentsConfig"
|
|
13
14
|
|
|
14
15
|
|
|
15
|
-
def autodiscover():
|
|
16
|
+
def autodiscover() -> None:
|
|
16
17
|
from django_components.app_settings import app_settings
|
|
17
18
|
|
|
18
19
|
if app_settings.AUTODISCOVER:
|
|
@@ -24,11 +25,11 @@ def autodiscover():
|
|
|
24
25
|
for path in component_filepaths:
|
|
25
26
|
import_file(path)
|
|
26
27
|
|
|
27
|
-
for
|
|
28
|
-
importlib.import_module(
|
|
28
|
+
for path_lib in app_settings.LIBRARIES:
|
|
29
|
+
importlib.import_module(path_lib)
|
|
29
30
|
|
|
30
31
|
|
|
31
|
-
def import_file(path):
|
|
32
|
+
def import_file(path: Union[str, Path]) -> None:
|
|
32
33
|
MODULE_PATH = path
|
|
33
34
|
MODULE_NAME = Path(path).stem
|
|
34
35
|
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from enum import Enum
|
|
2
|
+
from typing import List
|
|
2
3
|
|
|
3
4
|
from django.conf import settings
|
|
4
5
|
|
|
@@ -9,27 +10,27 @@ class ContextBehavior(Enum):
|
|
|
9
10
|
|
|
10
11
|
|
|
11
12
|
class AppSettings:
|
|
12
|
-
def __init__(self):
|
|
13
|
+
def __init__(self) -> None:
|
|
13
14
|
self.settings = getattr(settings, "COMPONENTS", {})
|
|
14
15
|
|
|
15
16
|
@property
|
|
16
|
-
def AUTODISCOVER(self):
|
|
17
|
+
def AUTODISCOVER(self) -> bool:
|
|
17
18
|
return self.settings.setdefault("autodiscover", True)
|
|
18
19
|
|
|
19
20
|
@property
|
|
20
|
-
def LIBRARIES(self):
|
|
21
|
+
def LIBRARIES(self) -> List:
|
|
21
22
|
return self.settings.setdefault("libraries", [])
|
|
22
23
|
|
|
23
24
|
@property
|
|
24
|
-
def TEMPLATE_CACHE_SIZE(self):
|
|
25
|
+
def TEMPLATE_CACHE_SIZE(self) -> int:
|
|
25
26
|
return self.settings.setdefault("template_cache_size", 128)
|
|
26
27
|
|
|
27
28
|
@property
|
|
28
|
-
def CONTEXT_BEHAVIOR(self):
|
|
29
|
+
def CONTEXT_BEHAVIOR(self) -> ContextBehavior:
|
|
29
30
|
raw_value = self.settings.setdefault("context_behavior", ContextBehavior.GLOBAL.value)
|
|
30
31
|
return self._validate_context_behavior(raw_value)
|
|
31
32
|
|
|
32
|
-
def _validate_context_behavior(self, raw_value):
|
|
33
|
+
def _validate_context_behavior(self, raw_value: ContextBehavior) -> ContextBehavior:
|
|
33
34
|
try:
|
|
34
35
|
return ContextBehavior(raw_value)
|
|
35
36
|
except ValueError:
|
|
@@ -2,7 +2,22 @@ import difflib
|
|
|
2
2
|
import inspect
|
|
3
3
|
import os
|
|
4
4
|
from collections import ChainMap
|
|
5
|
-
from
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import (
|
|
7
|
+
Any,
|
|
8
|
+
ClassVar,
|
|
9
|
+
Dict,
|
|
10
|
+
Iterable,
|
|
11
|
+
List,
|
|
12
|
+
Mapping,
|
|
13
|
+
MutableMapping,
|
|
14
|
+
Optional,
|
|
15
|
+
Set,
|
|
16
|
+
Tuple,
|
|
17
|
+
Type,
|
|
18
|
+
TypeVar,
|
|
19
|
+
Union,
|
|
20
|
+
)
|
|
6
21
|
|
|
7
22
|
from django.core.exceptions import ImproperlyConfigured
|
|
8
23
|
from django.forms.widgets import Media, MediaDefiningClass
|
|
@@ -12,7 +27,7 @@ from django.template.context import Context
|
|
|
12
27
|
from django.template.exceptions import TemplateSyntaxError
|
|
13
28
|
from django.template.loader import get_template
|
|
14
29
|
from django.utils.html import escape
|
|
15
|
-
from django.utils.safestring import mark_safe
|
|
30
|
+
from django.utils.safestring import SafeString, mark_safe
|
|
16
31
|
from django.views import View
|
|
17
32
|
|
|
18
33
|
# Global registry var and register() function moved to separate module.
|
|
@@ -37,11 +52,13 @@ from django_components.templatetags.component_tags import (
|
|
|
37
52
|
SlotName,
|
|
38
53
|
SlotNode,
|
|
39
54
|
)
|
|
40
|
-
from django_components.utils
|
|
55
|
+
from django_components.utils import search
|
|
56
|
+
|
|
57
|
+
_T = TypeVar("_T")
|
|
41
58
|
|
|
42
59
|
|
|
43
60
|
class SimplifiedInterfaceMediaDefiningClass(MediaDefiningClass):
|
|
44
|
-
def __new__(mcs, name, bases, attrs):
|
|
61
|
+
def __new__(mcs, name: str, bases: Tuple[Type, ...], attrs: Dict[str, Any]) -> Type:
|
|
45
62
|
if "Media" in attrs:
|
|
46
63
|
media: Component.Media = attrs["Media"]
|
|
47
64
|
|
|
@@ -68,7 +85,7 @@ class SimplifiedInterfaceMediaDefiningClass(MediaDefiningClass):
|
|
|
68
85
|
return super().__new__(mcs, name, bases, attrs)
|
|
69
86
|
|
|
70
87
|
|
|
71
|
-
def _resolve_component_relative_files(attrs:
|
|
88
|
+
def _resolve_component_relative_files(attrs: MutableMapping) -> None:
|
|
72
89
|
"""
|
|
73
90
|
Check if component's HTML, JS and CSS files refer to files in the same directory
|
|
74
91
|
as the component class. If so, modify the attributes so the class Django's rendering
|
|
@@ -96,7 +113,7 @@ def _resolve_component_relative_files(attrs: dict):
|
|
|
96
113
|
# Check if filepath refers to a file that's in the same directory as the component class.
|
|
97
114
|
# If yes, modify the path to refer to the relative file.
|
|
98
115
|
# If not, don't modify anything.
|
|
99
|
-
def resolve_file(filepath: str):
|
|
116
|
+
def resolve_file(filepath: str) -> str:
|
|
100
117
|
maybe_resolved_filepath = os.path.join(comp_dir_abs, filepath)
|
|
101
118
|
component_import_filepath = os.path.join(comp_dir_rel, filepath)
|
|
102
119
|
|
|
@@ -128,7 +145,9 @@ def _resolve_component_relative_files(attrs: dict):
|
|
|
128
145
|
media.js = [resolve_file(filepath) for filepath in media.js]
|
|
129
146
|
|
|
130
147
|
|
|
131
|
-
def _get_dir_path_from_component_module_path(
|
|
148
|
+
def _get_dir_path_from_component_module_path(
|
|
149
|
+
component_module_path: str, candidate_dirs: Union[List[str], List[Path]]
|
|
150
|
+
) -> Tuple[str, str]:
|
|
132
151
|
# Transform python module notation "pkg.module.name" to file path "pkg/module/name"
|
|
133
152
|
# Thus, we should get file path relative to Django project root
|
|
134
153
|
comp_path = os.sep.join(component_module_path.split("."))
|
|
@@ -164,6 +183,7 @@ def _get_dir_path_from_component_module_path(component_module_path: str, candida
|
|
|
164
183
|
class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
165
184
|
# Either template_name or template must be set on subclass OR subclass must implement get_template() with
|
|
166
185
|
# non-null return.
|
|
186
|
+
class_hash: ClassVar[int]
|
|
167
187
|
template_name: ClassVar[Optional[str]] = None
|
|
168
188
|
template: Optional[str] = None
|
|
169
189
|
js: Optional[str] = None
|
|
@@ -184,19 +204,19 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
184
204
|
self.outer_context: Context = outer_context or Context()
|
|
185
205
|
self.fill_content = fill_content
|
|
186
206
|
|
|
187
|
-
def __init_subclass__(cls, **kwargs):
|
|
207
|
+
def __init_subclass__(cls, **kwargs: Any) -> None:
|
|
188
208
|
cls.class_hash = hash(inspect.getfile(cls) + cls.__name__)
|
|
189
209
|
|
|
190
|
-
def get_context_data(self, *args, **kwargs) -> Dict[str, Any]:
|
|
210
|
+
def get_context_data(self, *args: Any, **kwargs: Any) -> Dict[str, Any]:
|
|
191
211
|
return {}
|
|
192
212
|
|
|
193
|
-
def get_template_name(self, context) -> Optional[str]:
|
|
213
|
+
def get_template_name(self, context: Mapping) -> Optional[str]:
|
|
194
214
|
return self.template_name
|
|
195
215
|
|
|
196
|
-
def get_template_string(self, context) -> Optional[str]:
|
|
216
|
+
def get_template_string(self, context: Mapping) -> Optional[str]:
|
|
197
217
|
return self.template
|
|
198
218
|
|
|
199
|
-
def render_dependencies(self):
|
|
219
|
+
def render_dependencies(self) -> SafeString:
|
|
200
220
|
"""Helper function to render all dependencies for a component."""
|
|
201
221
|
dependencies = []
|
|
202
222
|
|
|
@@ -210,19 +230,19 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
210
230
|
|
|
211
231
|
return mark_safe("\n".join(dependencies))
|
|
212
232
|
|
|
213
|
-
def render_css_dependencies(self):
|
|
233
|
+
def render_css_dependencies(self) -> SafeString:
|
|
214
234
|
"""Render only CSS dependencies available in the media class or provided as a string."""
|
|
215
235
|
if self.css is not None:
|
|
216
236
|
return mark_safe(f"<style>{self.css}</style>")
|
|
217
237
|
return mark_safe("\n".join(self.media.render_css()))
|
|
218
238
|
|
|
219
|
-
def render_js_dependencies(self):
|
|
239
|
+
def render_js_dependencies(self) -> SafeString:
|
|
220
240
|
"""Render only JS dependencies available in the media class or provided as a string."""
|
|
221
241
|
if self.js is not None:
|
|
222
242
|
return mark_safe(f"<script>{self.js}</script>")
|
|
223
243
|
return mark_safe("\n".join(self.media.render_js()))
|
|
224
244
|
|
|
225
|
-
def get_template(self, context) -> Template:
|
|
245
|
+
def get_template(self, context: Mapping) -> Template:
|
|
226
246
|
template_string = self.get_template_string(context)
|
|
227
247
|
if template_string is not None:
|
|
228
248
|
return Template(template_string)
|
|
@@ -259,9 +279,9 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
259
279
|
context_data: Dict[str, Any],
|
|
260
280
|
slots_data: Optional[Dict[SlotName, str]] = None,
|
|
261
281
|
escape_slots_content: bool = True,
|
|
262
|
-
*args,
|
|
263
|
-
**kwargs,
|
|
264
|
-
):
|
|
282
|
+
*args: Any,
|
|
283
|
+
**kwargs: Any,
|
|
284
|
+
) -> HttpResponse:
|
|
265
285
|
return HttpResponse(
|
|
266
286
|
self.render(context_data, slots_data, escape_slots_content),
|
|
267
287
|
*args,
|
|
@@ -272,7 +292,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
272
292
|
self,
|
|
273
293
|
slots_data: Dict[SlotName, str],
|
|
274
294
|
escape_content: bool = True,
|
|
275
|
-
):
|
|
295
|
+
) -> None:
|
|
276
296
|
"""Fill component slots outside of template rendering."""
|
|
277
297
|
self.fill_content = [
|
|
278
298
|
(
|
|
@@ -341,7 +361,7 @@ class Component(View, metaclass=SimplifiedInterfaceMediaDefiningClass):
|
|
|
341
361
|
f"even though none of its slots is marked as 'default'."
|
|
342
362
|
)
|
|
343
363
|
|
|
344
|
-
unfilled_slots: Set[str] =
|
|
364
|
+
unfilled_slots: Set[str] = {k for k, v in slot_name2fill_content.items() if v is None}
|
|
345
365
|
unmatched_fills: Set[str] = named_fills_content.keys() - slot_name2fill_content.keys()
|
|
346
366
|
|
|
347
367
|
# Check that 'required' slots are filled.
|
{django_components-0.60 → django_components-0.62/src}/django_components/component_registry.py
RENAMED
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
from typing import TYPE_CHECKING, Callable, Dict, Type, TypeVar
|
|
2
|
+
|
|
3
|
+
if TYPE_CHECKING:
|
|
4
|
+
from django_components import component
|
|
5
|
+
|
|
6
|
+
_TC = TypeVar("_TC", bound=Type["component.Component"])
|
|
7
|
+
|
|
8
|
+
|
|
1
9
|
class AlreadyRegistered(Exception):
|
|
2
10
|
pass
|
|
3
11
|
|
|
@@ -6,39 +14,39 @@ class NotRegistered(Exception):
|
|
|
6
14
|
pass
|
|
7
15
|
|
|
8
16
|
|
|
9
|
-
class ComponentRegistry
|
|
10
|
-
def __init__(self):
|
|
11
|
-
self._registry = {} # component name -> component_class mapping
|
|
17
|
+
class ComponentRegistry:
|
|
18
|
+
def __init__(self) -> None:
|
|
19
|
+
self._registry: Dict[str, Type["component.Component"]] = {} # component name -> component_class mapping
|
|
12
20
|
|
|
13
|
-
def register(self, name
|
|
21
|
+
def register(self, name: str, component: Type["component.Component"]) -> None:
|
|
14
22
|
existing_component = self._registry.get(name)
|
|
15
23
|
if existing_component and existing_component.class_hash != component.class_hash:
|
|
16
24
|
raise AlreadyRegistered('The component "%s" has already been registered' % name)
|
|
17
25
|
self._registry[name] = component
|
|
18
26
|
|
|
19
|
-
def unregister(self, name):
|
|
27
|
+
def unregister(self, name: str) -> None:
|
|
20
28
|
self.get(name)
|
|
21
29
|
|
|
22
30
|
del self._registry[name]
|
|
23
31
|
|
|
24
|
-
def get(self, name):
|
|
32
|
+
def get(self, name: str) -> Type["component.Component"]:
|
|
25
33
|
if name not in self._registry:
|
|
26
34
|
raise NotRegistered('The component "%s" is not registered' % name)
|
|
27
35
|
|
|
28
36
|
return self._registry[name]
|
|
29
37
|
|
|
30
|
-
def all(self):
|
|
38
|
+
def all(self) -> Dict[str, Type["component.Component"]]:
|
|
31
39
|
return self._registry
|
|
32
40
|
|
|
33
|
-
def clear(self):
|
|
41
|
+
def clear(self) -> None:
|
|
34
42
|
self._registry = {}
|
|
35
43
|
|
|
36
44
|
|
|
37
45
|
# This variable represents the global component registry
|
|
38
|
-
registry = ComponentRegistry()
|
|
46
|
+
registry: ComponentRegistry = ComponentRegistry()
|
|
39
47
|
|
|
40
48
|
|
|
41
|
-
def register(name):
|
|
49
|
+
def register(name: str) -> Callable[[_TC], _TC]:
|
|
42
50
|
"""Class decorator to register a component.
|
|
43
51
|
|
|
44
52
|
Usage:
|
|
@@ -48,7 +56,7 @@ def register(name):
|
|
|
48
56
|
...
|
|
49
57
|
"""
|
|
50
58
|
|
|
51
|
-
def decorator(component):
|
|
59
|
+
def decorator(component: _TC) -> _TC:
|
|
52
60
|
registry.register(name=name, component=component)
|
|
53
61
|
return component
|
|
54
62
|
|
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
import os
|
|
2
2
|
from textwrap import dedent
|
|
3
|
+
from typing import Any
|
|
3
4
|
|
|
4
5
|
from django.conf import settings
|
|
5
|
-
from django.core.management.base import BaseCommand, CommandError
|
|
6
|
+
from django.core.management.base import BaseCommand, CommandError, CommandParser
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
class Command(BaseCommand):
|
|
9
10
|
help = "Creates a new component"
|
|
10
11
|
|
|
11
|
-
def add_arguments(self, parser):
|
|
12
|
+
def add_arguments(self, parser: CommandParser) -> None:
|
|
12
13
|
parser.add_argument("name", type=str, help="The name of the component to create")
|
|
13
14
|
parser.add_argument(
|
|
14
15
|
"--path",
|
|
@@ -51,7 +52,7 @@ class Command(BaseCommand):
|
|
|
51
52
|
default=False,
|
|
52
53
|
)
|
|
53
54
|
|
|
54
|
-
def handle(self, *args, **kwargs):
|
|
55
|
+
def handle(self, *args: Any, **kwargs: Any) -> None:
|
|
55
56
|
name = kwargs["name"]
|
|
56
57
|
|
|
57
58
|
if name:
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import os
|
|
2
2
|
import re
|
|
3
3
|
from pathlib import Path
|
|
4
|
+
from typing import Any
|
|
4
5
|
|
|
5
6
|
from django.conf import settings
|
|
6
|
-
from django.core.management.base import BaseCommand
|
|
7
|
+
from django.core.management.base import BaseCommand, CommandParser
|
|
7
8
|
from django.template.engine import Engine
|
|
8
9
|
|
|
9
10
|
from django_components.template_loader import Loader
|
|
@@ -12,10 +13,10 @@ from django_components.template_loader import Loader
|
|
|
12
13
|
class Command(BaseCommand):
|
|
13
14
|
help = "Updates component and component_block tags to the new syntax"
|
|
14
15
|
|
|
15
|
-
def add_arguments(self, parser):
|
|
16
|
+
def add_arguments(self, parser: CommandParser) -> None:
|
|
16
17
|
parser.add_argument("--path", type=str, help="Path to search for components")
|
|
17
18
|
|
|
18
|
-
def handle(self, *args, **options):
|
|
19
|
+
def handle(self, *args: Any, **options: Any) -> None:
|
|
19
20
|
current_engine = Engine.get_default()
|
|
20
21
|
loader = Loader(current_engine)
|
|
21
22
|
dirs = loader.get_dirs()
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import re
|
|
2
|
+
from collections.abc import Callable
|
|
3
|
+
from typing import TYPE_CHECKING, Iterable
|
|
2
4
|
|
|
3
5
|
from django.conf import settings
|
|
4
6
|
from django.forms import Media
|
|
5
|
-
from django.http import StreamingHttpResponse
|
|
7
|
+
from django.http import HttpRequest, HttpResponse, StreamingHttpResponse
|
|
8
|
+
from django.http.response import HttpResponseBase
|
|
6
9
|
|
|
7
10
|
from django_components.component_registry import registry
|
|
8
11
|
|
|
12
|
+
if TYPE_CHECKING:
|
|
13
|
+
from django_components.component import Component
|
|
14
|
+
|
|
9
15
|
RENDERED_COMPONENTS_CONTEXT_KEY = "_COMPONENT_DEPENDENCIES"
|
|
10
16
|
CSS_DEPENDENCY_PLACEHOLDER = '<link name="CSS_PLACEHOLDER">'
|
|
11
17
|
JS_DEPENDENCY_PLACEHOLDER = '<script name="JS_PLACEHOLDER"></script>'
|
|
@@ -24,10 +30,10 @@ class ComponentDependencyMiddleware:
|
|
|
24
30
|
|
|
25
31
|
dependency_regex = COMPONENT_COMMENT_REGEX
|
|
26
32
|
|
|
27
|
-
def __init__(self, get_response):
|
|
33
|
+
def __init__(self, get_response: "Callable[[HttpRequest], HttpResponse]") -> None:
|
|
28
34
|
self.get_response = get_response
|
|
29
35
|
|
|
30
|
-
def __call__(self, request):
|
|
36
|
+
def __call__(self, request: HttpRequest) -> HttpResponseBase:
|
|
31
37
|
response = self.get_response(request)
|
|
32
38
|
if (
|
|
33
39
|
getattr(settings, "COMPONENTS", {}).get("RENDER_DEPENDENCIES", False)
|
|
@@ -38,7 +44,7 @@ class ComponentDependencyMiddleware:
|
|
|
38
44
|
return response
|
|
39
45
|
|
|
40
46
|
|
|
41
|
-
def process_response_content(content):
|
|
47
|
+
def process_response_content(content: bytes) -> bytes:
|
|
42
48
|
component_names_seen = {match.group("name") for match in COMPONENT_COMMENT_REGEX.finditer(content)}
|
|
43
49
|
all_components = [registry.get(name.decode("utf-8"))("") for name in component_names_seen]
|
|
44
50
|
all_media = join_media(all_components)
|
|
@@ -47,7 +53,7 @@ def process_response_content(content):
|
|
|
47
53
|
return PLACEHOLDER_REGEX.sub(DependencyReplacer(css_dependencies, js_dependencies), content)
|
|
48
54
|
|
|
49
55
|
|
|
50
|
-
def add_module_attribute_to_scripts(scripts):
|
|
56
|
+
def add_module_attribute_to_scripts(scripts: str) -> str:
|
|
51
57
|
return re.sub(SCRIPT_TAG_REGEX, '<script type="module"', scripts)
|
|
52
58
|
|
|
53
59
|
|
|
@@ -58,11 +64,11 @@ class DependencyReplacer:
|
|
|
58
64
|
CSS_PLACEHOLDER = bytes(CSS_DEPENDENCY_PLACEHOLDER, encoding="utf-8")
|
|
59
65
|
JS_PLACEHOLDER = bytes(JS_DEPENDENCY_PLACEHOLDER, encoding="utf-8")
|
|
60
66
|
|
|
61
|
-
def __init__(self, css_string, js_string):
|
|
67
|
+
def __init__(self, css_string: bytes, js_string: bytes) -> None:
|
|
62
68
|
self.js_string = js_string
|
|
63
69
|
self.css_string = css_string
|
|
64
70
|
|
|
65
|
-
def __call__(self, match):
|
|
71
|
+
def __call__(self, match: "re.Match[bytes]") -> bytes:
|
|
66
72
|
if match[0] == self.CSS_PLACEHOLDER:
|
|
67
73
|
replacement, self.css_string = self.css_string, b""
|
|
68
74
|
elif match[0] == self.JS_PLACEHOLDER:
|
|
@@ -72,7 +78,7 @@ class DependencyReplacer:
|
|
|
72
78
|
return replacement
|
|
73
79
|
|
|
74
80
|
|
|
75
|
-
def join_media(components):
|
|
81
|
+
def join_media(components: Iterable["Component"]) -> Media:
|
|
76
82
|
"""Return combined media object for iterable of components."""
|
|
77
83
|
|
|
78
84
|
return sum([component.media for component in components], Media())
|
|
@@ -4,7 +4,7 @@ Template loader that loads templates from each Django app's "components" directo
|
|
|
4
4
|
|
|
5
5
|
import os
|
|
6
6
|
from pathlib import Path
|
|
7
|
-
from typing import Set
|
|
7
|
+
from typing import List, Set
|
|
8
8
|
|
|
9
9
|
from django.apps import apps
|
|
10
10
|
from django.conf import settings
|
|
@@ -15,7 +15,7 @@ from django_components.logger import logger
|
|
|
15
15
|
|
|
16
16
|
# Same as `Path.is_relative_to`, defined as standalone function because `Path.is_relative_to`
|
|
17
17
|
# is marked for deprecation.
|
|
18
|
-
def path_is_relative_to(child_path: str, parent_path: str):
|
|
18
|
+
def path_is_relative_to(child_path: str, parent_path: str) -> bool:
|
|
19
19
|
# If the relative path doesn't start with `..`, then child is descendant of parent
|
|
20
20
|
# See https://stackoverflow.com/a/7288073/9788634
|
|
21
21
|
rel_path = os.path.relpath(child_path, parent_path)
|
|
@@ -23,7 +23,7 @@ def path_is_relative_to(child_path: str, parent_path: str):
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
class Loader(FilesystemLoader):
|
|
26
|
-
def get_dirs(self):
|
|
26
|
+
def get_dirs(self) -> List[Path]:
|
|
27
27
|
# Allow to configure from settings which dirs should be checked for components
|
|
28
28
|
if hasattr(settings, "STATICFILES_DIRS") and len(settings.STATICFILES_DIRS):
|
|
29
29
|
component_dirs = settings.STATICFILES_DIRS
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import sys
|
|
2
|
-
from typing import TYPE_CHECKING, Iterable, List, Optional, Set, Tuple, Type, Union
|
|
2
|
+
from typing import TYPE_CHECKING, Any, Iterable, List, Mapping, Optional, Set, Tuple, Type, Union
|
|
3
3
|
|
|
4
4
|
if sys.version_info[:2] < (3, 9):
|
|
5
5
|
from typing import ChainMap
|
|
@@ -9,11 +9,11 @@ else:
|
|
|
9
9
|
import django.template
|
|
10
10
|
from django.conf import settings
|
|
11
11
|
from django.template import Context, Template
|
|
12
|
-
from django.template.base import FilterExpression, Node, NodeList, TextNode, TokenType
|
|
12
|
+
from django.template.base import FilterExpression, Node, NodeList, Parser, TextNode, Token, TokenType
|
|
13
13
|
from django.template.defaulttags import CommentNode
|
|
14
14
|
from django.template.exceptions import TemplateSyntaxError
|
|
15
15
|
from django.template.library import parse_bits
|
|
16
|
-
from django.utils.safestring import mark_safe
|
|
16
|
+
from django.utils.safestring import SafeString, mark_safe
|
|
17
17
|
|
|
18
18
|
from django_components.app_settings import app_settings
|
|
19
19
|
from django_components.component_registry import ComponentRegistry
|
|
@@ -46,7 +46,7 @@ FillContent = Tuple[NodeList, Optional[AliasName]]
|
|
|
46
46
|
FilledSlotsContext = ChainMap[Tuple[SlotName, Template], FillContent]
|
|
47
47
|
|
|
48
48
|
|
|
49
|
-
def get_components_from_registry(registry: ComponentRegistry):
|
|
49
|
+
def get_components_from_registry(registry: ComponentRegistry) -> List["Component"]:
|
|
50
50
|
"""Returns a list unique components from the registry."""
|
|
51
51
|
|
|
52
52
|
unique_component_classes = set(registry.all().values())
|
|
@@ -58,7 +58,7 @@ def get_components_from_registry(registry: ComponentRegistry):
|
|
|
58
58
|
return components
|
|
59
59
|
|
|
60
60
|
|
|
61
|
-
def get_components_from_preload_str(preload_str):
|
|
61
|
+
def get_components_from_preload_str(preload_str: str) -> List["Component"]:
|
|
62
62
|
"""Returns a list of unique components from a comma-separated str"""
|
|
63
63
|
|
|
64
64
|
components = []
|
|
@@ -73,7 +73,7 @@ def get_components_from_preload_str(preload_str):
|
|
|
73
73
|
|
|
74
74
|
|
|
75
75
|
@register.simple_tag(name="component_dependencies")
|
|
76
|
-
def component_dependencies_tag(preload=""):
|
|
76
|
+
def component_dependencies_tag(preload: str = "") -> SafeString:
|
|
77
77
|
"""Marks location where CSS link and JS script tags should be rendered."""
|
|
78
78
|
|
|
79
79
|
if is_dependency_middleware_active():
|
|
@@ -90,7 +90,7 @@ def component_dependencies_tag(preload=""):
|
|
|
90
90
|
|
|
91
91
|
|
|
92
92
|
@register.simple_tag(name="component_css_dependencies")
|
|
93
|
-
def component_css_dependencies_tag(preload=""):
|
|
93
|
+
def component_css_dependencies_tag(preload: str = "") -> SafeString:
|
|
94
94
|
"""Marks location where CSS link tags should be rendered."""
|
|
95
95
|
|
|
96
96
|
if is_dependency_middleware_active():
|
|
@@ -107,7 +107,7 @@ def component_css_dependencies_tag(preload=""):
|
|
|
107
107
|
|
|
108
108
|
|
|
109
109
|
@register.simple_tag(name="component_js_dependencies")
|
|
110
|
-
def component_js_dependencies_tag(preload=""):
|
|
110
|
+
def component_js_dependencies_tag(preload: str = "") -> SafeString:
|
|
111
111
|
"""Marks location where JS script tags should be rendered."""
|
|
112
112
|
|
|
113
113
|
if is_dependency_middleware_active():
|
|
@@ -157,7 +157,7 @@ class TemplateAwareNodeMixin:
|
|
|
157
157
|
)
|
|
158
158
|
|
|
159
159
|
@template.setter
|
|
160
|
-
def template(self, value) -> None:
|
|
160
|
+
def template(self, value: Template) -> None:
|
|
161
161
|
self._template = value
|
|
162
162
|
|
|
163
163
|
|
|
@@ -175,7 +175,7 @@ class SlotNode(Node, TemplateAwareNodeMixin):
|
|
|
175
175
|
self.is_default = is_default
|
|
176
176
|
|
|
177
177
|
@property
|
|
178
|
-
def active_flags(self):
|
|
178
|
+
def active_flags(self) -> List[str]:
|
|
179
179
|
m = []
|
|
180
180
|
if self.is_required:
|
|
181
181
|
m.append("required")
|
|
@@ -183,10 +183,10 @@ class SlotNode(Node, TemplateAwareNodeMixin):
|
|
|
183
183
|
m.append("default")
|
|
184
184
|
return m
|
|
185
185
|
|
|
186
|
-
def __repr__(self):
|
|
186
|
+
def __repr__(self) -> str:
|
|
187
187
|
return f"<Slot Node: {self.name}. Contents: {repr(self.nodelist)}. Options: {self.active_flags}>"
|
|
188
188
|
|
|
189
|
-
def render(self, context):
|
|
189
|
+
def render(self, context: Context) -> SafeString:
|
|
190
190
|
try:
|
|
191
191
|
filled_slots_map: FilledSlotsContext = context[FILLED_SLOTS_CONTENT_CONTEXT_KEY]
|
|
192
192
|
except KeyError:
|
|
@@ -213,7 +213,7 @@ class SlotNode(Node, TemplateAwareNodeMixin):
|
|
|
213
213
|
|
|
214
214
|
|
|
215
215
|
@register.tag("slot")
|
|
216
|
-
def do_slot(parser, token):
|
|
216
|
+
def do_slot(parser: Parser, token: Token) -> SlotNode:
|
|
217
217
|
bits = token.split_contents()
|
|
218
218
|
args = bits[1:]
|
|
219
219
|
# e.g. {% slot <name> %}
|
|
@@ -258,10 +258,10 @@ class BaseFillNode(Node):
|
|
|
258
258
|
def __init__(self, nodelist: NodeList):
|
|
259
259
|
self.nodelist: NodeList = nodelist
|
|
260
260
|
|
|
261
|
-
def __repr__(self):
|
|
261
|
+
def __repr__(self) -> str:
|
|
262
262
|
raise NotImplementedError
|
|
263
263
|
|
|
264
|
-
def render(self, context):
|
|
264
|
+
def render(self, context: Context) -> str:
|
|
265
265
|
raise TemplateSyntaxError(
|
|
266
266
|
"{% fill ... %} block cannot be rendered directly. "
|
|
267
267
|
"You are probably seeing this because you have used one outside "
|
|
@@ -280,7 +280,7 @@ class NamedFillNode(BaseFillNode):
|
|
|
280
280
|
self.name_fexp = name_fexp
|
|
281
281
|
self.alias_fexp = alias_fexp
|
|
282
282
|
|
|
283
|
-
def __repr__(self):
|
|
283
|
+
def __repr__(self) -> str:
|
|
284
284
|
return f"<{type(self)} Name: {self.name_fexp}. Contents: {repr(self.nodelist)}.>"
|
|
285
285
|
|
|
286
286
|
|
|
@@ -291,12 +291,12 @@ class ImplicitFillNode(BaseFillNode):
|
|
|
291
291
|
as 'default'.
|
|
292
292
|
"""
|
|
293
293
|
|
|
294
|
-
def __repr__(self):
|
|
294
|
+
def __repr__(self) -> str:
|
|
295
295
|
return f"<{type(self)} Contents: {repr(self.nodelist)}.>"
|
|
296
296
|
|
|
297
297
|
|
|
298
298
|
@register.tag("fill")
|
|
299
|
-
def do_fill(parser, token):
|
|
299
|
+
def do_fill(parser: Parser, token: Token) -> NamedFillNode:
|
|
300
300
|
"""Block tag whose contents 'fill' (are inserted into) an identically named
|
|
301
301
|
'slot'-block in the component template referred to by a parent component.
|
|
302
302
|
It exists to make component nesting easier.
|
|
@@ -333,11 +333,11 @@ class ComponentNode(Node):
|
|
|
333
333
|
def __init__(
|
|
334
334
|
self,
|
|
335
335
|
name_fexp: FilterExpression,
|
|
336
|
-
context_args,
|
|
337
|
-
context_kwargs,
|
|
338
|
-
isolated_context=False,
|
|
336
|
+
context_args: List[FilterExpression],
|
|
337
|
+
context_kwargs: Mapping[str, FilterExpression],
|
|
338
|
+
isolated_context: bool = False,
|
|
339
339
|
fill_nodes: Union[ImplicitFillNode, Iterable[NamedFillNode]] = (),
|
|
340
|
-
):
|
|
340
|
+
) -> None:
|
|
341
341
|
self.name_fexp = name_fexp
|
|
342
342
|
self.context_args = context_args or []
|
|
343
343
|
self.context_kwargs = context_kwargs or {}
|
|
@@ -345,19 +345,19 @@ class ComponentNode(Node):
|
|
|
345
345
|
self.fill_nodes = fill_nodes
|
|
346
346
|
self.nodelist = self._create_nodelist(fill_nodes)
|
|
347
347
|
|
|
348
|
-
def _create_nodelist(self, fill_nodes) -> NodeList:
|
|
348
|
+
def _create_nodelist(self, fill_nodes: Union[Iterable[Node], ImplicitFillNode]) -> NodeList:
|
|
349
349
|
if isinstance(fill_nodes, ImplicitFillNode):
|
|
350
350
|
return NodeList([fill_nodes])
|
|
351
351
|
else:
|
|
352
352
|
return NodeList(fill_nodes)
|
|
353
353
|
|
|
354
|
-
def __repr__(self):
|
|
355
|
-
return "<ComponentNode:
|
|
354
|
+
def __repr__(self) -> str:
|
|
355
|
+
return "<ComponentNode: {}. Contents: {!r}>".format(
|
|
356
356
|
self.name_fexp,
|
|
357
357
|
getattr(self, "nodelist", None), # 'nodelist' attribute only assigned later.
|
|
358
358
|
)
|
|
359
359
|
|
|
360
|
-
def render(self, context: Context):
|
|
360
|
+
def render(self, context: Context) -> str:
|
|
361
361
|
resolved_component_name = self.name_fexp.resolve(context)
|
|
362
362
|
component_cls: Type[Component] = component_registry.get(resolved_component_name)
|
|
363
363
|
|
|
@@ -408,7 +408,7 @@ class ComponentNode(Node):
|
|
|
408
408
|
|
|
409
409
|
|
|
410
410
|
@register.tag(name="component")
|
|
411
|
-
def do_component(parser, token):
|
|
411
|
+
def do_component(parser: Parser, token: Token) -> ComponentNode:
|
|
412
412
|
"""
|
|
413
413
|
To give the component access to the template context:
|
|
414
414
|
{% component "name" positional_arg keyword_arg=value ... %}
|
|
@@ -497,7 +497,7 @@ def try_parse_as_default_fill(
|
|
|
497
497
|
return ImplicitFillNode(nodelist=nodelist)
|
|
498
498
|
|
|
499
499
|
|
|
500
|
-
def block_has_content(nodelist) -> bool:
|
|
500
|
+
def block_has_content(nodelist: NodeList) -> bool:
|
|
501
501
|
for node in nodelist:
|
|
502
502
|
if isinstance(node, TextNode) and node.s.isspace():
|
|
503
503
|
pass
|
|
@@ -512,16 +512,16 @@ def is_whitespace_node(node: Node) -> bool:
|
|
|
512
512
|
return isinstance(node, TextNode) and node.s.isspace()
|
|
513
513
|
|
|
514
514
|
|
|
515
|
-
def is_whitespace_token(token):
|
|
515
|
+
def is_whitespace_token(token: Token) -> bool:
|
|
516
516
|
return token.token_type == TokenType.TEXT and not token.contents.strip()
|
|
517
517
|
|
|
518
518
|
|
|
519
|
-
def is_block_tag_token(token, name):
|
|
519
|
+
def is_block_tag_token(token: Token, name: str) -> bool:
|
|
520
520
|
return token.token_type == TokenType.BLOCK and token.split_contents()[0] == name
|
|
521
521
|
|
|
522
522
|
|
|
523
523
|
@register.tag(name="if_filled")
|
|
524
|
-
def do_if_filled_block(parser, token):
|
|
524
|
+
def do_if_filled_block(parser: Parser, token: Token) -> "IfSlotFilledNode":
|
|
525
525
|
"""
|
|
526
526
|
### Usage
|
|
527
527
|
|
|
@@ -623,7 +623,7 @@ class _IfSlotFilledBranchNode(Node):
|
|
|
623
623
|
def render(self, context: Context) -> str:
|
|
624
624
|
return self.nodelist.render(context)
|
|
625
625
|
|
|
626
|
-
def evaluate(self, context) -> bool:
|
|
626
|
+
def evaluate(self, context: Context) -> bool:
|
|
627
627
|
raise NotImplementedError
|
|
628
628
|
|
|
629
629
|
|
|
@@ -632,13 +632,13 @@ class IfSlotFilledConditionBranchNode(_IfSlotFilledBranchNode, TemplateAwareNode
|
|
|
632
632
|
self,
|
|
633
633
|
slot_name: str,
|
|
634
634
|
nodelist: NodeList,
|
|
635
|
-
is_positive=True,
|
|
635
|
+
is_positive: Union[bool, None] = True,
|
|
636
636
|
) -> None:
|
|
637
637
|
self.slot_name = slot_name
|
|
638
|
-
self.is_positive: bool = is_positive
|
|
638
|
+
self.is_positive: Optional[bool] = is_positive
|
|
639
639
|
super().__init__(nodelist)
|
|
640
640
|
|
|
641
|
-
def evaluate(self, context) -> bool:
|
|
641
|
+
def evaluate(self, context: Context) -> bool:
|
|
642
642
|
try:
|
|
643
643
|
filled_slots: FilledSlotsContext = context[FILLED_SLOTS_CONTENT_CONTEXT_KEY]
|
|
644
644
|
except KeyError:
|
|
@@ -654,7 +654,7 @@ class IfSlotFilledConditionBranchNode(_IfSlotFilledBranchNode, TemplateAwareNode
|
|
|
654
654
|
|
|
655
655
|
|
|
656
656
|
class IfSlotFilledElseBranchNode(_IfSlotFilledBranchNode):
|
|
657
|
-
def evaluate(self, context) -> bool:
|
|
657
|
+
def evaluate(self, context: Context) -> bool:
|
|
658
658
|
return True
|
|
659
659
|
|
|
660
660
|
|
|
@@ -666,13 +666,13 @@ class IfSlotFilledNode(Node):
|
|
|
666
666
|
self.branches = branches
|
|
667
667
|
self.nodelist = self._create_nodelist(branches)
|
|
668
668
|
|
|
669
|
-
def __repr__(self):
|
|
669
|
+
def __repr__(self) -> str:
|
|
670
670
|
return f"<{self.__class__.__name__}>"
|
|
671
671
|
|
|
672
|
-
def _create_nodelist(self, branches) -> NodeList:
|
|
672
|
+
def _create_nodelist(self, branches: List[_IfSlotFilledBranchNode]) -> NodeList:
|
|
673
673
|
return NodeList(branches)
|
|
674
674
|
|
|
675
|
-
def render(self, context):
|
|
675
|
+
def render(self, context: Context) -> str:
|
|
676
676
|
for node in self.branches:
|
|
677
677
|
if isinstance(node, IfSlotFilledElseBranchNode):
|
|
678
678
|
return node.render(context)
|
|
@@ -682,7 +682,7 @@ class IfSlotFilledNode(Node):
|
|
|
682
682
|
return ""
|
|
683
683
|
|
|
684
684
|
|
|
685
|
-
def check_for_isolated_context_keyword(bits):
|
|
685
|
+
def check_for_isolated_context_keyword(bits: List[str]) -> Tuple[List[str], bool]:
|
|
686
686
|
"""Return True and strip the last word if token ends with 'only' keyword or if CONTEXT_BEHAVIOR is 'isolated'."""
|
|
687
687
|
|
|
688
688
|
if bits[-1] == "only":
|
|
@@ -694,7 +694,9 @@ def check_for_isolated_context_keyword(bits):
|
|
|
694
694
|
return bits, False
|
|
695
695
|
|
|
696
696
|
|
|
697
|
-
def parse_component_with_args(
|
|
697
|
+
def parse_component_with_args(
|
|
698
|
+
parser: Parser, bits: List[str], tag_name: str
|
|
699
|
+
) -> Tuple[str, List[FilterExpression], Mapping[str, FilterExpression]]:
|
|
698
700
|
tag_args, tag_kwargs = parse_bits(
|
|
699
701
|
parser=parser,
|
|
700
702
|
bits=bits,
|
|
@@ -726,21 +728,21 @@ def parse_component_with_args(parser, bits, tag_name):
|
|
|
726
728
|
return component_name, context_args, context_kwargs
|
|
727
729
|
|
|
728
730
|
|
|
729
|
-
def safe_resolve(context_item, context):
|
|
731
|
+
def safe_resolve(context_item: FilterExpression, context: Context) -> Any:
|
|
730
732
|
"""Resolve FilterExpressions and Variables in context if possible. Return other items unchanged."""
|
|
731
733
|
|
|
732
734
|
return context_item.resolve(context) if hasattr(context_item, "resolve") else context_item
|
|
733
735
|
|
|
734
736
|
|
|
735
|
-
def is_wrapped_in_quotes(s):
|
|
737
|
+
def is_wrapped_in_quotes(s: str) -> bool:
|
|
736
738
|
return s.startswith(('"', "'")) and s[0] == s[-1]
|
|
737
739
|
|
|
738
740
|
|
|
739
|
-
def is_dependency_middleware_active():
|
|
741
|
+
def is_dependency_middleware_active() -> bool:
|
|
740
742
|
return getattr(settings, "COMPONENTS", {}).get("RENDER_DEPENDENCIES", False)
|
|
741
743
|
|
|
742
744
|
|
|
743
|
-
def norm_and_validate_name(name: str, tag: str, context: Optional[str] = None):
|
|
745
|
+
def norm_and_validate_name(name: str, tag: str, context: Optional[str] = None) -> str:
|
|
744
746
|
"""
|
|
745
747
|
Notes:
|
|
746
748
|
- Value of `tag` in {"slot", "fill", "alias"}
|
|
@@ -756,7 +758,7 @@ def strip_quotes(s: str) -> str:
|
|
|
756
758
|
return s.strip("\"'")
|
|
757
759
|
|
|
758
760
|
|
|
759
|
-
def bool_from_string(s: str):
|
|
761
|
+
def bool_from_string(s: str) -> bool:
|
|
760
762
|
s = strip_quotes(s.lower())
|
|
761
763
|
if s == "true":
|
|
762
764
|
return True
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
+
import typing
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
1
4
|
try:
|
|
2
5
|
from typing import Annotated # type: ignore
|
|
3
6
|
except ImportError:
|
|
4
7
|
|
|
8
|
+
@typing.no_type_check
|
|
5
9
|
class Annotated: # type: ignore
|
|
6
|
-
def __init__(self, type_, *args, **kwargs):
|
|
10
|
+
def __init__(self, type_: str, *args: Any, **kwargs: Any):
|
|
7
11
|
self.type_ = type_
|
|
8
12
|
self.metadata = args, kwargs
|
|
9
13
|
|
|
10
|
-
def __repr__(self):
|
|
14
|
+
def __repr__(self) -> str:
|
|
11
15
|
return f"Annotated[{self.type_}, {self.metadata[0]!r}, {self.metadata[1]!r}]"
|
|
12
16
|
|
|
13
|
-
def __getitem__(self, params):
|
|
17
|
+
def __getitem__(self, params: Any) -> "Annotated": # type: ignore
|
|
14
18
|
if not isinstance(params, tuple):
|
|
15
19
|
params = (params,)
|
|
16
20
|
return Annotated(self.type_, *params, **self.metadata[1]) # type: ignore
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import glob
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from typing import List, Optional, Union
|
|
4
|
+
|
|
5
|
+
from django.template.engine import Engine
|
|
6
|
+
|
|
7
|
+
from django_components.template_loader import Loader
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def search(search_glob: Optional[str] = None, engine: Optional[Engine] = None) -> Union[List[str], List[Path]]:
|
|
11
|
+
"""
|
|
12
|
+
Search for directories that may contain components.
|
|
13
|
+
|
|
14
|
+
If `search_glob` is given, the directories are searched for said glob pattern,
|
|
15
|
+
and glob search results are returned as a flattened list.
|
|
16
|
+
"""
|
|
17
|
+
current_engine = engine
|
|
18
|
+
if current_engine is None:
|
|
19
|
+
current_engine = Engine.get_default()
|
|
20
|
+
|
|
21
|
+
loader = Loader(current_engine)
|
|
22
|
+
dirs = loader.get_dirs()
|
|
23
|
+
|
|
24
|
+
if search_glob is None:
|
|
25
|
+
return dirs
|
|
26
|
+
|
|
27
|
+
component_filenames: List[str] = []
|
|
28
|
+
for directory in dirs:
|
|
29
|
+
for path in glob.iglob(str(Path(directory) / search_glob), recursive=True):
|
|
30
|
+
component_filenames.append(path)
|
|
31
|
+
|
|
32
|
+
return component_filenames
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.py
|
|
5
|
+
src/django_components/__init__.py
|
|
6
|
+
src/django_components/app_settings.py
|
|
7
|
+
src/django_components/apps.py
|
|
8
|
+
src/django_components/component.py
|
|
9
|
+
src/django_components/component_registry.py
|
|
10
|
+
src/django_components/logger.py
|
|
11
|
+
src/django_components/middleware.py
|
|
12
|
+
src/django_components/py.typed
|
|
13
|
+
src/django_components/template_loader.py
|
|
14
|
+
src/django_components/types.py
|
|
15
|
+
src/django_components/utils.py
|
|
16
|
+
src/django_components.egg-info/PKG-INFO
|
|
17
|
+
src/django_components.egg-info/SOURCES.txt
|
|
18
|
+
src/django_components.egg-info/dependency_links.txt
|
|
19
|
+
src/django_components.egg-info/requires.txt
|
|
20
|
+
src/django_components.egg-info/top_level.txt
|
|
21
|
+
src/django_components/management/__init__.py
|
|
22
|
+
src/django_components/management/commands/__init__.py
|
|
23
|
+
src/django_components/management/commands/startcomponent.py
|
|
24
|
+
src/django_components/management/commands/upgradecomponent.py
|
|
25
|
+
src/django_components/safer_staticfiles/__init__.py
|
|
26
|
+
src/django_components/safer_staticfiles/apps.py
|
|
27
|
+
src/django_components/templatetags/__init__.py
|
|
28
|
+
src/django_components/templatetags/component_tags.py
|
|
29
|
+
tests/test_autodiscover.py
|
|
30
|
+
tests/test_component.py
|
|
31
|
+
tests/test_component_as_view.py
|
|
32
|
+
tests/test_context.py
|
|
33
|
+
tests/test_dependency_rendering.py
|
|
34
|
+
tests/test_registry.py
|
|
35
|
+
tests/test_settings.py
|
|
36
|
+
tests/test_startcomponent_command.py
|
|
37
|
+
tests/test_templatetags.py
|
|
38
|
+
tests/testutils.py
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
LICENSE
|
|
2
|
-
README.md
|
|
3
|
-
pyproject.toml
|
|
4
|
-
setup.py
|
|
5
|
-
django_components/__init__.py
|
|
6
|
-
django_components/app_settings.py
|
|
7
|
-
django_components/apps.py
|
|
8
|
-
django_components/component.py
|
|
9
|
-
django_components/component_registry.py
|
|
10
|
-
django_components/logger.py
|
|
11
|
-
django_components/middleware.py
|
|
12
|
-
django_components/template_loader.py
|
|
13
|
-
django_components/types.py
|
|
14
|
-
django_components.egg-info/PKG-INFO
|
|
15
|
-
django_components.egg-info/SOURCES.txt
|
|
16
|
-
django_components.egg-info/dependency_links.txt
|
|
17
|
-
django_components.egg-info/requires.txt
|
|
18
|
-
django_components.egg-info/top_level.txt
|
|
19
|
-
django_components/management/__init__.py
|
|
20
|
-
django_components/management/commands/__init__.py
|
|
21
|
-
django_components/management/commands/startcomponent.py
|
|
22
|
-
django_components/management/commands/upgradecomponent.py
|
|
23
|
-
django_components/safer_staticfiles/__init__.py
|
|
24
|
-
django_components/safer_staticfiles/apps.py
|
|
25
|
-
django_components/templatetags/__init__.py
|
|
26
|
-
django_components/templatetags/component_tags.py
|
|
27
|
-
tests/test_autodiscover.py
|
|
28
|
-
tests/test_component.py
|
|
29
|
-
tests/test_component_as_view.py
|
|
30
|
-
tests/test_context.py
|
|
31
|
-
tests/test_dependency_rendering.py
|
|
32
|
-
tests/test_registry.py
|
|
33
|
-
tests/test_settings.py
|
|
34
|
-
tests/test_startcomponent_command.py
|
|
35
|
-
tests/test_templatetags.py
|
|
36
|
-
tests/testutils.py
|
|
37
|
-
tests/components/__init__.py
|
|
38
|
-
tests/components/single_file.py
|
|
39
|
-
tests/components/urls.py
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
from typing import Any, Dict
|
|
2
|
-
|
|
3
|
-
from django.http import HttpResponse
|
|
4
|
-
|
|
5
|
-
from django_components import component
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
@component.register("single_file_component")
|
|
9
|
-
class SingleFileComponent(component.Component):
|
|
10
|
-
template = """
|
|
11
|
-
<form method="post">
|
|
12
|
-
{% csrf_token %}
|
|
13
|
-
<input type="text" name="variable" value="{{ variable }}">
|
|
14
|
-
<input type="submit">
|
|
15
|
-
</form>
|
|
16
|
-
"""
|
|
17
|
-
|
|
18
|
-
def post(self, request, *args, **kwargs) -> HttpResponse:
|
|
19
|
-
variable = request.POST.get("variable")
|
|
20
|
-
return self.render_to_response({"variable": variable})
|
|
21
|
-
|
|
22
|
-
def get(self, request, *args, **kwargs) -> HttpResponse:
|
|
23
|
-
return self.render_to_response({"variable": "GET"})
|
|
24
|
-
|
|
25
|
-
def get_context_data(self, variable, *args, **kwargs) -> Dict[str, Any]:
|
|
26
|
-
return {"variable": variable}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
from django.urls import path
|
|
2
|
-
|
|
3
|
-
from tests.components.multi_file.multi_file import MultFileComponent
|
|
4
|
-
from tests.components.single_file import SingleFileComponent
|
|
5
|
-
|
|
6
|
-
urlpatterns = [
|
|
7
|
-
path("single/", SingleFileComponent.as_view(), name="single"),
|
|
8
|
-
path("multi/", MultFileComponent.as_view(), name="multi"),
|
|
9
|
-
]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.60 → django_components-0.62/src}/django_components/management/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.60 → django_components-0.62/src}/django_components/safer_staticfiles/apps.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{django_components-0.60 → django_components-0.62/src}/django_components.egg-info/requires.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
|