forked from reactive-python/reactpy-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer.py
137 lines (117 loc) · 5.16 KB
/
consumer.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
"""Anything used to construct a websocket endpoint"""
from __future__ import annotations
import asyncio
import logging
from datetime import timedelta
from typing import Any, MutableMapping, Sequence
import dill as pickle
from channels.auth import login
from channels.db import database_sync_to_async as convert_to_async
from channels.generic.websocket import AsyncJsonWebsocketConsumer
from django.utils import timezone
from idom.backend.hooks import ConnectionContext
from idom.backend.types import Connection, Location
from idom.core.layout import Layout
from idom.core.serve import serve_layout
from django_idom.types import ComponentParamData, ComponentWebsocket
from django_idom.utils import db_cleanup, func_has_params
_logger = logging.getLogger(__name__)
class IdomAsyncWebsocketConsumer(AsyncJsonWebsocketConsumer):
"""Communicates with the browser to perform actions on-demand."""
async def connect(self) -> None:
from django.contrib.auth.models import AbstractBaseUser
await super().connect()
user: AbstractBaseUser = self.scope.get("user")
if user and user.is_authenticated:
try:
await login(self.scope, user)
await convert_to_async(self.scope["session"].save)()
except Exception:
_logger.exception("IDOM websocket authentication has failed!")
elif user is None:
_logger.warning("IDOM websocket is missing AuthMiddlewareStack!")
self._idom_dispatcher_future = asyncio.ensure_future(self._run_dispatch_loop())
async def disconnect(self, code: int) -> None:
if self._idom_dispatcher_future.done():
await self._idom_dispatcher_future
else:
self._idom_dispatcher_future.cancel()
await super().disconnect(code)
async def receive_json(self, content: Any, **_) -> None:
await self._idom_recv_queue.put(content)
async def _run_dispatch_loop(self):
from django_idom import models
from django_idom.config import (
IDOM_DATABASE,
IDOM_RECONNECT_MAX,
IDOM_REGISTERED_COMPONENTS,
)
scope = self.scope
dotted_path = scope["url_route"]["kwargs"]["dotted_path"]
uuid = scope["url_route"]["kwargs"]["uuid"]
search = scope["query_string"].decode()
self._idom_recv_queue: asyncio.Queue = asyncio.Queue()
connection = Connection( # For `use_connection`
scope=scope,
location=Location(
pathname=scope["path"],
search=f"?{search}" if (search and (search != "undefined")) else "",
),
carrier=ComponentWebsocket(self.close, self.disconnect, dotted_path),
)
now = timezone.now()
component_args: Sequence[Any] = tuple()
component_kwargs: MutableMapping[str, Any] = {}
# Verify the component has already been registered
try:
component_constructor = IDOM_REGISTERED_COMPONENTS[dotted_path]
except KeyError:
_logger.warning(
f"Attempt to access invalid IDOM component: {dotted_path!r}"
)
return
# Fetch the component's args/kwargs from the database, if needed
try:
if func_has_params(component_constructor):
try:
# Always clean up expired entries first
await convert_to_async(db_cleanup)()
# Get the queries from a DB
params_query = await models.ComponentSession.objects.using(
IDOM_DATABASE
).aget(
uuid=uuid,
last_accessed__gt=now - timedelta(seconds=IDOM_RECONNECT_MAX),
)
params_query.last_accessed = timezone.now()
await convert_to_async(params_query.save)()
except models.ComponentSession.DoesNotExist:
_logger.warning(
f"Browser has attempted to access '{dotted_path}', "
f"but the component has already expired beyond IDOM_RECONNECT_MAX. "
"If this was expected, this warning can be ignored."
)
return
component_params: ComponentParamData = pickle.loads(params_query.params)
component_args = component_params.args
component_kwargs = component_params.kwargs
# Generate the initial component instance
component_instance = component_constructor(
*component_args, **component_kwargs
)
except Exception:
_logger.exception(
f"Failed to construct component {component_constructor} "
f"with parameters {component_kwargs}"
)
return
# Begin serving the IDOM component
try:
await serve_layout(
Layout(ConnectionContext(component_instance, value=connection)),
self.send_json,
self._idom_recv_queue.get,
)
except Exception:
await self.close()
raise