[sql] Late init_app of SQLAlchemy
in order to be able to run sql/models.py alone (e.g. sphinx autodoc)
This commit is contained in:
parent
a0318ce82e
commit
8e59d3d661
1 changed files with 5 additions and 6 deletions
|
@ -8,18 +8,17 @@ from accounting.storage import Storage
|
||||||
from accounting.models import Transaction, Posting, Amount
|
from accounting.models import Transaction, Posting, Amount
|
||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
db = None
|
db = SQLAlchemy()
|
||||||
|
|
||||||
|
|
||||||
class SQLStorage(Storage):
|
class SQLStorage(Storage):
|
||||||
def __init__(self, app=None):
|
def __init__(self, app=None):
|
||||||
global db
|
|
||||||
|
|
||||||
if not app:
|
if not app:
|
||||||
raise Exception('Missing app keyword argument')
|
raise Exception('Missing app keyword argument')
|
||||||
|
|
||||||
self.app = app
|
self.app = app
|
||||||
db = self.db = SQLAlchemy(app)
|
db.init_app(app)
|
||||||
|
|
||||||
from .models import Transaction as SQLTransaction, \
|
from .models import Transaction as SQLTransaction, \
|
||||||
Posting as SQLPosting, Amount as SQLAmount
|
Posting as SQLPosting, Amount as SQLAmount
|
||||||
|
@ -69,7 +68,7 @@ class SQLStorage(Storage):
|
||||||
_t.payee = transaction.payee
|
_t.payee = transaction.payee
|
||||||
_t.meta = json.dumps(transaction.metadata)
|
_t.meta = json.dumps(transaction.metadata)
|
||||||
|
|
||||||
self.db.session.add(_t)
|
db.session.add(_t)
|
||||||
|
|
||||||
for posting in transaction.postings:
|
for posting in transaction.postings:
|
||||||
_p = self.Posting()
|
_p = self.Posting()
|
||||||
|
@ -79,6 +78,6 @@ class SQLStorage(Storage):
|
||||||
_p.amount = self.Amount(symbol=posting.amount.symbol,
|
_p.amount = self.Amount(symbol=posting.amount.symbol,
|
||||||
amount=posting.amount.amount)
|
amount=posting.amount.amount)
|
||||||
|
|
||||||
self.db.session.add(_p)
|
db.session.add(_p)
|
||||||
|
|
||||||
self.db.session.commit()
|
db.session.commit()
|
||||||
|
|
Loading…
Reference in a new issue