forked from reactive-python/reactpy-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.py
338 lines (276 loc) · 11.2 KB
/
components.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
from __future__ import annotations
import json
import os
from typing import Any, Callable, Sequence, Union, cast, overload
from urllib.parse import urlencode
from uuid import uuid4
from warnings import warn
from django.contrib.staticfiles.finders import find
from django.core.cache import caches
from django.http import HttpRequest
from django.urls import reverse
from django.views import View
from reactpy import component, hooks, html, utils
from reactpy.types import Key, VdomDict
from reactpy_django.exceptions import ViewNotRegisteredError
from reactpy_django.hooks import use_scope
from reactpy_django.utils import generate_obj_name, import_module, render_view
# Type hints for:
# 1. example = view_to_component(my_view, ...)
# 2. @view_to_component
@overload
def view_to_component(
view: Callable | View | str,
compatibility: bool = False,
transforms: Sequence[Callable[[VdomDict], Any]] = (),
strict_parsing: bool = True,
) -> Any: ...
# Type hints for:
# 1. @view_to_component(...)
@overload
def view_to_component(
view: None = ...,
compatibility: bool = False,
transforms: Sequence[Callable[[VdomDict], Any]] = (),
strict_parsing: bool = True,
) -> Callable[[Callable], Any]: ...
def view_to_component(
view: Callable | View | str | None = None,
compatibility: bool = False,
transforms: Sequence[Callable[[VdomDict], Any]] = (),
strict_parsing: bool = True,
) -> Any | Callable[[Callable], Any]:
"""Converts a Django view to a ReactPy component.
Keyword Args:
view: The view to convert, or the view's dotted path as a string.
compatibility: **DEPRECATED.** Use `view_to_iframe` instead.
transforms: A list of functions that transforms the newly generated VDOM. \
The functions will be called on each VDOM node.
strict_parsing: If True, an exception will be generated if the HTML does not \
perfectly adhere to HTML5.
Returns:
A function that takes `request, *args, key, **kwargs` and returns a ReactPy component.
"""
def decorator(view: Callable | View | str):
if not view:
raise ValueError("A view must be provided to `view_to_component`")
def constructor(
request: HttpRequest | None = None,
*args,
key: Key | None = None,
**kwargs,
):
return _view_to_component(
view=view,
compatibility=compatibility,
transforms=transforms,
strict_parsing=strict_parsing,
request=request,
args=args,
kwargs=kwargs,
key=key,
)
return constructor
if not view:
warn(
"Using `view_to_component` as a decorator is deprecated. "
"This functionality will be removed in a future version.",
DeprecationWarning,
)
return decorator(view) if view else decorator
def view_to_iframe(
view: Callable | View | str, extra_props: dict[str, Any] | None = None
):
"""
Args:
view: The view function or class to convert, or the dotted path to the view.
Keyword Args:
extra_props: Additional properties to add to the `iframe` element.
Returns:
A function that takes `*args, key, **kwargs` and returns a ReactPy component.
"""
def constructor(
*args,
key: Key | None = None,
**kwargs,
):
return _view_to_iframe(
view=view, extra_props=extra_props, args=args, kwargs=kwargs, key=key
)
return constructor
def django_css(
static_path: str, allow_duplicates: bool = False, key: Key | None = None
):
"""Fetches a CSS static file for use within ReactPy. This allows for deferred CSS loading.
Args:
static_path: The path to the static file. This path is identical to what you would \
use on Django's `{% static %}` template tag
key: A key to uniquely identify this component which is unique amongst a component's \
immediate siblings
"""
return _django_css(
static_path=static_path, allow_duplicates=allow_duplicates, key=key
)
def django_js(static_path: str, allow_duplicates: bool = False, key: Key | None = None):
"""Fetches a JS static file for use within ReactPy. This allows for deferred JS loading.
Args:
static_path: The path to the static file. This path is identical to what you would \
use on Django's `{% static %}` template tag.
key: A key to uniquely identify this component which is unique amongst a component's \
immediate siblings
"""
return _django_js(
static_path=static_path, allow_duplicates=allow_duplicates, key=key
)
@component
def _view_to_component(
view: Callable | View | str,
compatibility: bool,
transforms: Sequence[Callable[[VdomDict], Any]],
strict_parsing: bool,
request: HttpRequest | None,
args: Sequence | None,
kwargs: dict | None,
):
"""The actual component. Used to prevent pollution of acceptable kwargs keys."""
converted_view, set_converted_view = hooks.use_state(
cast(Union[VdomDict, None], None)
)
_args: Sequence = args or ()
_kwargs: dict = kwargs or {}
if request:
_request: HttpRequest = request
else:
_request = HttpRequest()
_request.method = "GET"
resolved_view: Callable = import_module(view) if isinstance(view, str) else view # type: ignore[assignment]
# Render the view render within a hook
@hooks.use_effect(
dependencies=[
json.dumps(vars(_request), default=lambda x: generate_obj_name(x)),
json.dumps([_args, _kwargs], default=lambda x: generate_obj_name(x)),
]
)
async def async_render():
"""Render the view in an async hook to avoid blocking the main thread."""
# Compatibility mode doesn't require a traditional render
if compatibility:
return
# Render the view
response = await render_view(resolved_view, _request, _args, _kwargs)
set_converted_view(
utils.html_to_vdom(
response.content.decode("utf-8").strip(),
utils.del_html_head_body_transform,
*transforms,
strict=strict_parsing,
)
)
# Render in compatibility mode, if needed
if compatibility:
# Warn the user that compatibility mode is deprecated
warn(
"view_to_component(compatibility=True) is deprecated and will be removed in a future version. "
"Please use `view_to_iframe` instead.",
DeprecationWarning,
)
return view_to_iframe(resolved_view)(*_args, **_kwargs)
# Return the view if it's been rendered via the `async_render` hook
return converted_view
@component
def _view_to_iframe(
view: Callable | View | str,
extra_props: dict[str, Any] | None,
args: Sequence,
kwargs: dict,
) -> VdomDict:
"""The actual component. Used to prevent pollution of acceptable kwargs keys."""
from reactpy_django.config import REACTPY_REGISTERED_IFRAME_VIEWS
if hasattr(view, "view_class"):
view = view.view_class
dotted_path = view if isinstance(view, str) else generate_obj_name(view)
registered_view = REACTPY_REGISTERED_IFRAME_VIEWS.get(dotted_path)
if not registered_view:
raise ViewNotRegisteredError(
f"'{dotted_path}' has not been registered as an iframe! "
"Are you sure you called `register_iframe` within a Django `AppConfig.ready` method?"
)
query = kwargs.copy()
if args:
query["_args"] = args
query_string = f"?{urlencode(query, doseq=True)}" if args or kwargs else ""
extra_props = extra_props or {}
extra_props.pop("src", None)
return html.iframe(
{
"src": reverse("reactpy:view_to_iframe", args=[dotted_path]) + query_string,
"style": {"border": "none"},
"onload": 'javascript:(function(o){o.style.height=o.contentWindow.document.body.scrollHeight+"px";}(this));',
"loading": "lazy",
}
| extra_props
)
@component
def _django_css(static_path: str, allow_duplicates: bool):
scope = use_scope()
ownership_uuid = hooks.use_memo(lambda: uuid4())
scope.setdefault("reactpy", {}).setdefault("css", {})
scope["reactpy"]["css"].setdefault(static_path, ownership_uuid)
# Load the file if no other component has loaded it
@hooks.use_effect(dependencies=None)
async def duplicate_manager():
if allow_duplicates:
return
# If the file currently isn't rendered, let this component render it
if not scope["reactpy"]["css"].get(static_path):
scope["reactpy"]["css"].setdefault(static_path, ownership_uuid)
# The component that loaded the file should notify when it's removed
def unmount():
if scope["reactpy"]["css"].get(static_path) == ownership_uuid:
scope["reactpy"]["css"].pop(static_path)
return unmount
if allow_duplicates or (scope["reactpy"]["css"].get(static_path) == ownership_uuid):
return html.style(_cached_static_contents(static_path))
@component
def _django_js(static_path: str, allow_duplicates: bool):
scope = use_scope()
ownership_uuid = hooks.use_memo(lambda: uuid4())
scope.setdefault("reactpy", {}).setdefault("js", {})
scope["reactpy"]["js"].setdefault(static_path, ownership_uuid)
# Load the file if no other component has loaded it
@hooks.use_effect(dependencies=None)
async def duplicate_manager():
if allow_duplicates:
return
# If the file currently isn't rendered, let this component render it
if not scope["reactpy"]["js"].get(static_path):
scope["reactpy"]["js"].setdefault(static_path, ownership_uuid)
# The component that loaded the file should notify when it's removed
def unmount():
if scope["reactpy"]["js"].get(static_path) == ownership_uuid:
scope["reactpy"]["js"].pop(static_path)
return unmount
if allow_duplicates or (scope["reactpy"]["js"].get(static_path) == ownership_uuid):
return html.script(_cached_static_contents(static_path))
def _cached_static_contents(static_path: str) -> str:
from reactpy_django.config import REACTPY_CACHE
# Try to find the file within Django's static files
abs_path = find(static_path)
if not abs_path:
raise FileNotFoundError(
f"Could not find static file {static_path} within Django's static files."
)
# Fetch the file from cache, if available
last_modified_time = os.stat(abs_path).st_mtime
cache_key = f"reactpy_django:static_contents:{static_path}"
file_contents: str | None = caches[REACTPY_CACHE].get(
cache_key, version=int(last_modified_time)
)
if file_contents is None:
with open(abs_path, encoding="utf-8") as static_file:
file_contents = static_file.read()
caches[REACTPY_CACHE].delete(cache_key)
caches[REACTPY_CACHE].set(
cache_key, file_contents, timeout=None, version=int(last_modified_time)
)
return file_contents