Skip to content

Commit 60357a7

Browse files
committed
Fixed python-gino/gino#38, move get_or_404 to ext
1 parent 068b6a9 commit 60357a7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

gino/ext/sanic.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
# noinspection PyPackageRequirements
2+
from sanic.exceptions import NotFound
3+
14
from .. import api
25
from ..local import enable_task_local, disable_task_local
36

47

8+
class SanicModelMixin:
9+
@classmethod
10+
async def get_or_404(cls, *args, **kwargs):
11+
# noinspection PyUnresolvedReferences
12+
rv = await cls.get(*args, **kwargs)
13+
if rv is None:
14+
raise NotFound('{} is not found'.format(cls.__name__))
15+
return rv
16+
17+
518
# noinspection PyClassHasNoInit
619
class Gino(api.Gino):
720
"""Support Sanic web server.
@@ -21,6 +34,8 @@ class Gino(api.Gino):
2134
Here `request['connection']` is a :class:`LazyConnection` object, see its
2235
doc string for more information.
2336
"""
37+
default_model_classes = api.Gino.default_model_classes + (SanicModelMixin,)
38+
2439
def init_app(self, app):
2540
task_local_enabled = [False]
2641

0 commit comments

Comments
 (0)