@@ -13,7 +13,7 @@ async def get_or_404(cls, *args, **kwargs):
13
13
# noinspection PyUnresolvedReferences
14
14
rv = await cls .get (* args , ** kwargs )
15
15
if rv is None :
16
- raise NotFound (' {} is not found' .format (cls .__name__ ))
16
+ raise NotFound (" {} is not found" .format (cls .__name__ ))
17
17
return rv
18
18
19
19
@@ -22,7 +22,7 @@ class GinoExecutor(_Executor):
22
22
async def first_or_404 (self , * args , ** kwargs ):
23
23
rv = await self .first (* args , ** kwargs )
24
24
if rv is None :
25
- raise NotFound (' No such data' )
25
+ raise NotFound (" No such data" )
26
26
return rv
27
27
28
28
@@ -31,7 +31,7 @@ class GinoConnection(_Connection):
31
31
async def first_or_404 (self , * args , ** kwargs ):
32
32
rv = await self .first (* args , ** kwargs )
33
33
if rv is None :
34
- raise NotFound (' No such data' )
34
+ raise NotFound (" No such data" )
35
35
return rv
36
36
37
37
@@ -42,12 +42,12 @@ class GinoEngine(_Engine):
42
42
async def first_or_404 (self , * args , ** kwargs ):
43
43
rv = await self .first (* args , ** kwargs )
44
44
if rv is None :
45
- raise NotFound (' No such data' )
45
+ raise NotFound (" No such data" )
46
46
return rv
47
47
48
48
49
49
class SanicStrategy (GinoStrategy ):
50
- name = ' sanic'
50
+ name = " sanic"
51
51
engine_cls = GinoEngine
52
52
53
53
@@ -72,6 +72,7 @@ class Gino(_Gino):
72
72
await request['connection'].release(permanent=False)
73
73
74
74
"""
75
+
75
76
model_base_classes = _Gino .model_base_classes + (SanicModelMixin ,)
76
77
query_executor = GinoExecutor
77
78
@@ -81,58 +82,59 @@ def __init__(self, app=None, *args, **kwargs):
81
82
self .init_app (app )
82
83
83
84
def init_app (self , app ):
84
- if app .config .setdefault ('DB_USE_CONNECTION_FOR_REQUEST' , True ):
85
- @app .middleware ('request' )
85
+ if app .config .setdefault ("DB_USE_CONNECTION_FOR_REQUEST" , True ):
86
+
87
+ @app .middleware ("request" )
86
88
async def on_request (request ):
87
89
conn = await self .acquire (lazy = True )
88
- if hasattr (request , ' ctx' ):
90
+ if hasattr (request , " ctx" ):
89
91
request .ctx .connection = conn
90
92
else :
91
- request [' connection' ] = conn
93
+ request [" connection" ] = conn
92
94
93
- @app .middleware (' response' )
95
+ @app .middleware (" response" )
94
96
async def on_response (request , _ ):
95
- if hasattr (request , ' ctx' ):
96
- conn = getattr (request .ctx , ' connection' , None )
97
+ if hasattr (request , " ctx" ):
98
+ conn = getattr (request .ctx , " connection" , None )
97
99
else :
98
- conn = request .pop (' connection' , None )
100
+ conn = request .pop (" connection" , None )
99
101
if conn is not None :
100
102
await conn .release ()
101
103
102
- @app .listener (' after_server_start' )
104
+ @app .listener (" after_server_start" )
103
105
async def before_server_start (_ , loop ):
104
- if app .config .get (' DB_DSN' ):
106
+ if app .config .get (" DB_DSN" ):
105
107
dsn = app .config .DB_DSN
106
108
else :
107
109
dsn = URL (
108
- drivername = app .config .setdefault (' DB_DRIVER' , ' asyncpg' ),
109
- host = app .config .setdefault (' DB_HOST' , ' localhost' ),
110
- port = app .config .setdefault (' DB_PORT' , 5432 ),
111
- username = app .config .setdefault (' DB_USER' , ' postgres' ),
112
- password = app .config .setdefault (' DB_PASSWORD' , '' ),
113
- database = app .config .setdefault (' DB_DATABASE' , ' postgres' ),
110
+ drivername = app .config .setdefault (" DB_DRIVER" , " asyncpg" ),
111
+ host = app .config .setdefault (" DB_HOST" , " localhost" ),
112
+ port = app .config .setdefault (" DB_PORT" , 5432 ),
113
+ username = app .config .setdefault (" DB_USER" , " postgres" ),
114
+ password = app .config .setdefault (" DB_PASSWORD" , "" ),
115
+ database = app .config .setdefault (" DB_DATABASE" , " postgres" ),
114
116
)
115
117
116
118
await self .set_bind (
117
119
dsn ,
118
- echo = app .config .setdefault (' DB_ECHO' , False ),
119
- min_size = app .config .setdefault (' DB_POOL_MIN_SIZE' , 5 ),
120
- max_size = app .config .setdefault (' DB_POOL_MAX_SIZE' , 10 ),
121
- ssl = app .config .setdefault (' DB_SSL' ),
120
+ echo = app .config .setdefault (" DB_ECHO" , False ),
121
+ min_size = app .config .setdefault (" DB_POOL_MIN_SIZE" , 5 ),
122
+ max_size = app .config .setdefault (" DB_POOL_MAX_SIZE" , 10 ),
123
+ ssl = app .config .setdefault (" DB_SSL" ),
122
124
loop = loop ,
123
- ** app .config .setdefault (' DB_KWARGS' , dict ()),
125
+ ** app .config .setdefault (" DB_KWARGS" , dict ()),
124
126
)
125
127
126
- @app .listener (' before_server_stop' )
128
+ @app .listener (" before_server_stop" )
127
129
async def after_server_stop (_ , loop ):
128
130
await self .pop_bind ().close ()
129
131
130
132
async def first_or_404 (self , * args , ** kwargs ):
131
133
rv = await self .first (* args , ** kwargs )
132
134
if rv is None :
133
- raise NotFound (' No such data' )
135
+ raise NotFound (" No such data" )
134
136
return rv
135
137
136
138
async def set_bind (self , bind , loop = None , ** kwargs ):
137
- kwargs .setdefault (' strategy' , ' sanic' )
139
+ kwargs .setdefault (" strategy" , " sanic" )
138
140
return await super ().set_bind (bind , loop = loop , ** kwargs )
0 commit comments