Skip to content

Commit 1ee2799

Browse files
committed
cached_static_contents -> cached_static_file
1 parent 3711803 commit 1ee2799

File tree

2 files changed

+29
-36
lines changed

2 files changed

+29
-36
lines changed

src/reactpy_django/components.py

+3-36
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
from __future__ import annotations
44

55
import json
6-
import os
76
from typing import TYPE_CHECKING, Any, Callable, Union, cast
87
from urllib.parse import urlencode
98

10-
from django.contrib.staticfiles.finders import find
11-
from django.core.cache import caches
129
from django.http import HttpRequest
1310
from django.urls import reverse
1411
from reactpy import component, hooks, html, utils
@@ -17,11 +14,7 @@
1714
from reactpy_django.exceptions import ViewNotRegisteredError
1815
from reactpy_django.forms.components import _django_form
1916
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
2518

2619
if TYPE_CHECKING:
2720
from collections.abc import Sequence
@@ -207,7 +200,6 @@ def _view_to_component(
207200
args: Sequence | None,
208201
kwargs: dict | None,
209202
):
210-
"""The actual component. Used to prevent pollution of acceptable kwargs keys."""
211203
converted_view, set_converted_view = hooks.use_state(cast(Union[VdomDict, None], None))
212204
_args: Sequence = args or ()
213205
_kwargs: dict = kwargs or {}
@@ -249,7 +241,6 @@ def _view_to_iframe(
249241
args: Sequence,
250242
kwargs: dict,
251243
):
252-
"""The actual component. Used to prevent pollution of acceptable kwargs keys."""
253244
from reactpy_django.config import REACTPY_REGISTERED_IFRAME_VIEWS
254245

255246
if hasattr(view, "view_class"):
@@ -283,33 +274,9 @@ def _view_to_iframe(
283274

284275
@component
285276
def _django_css(static_path: str):
286-
return html.style(_cached_static_contents(static_path))
277+
return html.style(cached_static_file(static_path))
287278

288279

289280
@component
290281
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))

src/reactpy_django/utils.py

+26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import dill
1717
from asgiref.sync import async_to_sync
1818
from channels.db import database_sync_to_async
19+
from django.contrib.staticfiles.finders import find
20+
from django.core.cache import caches
1921
from django.db.models import ManyToManyField, ManyToOneRel, prefetch_related_objects
2022
from django.db.models.base import Model
2123
from django.db.models.query import QuerySet
@@ -488,3 +490,27 @@ def wrapper(*args, **kwargs):
488490
)
489491

490492
return wrapper
493+
494+
495+
def cached_static_file(static_path: str) -> str:
496+
from reactpy_django.config import REACTPY_CACHE
497+
498+
# Try to find the file within Django's static files
499+
abs_path = find(static_path)
500+
if not abs_path:
501+
msg = f"Could not find static file {static_path} within Django's static files."
502+
raise FileNotFoundError(msg)
503+
if isinstance(abs_path, (list, tuple)):
504+
abs_path = abs_path[0]
505+
506+
# Fetch the file from cache, if available
507+
last_modified_time = os.stat(abs_path).st_mtime
508+
cache_key = f"reactpy_django:static_contents:{static_path}"
509+
file_contents: str | None = caches[REACTPY_CACHE].get(cache_key, version=int(last_modified_time))
510+
if file_contents is None:
511+
with open(abs_path, encoding="utf-8") as static_file:
512+
file_contents = static_file.read()
513+
caches[REACTPY_CACHE].delete(cache_key)
514+
caches[REACTPY_CACHE].set(cache_key, file_contents, timeout=None, version=int(last_modified_time))
515+
516+
return file_contents

0 commit comments

Comments
 (0)