Fixed (really) the session management.
This commit is contained in:
parent
f56e06223f
commit
cfef22ee65
@ -1,7 +1,6 @@
|
|||||||
|
|
||||||
from app import app
|
from app import app
|
||||||
from app import db
|
from app import db
|
||||||
from app import session
|
|
||||||
|
|
||||||
from api.model.accounts import Account
|
from api.model.accounts import Account
|
||||||
from api.model.entries import Entry
|
from api.model.entries import Entry
|
||||||
@ -15,8 +14,9 @@ def get_accounts():
|
|||||||
"""
|
"""
|
||||||
Returns accounts with their solds.
|
Returns accounts with their solds.
|
||||||
"""
|
"""
|
||||||
|
session = db.create_scoped_session()
|
||||||
|
|
||||||
query=session.query(
|
query = session.query(
|
||||||
Account.id.label("id"),
|
Account.id.label("id"),
|
||||||
Account.name.label("name"),
|
Account.name.label("name"),
|
||||||
func.sum(Entry.value).label("future"),
|
func.sum(Entry.value).label("future"),
|
||||||
@ -34,7 +34,9 @@ def get_accounts():
|
|||||||
|
|
||||||
@app.route("/api/accounts/<account_id>/months")
|
@app.route("/api/accounts/<account_id>/months")
|
||||||
def get_months(account_id):
|
def get_months(account_id):
|
||||||
query=session.query(
|
session = db.create_scoped_session()
|
||||||
|
|
||||||
|
query = session.query(
|
||||||
distinct(extract("year", Entry.value_date)).label("year"),
|
distinct(extract("year", Entry.value_date)).label("year"),
|
||||||
extract("month", Entry.value_date).label("month")
|
extract("month", Entry.value_date).label("month")
|
||||||
).filter(Entry.account_id == account_id).order_by("year", "month")
|
).filter(Entry.account_id == account_id).order_by("year", "month")
|
||||||
@ -46,7 +48,8 @@ def get_months(account_id):
|
|||||||
|
|
||||||
@app.route("/api/accounts", methods=["PUT"])
|
@app.route("/api/accounts", methods=["PUT"])
|
||||||
def add_account():
|
def add_account():
|
||||||
with session.begin():
|
session = db.create_scoped_session()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
account = Account(request.json['name'])
|
account = Account(request.json['name'])
|
||||||
|
|
||||||
@ -61,7 +64,8 @@ def add_account():
|
|||||||
|
|
||||||
@app.route("/api/accounts/<account_id>", methods=["PUT"])
|
@app.route("/api/accounts/<account_id>", methods=["PUT"])
|
||||||
def update_account(account_id):
|
def update_account(account_id):
|
||||||
with session.begin():
|
session = db.create_scoped_session()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
account = session.query(Account).filter(Account.id == account_id).first()
|
account = session.query(Account).filter(Account.id == account_id).first()
|
||||||
|
|
||||||
@ -77,7 +81,8 @@ def update_account(account_id):
|
|||||||
|
|
||||||
@app.route("/api/accounts/<account_id>", methods=["DELETE"])
|
@app.route("/api/accounts/<account_id>", methods=["DELETE"])
|
||||||
def delete_account(account_id):
|
def delete_account(account_id):
|
||||||
with session.begin():
|
session = db.create_scoped_session()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
account = session.query(Account).filter(Account.id == account_id).first()
|
account = session.query(Account).filter(Account.id == account_id).first()
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
from app import app
|
from app import app
|
||||||
from app import db
|
from app import db
|
||||||
from app import session
|
|
||||||
|
|
||||||
from api.model.entries import Entry
|
from api.model.entries import Entry
|
||||||
|
|
||||||
@ -18,7 +17,9 @@ def get_entries(account_id, year, month):
|
|||||||
"""
|
"""
|
||||||
Return entries for an account, year, and month.
|
Return entries for an account, year, and month.
|
||||||
"""
|
"""
|
||||||
query=session.query(
|
session = db.create_scoped_session()
|
||||||
|
|
||||||
|
query = session.query(
|
||||||
Entry
|
Entry
|
||||||
).select_from(
|
).select_from(
|
||||||
session.query(Entry)
|
session.query(Entry)
|
||||||
@ -45,7 +46,8 @@ def get_entries(account_id, year, month):
|
|||||||
|
|
||||||
@app.route("/api/entries", methods=["PUT"])
|
@app.route("/api/entries", methods=["PUT"])
|
||||||
def add_entry():
|
def add_entry():
|
||||||
with session.begin():
|
session = db.create_scoped_session()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
entry = Entry(
|
entry = Entry(
|
||||||
value_date = request.json['value_date'],
|
value_date = request.json['value_date'],
|
||||||
@ -66,7 +68,8 @@ def add_entry():
|
|||||||
|
|
||||||
@app.route("/api/entries/<entry_id>", methods=["PUT"])
|
@app.route("/api/entries/<entry_id>", methods=["PUT"])
|
||||||
def update_entry(entry_id):
|
def update_entry(entry_id):
|
||||||
with session.begin():
|
session = db.create_scoped_session()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
||||||
|
|
||||||
@ -88,7 +91,8 @@ def update_entry(entry_id):
|
|||||||
|
|
||||||
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
|
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
|
||||||
def delete_entry(entry_id):
|
def delete_entry(entry_id):
|
||||||
with session.begin():
|
session = db.create_scoped_session()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
entry = session.query(Entry).filter(Entry.id == entry_id).first()
|
||||||
|
|
||||||
|
@ -9,5 +9,4 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://accountant:accountant@loca
|
|||||||
app.config['SQLALCHEMY_RECORD_QUERIES'] = True
|
app.config['SQLALCHEMY_RECORD_QUERIES'] = True
|
||||||
|
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
session = db.create_scoped_session()
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user