Skip to content

Commit 5a2b008

Browse files
committed
Changes regarding to this Flask change: pallets/flask#4682
1 parent 99fd948 commit 5a2b008

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

examples/app.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
1+
import logging
2+
13
from flask import Flask
24
from 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+
411
app = Flask(__name__)
512
app.config["PG_HOST"] = "192.168.99.103"
613
app.config["PG_USERNAME"] = "test"
714
app.config["PG_PASSWORD"] = "test"
815
app.config["PG_DB"] = "test"
916
app.config["PG_PORT"] = "5432"
1017

11-
db = PostgreSQL(app)
18+
postgres = PostgreSQL(app)
1219

1320

1421
@app.route("/")
1522
def users():
16-
connection = db.connection
23+
connection = postgres.connection
1724

1825
with connection:
1926
with connection.cursor() as curs:

flask_postgresql/postgresql.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22

33
import psycopg2
4-
from flask import current_app, _app_ctx_stack
4+
from flask import current_app, g
55

66
logger = 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

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="Flask-PostgreSQL",
8-
version="1.0.0",
8+
version="1.1.0",
99
author="Aleksandrs Kašs",
1010
author_email="[email protected]",
1111
description="Flask extension for PostgreSQL",

0 commit comments

Comments
 (0)