22
33import json
44import os
5- from typing import Any , Callable , Sequence , Union , cast , overload
5+ from typing import Any , Callable , Sequence , Union , cast
66from urllib .parse import urlencode
77from uuid import uuid4
8- from warnings import warn
98
109from django .contrib .staticfiles .finders import find
1110from django .core .cache import caches
2625)
2726
2827
29- # Type hints for:
30- # 1. example = view_to_component(my_view, ...)
31- # 2. @view_to_component
32- @overload
3328def view_to_component (
3429 view : Callable | View | str ,
35- compatibility : bool = False ,
3630 transforms : Sequence [Callable [[VdomDict ], Any ]] = (),
3731 strict_parsing : bool = True ,
38- ) -> Any : ...
39-
40-
41- # Type hints for:
42- # 1. @view_to_component(...)
43- @overload
44- def view_to_component (
45- view : None = ...,
46- compatibility : bool = False ,
47- transforms : Sequence [Callable [[VdomDict ], Any ]] = (),
48- strict_parsing : bool = True ,
49- ) -> Callable [[Callable ], Any ]: ...
50-
51-
52- def view_to_component (
53- view : Callable | View | str | None = None ,
54- compatibility : bool = False ,
55- transforms : Sequence [Callable [[VdomDict ], Any ]] = (),
56- strict_parsing : bool = True ,
57- ) -> Any | Callable [[Callable ], Any ]:
32+ ) -> Any :
5833 """Converts a Django view to a ReactPy component.
5934
6035 Keyword Args:
6136 view: The view to convert, or the view's dotted path as a string.
62- compatibility: **DEPRECATED.** Use `view_to_iframe` instead.
6337 transforms: A list of functions that transforms the newly generated VDOM. \
6438 The functions will be called on each VDOM node.
6539 strict_parsing: If True, an exception will be generated if the HTML does not \
@@ -69,37 +43,23 @@ def view_to_component(
6943 A function that takes `request, *args, key, **kwargs` and returns a ReactPy component.
7044 """
7145
72- def decorator (view : Callable | View | str ):
73- if not view :
74- raise ValueError ("A view must be provided to `view_to_component`" )
75-
76- def constructor (
77- request : HttpRequest | None = None ,
78- * args ,
79- key : Key | None = None ,
80- ** kwargs ,
81- ):
82- return _view_to_component (
83- view = view ,
84- compatibility = compatibility ,
85- transforms = transforms ,
86- strict_parsing = strict_parsing ,
87- request = request ,
88- args = args ,
89- kwargs = kwargs ,
90- key = key ,
91- )
92-
93- return constructor
94-
95- if not view :
96- warn (
97- "Using `view_to_component` as a decorator is deprecated. "
98- "This functionality will be removed in a future version." ,
99- DeprecationWarning ,
46+ def constructor (
47+ request : HttpRequest | None = None ,
48+ * args ,
49+ key : Key | None = None ,
50+ ** kwargs ,
51+ ):
52+ return _view_to_component (
53+ view = view ,
54+ transforms = transforms ,
55+ strict_parsing = strict_parsing ,
56+ request = request ,
57+ args = args ,
58+ kwargs = kwargs ,
59+ key = key ,
10060 )
10161
102- return decorator ( view ) if view else decorator
62+ return constructor
10363
10464
10565def view_to_iframe (
@@ -180,7 +140,6 @@ def pyscript_component(
180140@component
181141def _view_to_component (
182142 view : Callable | View | str ,
183- compatibility : bool ,
184143 transforms : Sequence [Callable [[VdomDict ], Any ]],
185144 strict_parsing : bool ,
186145 request : HttpRequest | None ,
@@ -209,10 +168,6 @@ def _view_to_component(
209168 )
210169 async def async_render ():
211170 """Render the view in an async hook to avoid blocking the main thread."""
212- # Compatibility mode doesn't require a traditional render
213- if compatibility :
214- return
215-
216171 # Render the view
217172 response = await render_view (resolved_view , _request , _args , _kwargs )
218173 set_converted_view (
@@ -224,17 +179,6 @@ async def async_render():
224179 )
225180 )
226181
227- # Render in compatibility mode, if needed
228- if compatibility :
229- # Warn the user that compatibility mode is deprecated
230- warn (
231- "view_to_component(compatibility=True) is deprecated and will be removed in a future version. "
232- "Please use `view_to_iframe` instead." ,
233- DeprecationWarning ,
234- )
235-
236- return view_to_iframe (resolved_view )(* _args , ** _kwargs )
237-
238182 # Return the view if it's been rendered via the `async_render` hook
239183 return converted_view
240184
0 commit comments