|
3 | 3 | from __future__ import annotations
|
4 | 4 |
|
5 | 5 | import json
|
6 |
| -import os |
7 | 6 | from typing import TYPE_CHECKING, Any, Callable, Union, cast
|
8 | 7 | from urllib.parse import urlencode
|
9 | 8 |
|
10 |
| -from django.contrib.staticfiles.finders import find |
11 |
| -from django.core.cache import caches |
12 | 9 | from django.http import HttpRequest
|
13 | 10 | from django.urls import reverse
|
14 | 11 | from reactpy import component, hooks, html, utils
|
|
17 | 14 | from reactpy_django.exceptions import ViewNotRegisteredError
|
18 | 15 | from reactpy_django.forms.components import _django_form
|
19 | 16 | from reactpy_django.pyscript.components import _pyscript_component
|
20 |
| -from reactpy_django.utils import ( |
21 |
| - generate_obj_name, |
22 |
| - import_module, |
23 |
| - render_view, |
24 |
| -) |
| 17 | +from reactpy_django.utils import cached_static_file, generate_obj_name, import_module, render_view |
25 | 18 |
|
26 | 19 | if TYPE_CHECKING:
|
27 | 20 | from collections.abc import Sequence
|
@@ -207,7 +200,6 @@ def _view_to_component(
|
207 | 200 | args: Sequence | None,
|
208 | 201 | kwargs: dict | None,
|
209 | 202 | ):
|
210 |
| - """The actual component. Used to prevent pollution of acceptable kwargs keys.""" |
211 | 203 | converted_view, set_converted_view = hooks.use_state(cast(Union[VdomDict, None], None))
|
212 | 204 | _args: Sequence = args or ()
|
213 | 205 | _kwargs: dict = kwargs or {}
|
@@ -249,7 +241,6 @@ def _view_to_iframe(
|
249 | 241 | args: Sequence,
|
250 | 242 | kwargs: dict,
|
251 | 243 | ):
|
252 |
| - """The actual component. Used to prevent pollution of acceptable kwargs keys.""" |
253 | 244 | from reactpy_django.config import REACTPY_REGISTERED_IFRAME_VIEWS
|
254 | 245 |
|
255 | 246 | if hasattr(view, "view_class"):
|
@@ -283,33 +274,9 @@ def _view_to_iframe(
|
283 | 274 |
|
284 | 275 | @component
|
285 | 276 | def _django_css(static_path: str):
|
286 |
| - return html.style(_cached_static_contents(static_path)) |
| 277 | + return html.style(cached_static_file(static_path)) |
287 | 278 |
|
288 | 279 |
|
289 | 280 | @component
|
290 | 281 | def _django_js(static_path: str):
|
291 |
| - return html.script(_cached_static_contents(static_path)) |
292 |
| - |
293 |
| - |
294 |
| -def _cached_static_contents(static_path: str) -> str: |
295 |
| - from reactpy_django.config import REACTPY_CACHE |
296 |
| - |
297 |
| - # Try to find the file within Django's static files |
298 |
| - abs_path = find(static_path) |
299 |
| - if not abs_path: |
300 |
| - msg = f"Could not find static file {static_path} within Django's static files." |
301 |
| - raise FileNotFoundError(msg) |
302 |
| - if isinstance(abs_path, (list, tuple)): |
303 |
| - abs_path = abs_path[0] |
304 |
| - |
305 |
| - # Fetch the file from cache, if available |
306 |
| - last_modified_time = os.stat(abs_path).st_mtime |
307 |
| - cache_key = f"reactpy_django:static_contents:{static_path}" |
308 |
| - file_contents: str | None = caches[REACTPY_CACHE].get(cache_key, version=int(last_modified_time)) |
309 |
| - if file_contents is None: |
310 |
| - with open(abs_path, encoding="utf-8") as static_file: |
311 |
| - file_contents = static_file.read() |
312 |
| - caches[REACTPY_CACHE].delete(cache_key) |
313 |
| - caches[REACTPY_CACHE].set(cache_key, file_contents, timeout=None, version=int(last_modified_time)) |
314 |
| - |
315 |
| - return file_contents |
| 282 | + return html.script(cached_static_file(static_path)) |
0 commit comments