1
1
# noinspection PyPackageRequirements
2
2
from sanic .exceptions import NotFound
3
3
4
- from ..api import Gino as _Gino
4
+ from ..api import Gino as _Gino , GinoExecutor as _Executor
5
5
from ..local import enable_task_local , disable_task_local
6
+ from ..connection import GinoConnection as _Connection
7
+ from ..pool import GinoPool as _Pool
6
8
7
9
8
10
class SanicModelMixin :
@@ -15,6 +17,33 @@ async def get_or_404(cls, *args, **kwargs):
15
17
return rv
16
18
17
19
20
+ # noinspection PyClassHasNoInit
21
+ class GinoExecutor (_Executor ):
22
+ async def first_or_404 (self , * args , ** kwargs ):
23
+ rv = await self .first (* args , ** kwargs )
24
+ if rv is None :
25
+ raise NotFound ('No such data' )
26
+ return rv
27
+
28
+
29
+ # noinspection PyClassHasNoInit
30
+ class GinoConnection (_Connection ):
31
+ async def first_or_404 (self , * args , ** kwargs ):
32
+ rv = await self .first (* args , ** kwargs )
33
+ if rv is None :
34
+ raise NotFound ('No such data' )
35
+ return rv
36
+
37
+
38
+ # noinspection PyClassHasNoInit
39
+ class GinoPool (_Pool ):
40
+ async def first_or_404 (self , * args , ** kwargs ):
41
+ rv = await self .first (* args , ** kwargs )
42
+ if rv is None :
43
+ raise NotFound ('No such data' )
44
+ return rv
45
+
46
+
18
47
# noinspection PyClassHasNoInit
19
48
class Gino (_Gino ):
20
49
"""Support Sanic web server.
@@ -34,7 +63,10 @@ class Gino(_Gino):
34
63
Here `request['connection']` is a :class:`LazyConnection` object, see its
35
64
doc string for more information.
36
65
"""
37
- default_model_classes = _Gino .default_model_classes + (SanicModelMixin ,)
66
+ model_base_classes = _Gino .model_base_classes + (SanicModelMixin ,)
67
+ query_executor = GinoExecutor
68
+ connection_cls = GinoConnection
69
+ pool_cls = GinoPool
38
70
39
71
def init_app (self , app ):
40
72
task_local_enabled = [False ]
@@ -75,3 +107,9 @@ async def after_server_stop(_, loop):
75
107
if task_local_enabled [0 ]:
76
108
disable_task_local (loop )
77
109
task_local_enabled [0 ] = False
110
+
111
+ async def first_or_404 (self , * args , ** kwargs ):
112
+ rv = await self .first (* args , ** kwargs )
113
+ if rv is None :
114
+ raise NotFound ('No such data' )
115
+ return rv
0 commit comments