-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcomponents.py
54 lines (41 loc) · 1.56 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
from functools import wraps
from typing import Callable
from django.urls import path
from idom.core.proto import VdomDict
from idom.core.vdom import make_vdom_constructor
from idom.html import div
from conreq.utils.environment import get_base_url
BASE_URL = get_base_url(append_slash=False, prepend_slash=False)
if BASE_URL:
BASE_URL = BASE_URL + "/"
iframe = make_vdom_constructor("iframe")
def authenticated(
fallback: str = None,
) -> Callable:
"""Decorates an IDOM component."""
def decorator(func):
@wraps(func)
def _wrapped_func(websocket, *args, **kwargs):
return (
func(websocket, *args, **kwargs)
if websocket.scope["user"].is_authenticated
else fallback or div()
)
return _wrapped_func
return decorator
def django_to_idom(func: Callable) -> VdomDict:
"""Converts a Django view function/class into an IDOM component
by turning it into an iframe. Since this is an iframe, you'll
need to handle all styling on your own."""
# pylint: disable=import-outside-toplevel
from conreq.urls import urlpatterns
from conreq.utils.profiling import profiled_view
# Add a /viewport/path.to.component URL
view = profiled_view(func)
view_name = func.__qualname__
url = (BASE_URL + "viewport/" + view_name).replace("<locals>", "locals")
urlpatterns.append(path(url, view))
# Create an iframe with src=/viewport/path.to.component
def idom_component(*args, **kwargs):
return iframe({"src": url})
return idom_component