File tree Expand file tree Collapse file tree 3 files changed +19
-14
lines changed Expand file tree Collapse file tree 3 files changed +19
-14
lines changed Original file line number Diff line number Diff line change 1+ import logging
2+
13from flask import Flask
24from flask_postgresql import PostgreSQL
35
6+ logging .basicConfig (
7+ level = logging .DEBUG ,
8+ format = '%(asctime)s %(levelname)-8s %(name)s %(threadName)s : %(message)s'
9+ )
10+
411app = Flask (__name__ )
512app .config ["PG_HOST" ] = "192.168.99.103"
613app .config ["PG_USERNAME" ] = "test"
714app .config ["PG_PASSWORD" ] = "test"
815app .config ["PG_DB" ] = "test"
916app .config ["PG_PORT" ] = "5432"
1017
11- db = PostgreSQL (app )
18+ postgres = PostgreSQL (app )
1219
1320
1421@app .route ("/" )
1522def users ():
16- connection = db .connection
23+ connection = postgres .connection
1724
1825 with connection :
1926 with connection .cursor () as curs :
Original file line number Diff line number Diff line change 11import logging
22
33import psycopg2
4- from flask import current_app , _app_ctx_stack
4+ from flask import current_app , g
55
66logger = logging .getLogger (__name__ )
77
@@ -44,16 +44,14 @@ def connect(self):
4444 logger .debug ('Connection opened successfully' )
4545
4646 def teardown (self , exception ):
47- ctx = _app_ctx_stack .top
48- if hasattr (ctx , 'postgresql_db' ):
49- if ctx .postgresql_db is not None :
50- ctx .postgresql_db .close ()
51- logger .debug ('Connection closed' )
47+ db = getattr (g , '_postgres_db' , None )
48+ if db is not None :
49+ db .close ()
50+ logger .debug ('Connection closed' )
5251
5352 @property
5453 def connection (self ):
55- ctx = _app_ctx_stack .top
56- if ctx is not None :
57- if not hasattr (ctx , 'postgresql_db' ):
58- ctx .postgresql_db = self .connect ()
59- return ctx .postgresql_db
54+ db = getattr (g , '_postgres_db' , None )
55+ if db is None :
56+ db = g ._postgres_db = self .connect ()
57+ return db
Original file line number Diff line number Diff line change 55
66setup (
77 name = "Flask-PostgreSQL" ,
8- version = "1.0 .0" ,
8+ version = "1.1 .0" ,
99 author = "Aleksandrs Kašs" ,
10101111 description = "Flask extension for PostgreSQL" ,
You can’t perform that action at this time.
0 commit comments