Skip to content

Commit 09d7f84

Browse files
committed
Added first_or_404
1 parent 4768c9a commit 09d7f84

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

gino/ext/sanic.py

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# noinspection PyPackageRequirements
22
from sanic.exceptions import NotFound
33

4-
from ..api import Gino as _Gino
4+
from ..api import Gino as _Gino, GinoExecutor as _Executor
55
from ..local import enable_task_local, disable_task_local
6+
from ..connection import GinoConnection as _Connection
7+
from ..pool import GinoPool as _Pool
68

79

810
class SanicModelMixin:
@@ -15,6 +17,33 @@ async def get_or_404(cls, *args, **kwargs):
1517
return rv
1618

1719

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+
1847
# noinspection PyClassHasNoInit
1948
class Gino(_Gino):
2049
"""Support Sanic web server.
@@ -34,7 +63,10 @@ class Gino(_Gino):
3463
Here `request['connection']` is a :class:`LazyConnection` object, see its
3564
doc string for more information.
3665
"""
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
3870

3971
def init_app(self, app):
4072
task_local_enabled = [False]
@@ -75,3 +107,9 @@ async def after_server_stop(_, loop):
75107
if task_local_enabled[0]:
76108
disable_task_local(loop)
77109
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

Comments
 (0)