Fixed (really) the session management.
This commit is contained in:
		| @@ -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,6 +14,7 @@ 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"), | ||||||
| @@ -34,6 +34,8 @@ 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): | ||||||
|  |     session = db.create_scoped_session() | ||||||
|  |  | ||||||
|     query = session.query( |     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") | ||||||
| @@ -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,6 +17,8 @@ def get_entries(account_id, year, month): | |||||||
|     """ |     """ | ||||||
|     Return entries for an account, year, and month. |     Return entries for an account, year, and month. | ||||||
|     """ |     """ | ||||||
|  |     session = db.create_scoped_session() | ||||||
|  |  | ||||||
|     query = session.query( |     query = session.query( | ||||||
|       Entry |       Entry | ||||||
|     ).select_from( |     ).select_from( | ||||||
| @@ -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() |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user