-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.py
54 lines (37 loc) · 1.15 KB
/
common.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 __future__ import annotations
from typing import Awaitable, Callable,overload
from reactpy_django.types import FuncParams, Inferred, QueryOptions
import reactpy_django
from utils.params import ParamsBase
class Params(ParamsBase):
pk: int
class LoadingException(Exception):
pass
class RecordNotFound(Exception):
pass
@overload
def use_query(
options: QueryOptions,
/,
query: Callable[FuncParams, Awaitable[Inferred]] | Callable[FuncParams, Inferred],
*args: FuncParams.args,
**kwargs: FuncParams.kwargs,
) -> Inferred: ...
@overload
def use_query(
query: Callable[FuncParams, Awaitable[Inferred]] | Callable[FuncParams, Inferred],
*args: FuncParams.args,
**kwargs: FuncParams.kwargs,
) -> Inferred: ...
def use_query(*args, **kwargs) -> Inferred:
qs = reactpy_django.hooks.use_query(*args, **kwargs)
if qs.error:
raise Exception(qs.error)
elif qs.data is None:
raise LoadingException("Loading...")
records = [rec for rec in qs.data]
if not records:
raise RecordNotFound("Record Not Found")
if len(records) == 1:
qs.data = qs.data[0]
return qs.data