From 8e59d3d661591d8d3dcb8a9d9084572b627c7958 Mon Sep 17 00:00:00 2001 From: Joar Wandborg Date: Tue, 17 Dec 2013 11:16:51 +0100 Subject: [PATCH] [sql] Late init_app of SQLAlchemy in order to be able to run sql/models.py alone (e.g. sphinx autodoc) --- accounting/storage/sql/__init__.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/accounting/storage/sql/__init__.py b/accounting/storage/sql/__init__.py index 69c26a2..c213f81 100644 --- a/accounting/storage/sql/__init__.py +++ b/accounting/storage/sql/__init__.py @@ -8,18 +8,17 @@ from accounting.storage import Storage from accounting.models import Transaction, Posting, Amount _log = logging.getLogger(__name__) -db = None +db = SQLAlchemy() class SQLStorage(Storage): def __init__(self, app=None): - global db if not app: raise Exception('Missing app keyword argument') self.app = app - db = self.db = SQLAlchemy(app) + db.init_app(app) from .models import Transaction as SQLTransaction, \ Posting as SQLPosting, Amount as SQLAmount @@ -69,7 +68,7 @@ class SQLStorage(Storage): _t.payee = transaction.payee _t.meta = json.dumps(transaction.metadata) - self.db.session.add(_t) + db.session.add(_t) for posting in transaction.postings: _p = self.Posting() @@ -79,6 +78,6 @@ class SQLStorage(Storage): _p.amount = self.Amount(symbol=posting.amount.symbol, amount=posting.amount.amount) - self.db.session.add(_p) + db.session.add(_p) - self.db.session.commit() + db.session.commit()