Finished refactoring.

This commit is contained in:
Alexis Lahouze 2013-12-03 22:22:25 +01:00
parent b2219b847a
commit 9a94d939b8
13 changed files with 118 additions and 118 deletions

View File

@ -0,0 +1,6 @@
from flask import Blueprint
api = Blueprint('api', __name__)
from .controller import *

View File

@ -0,0 +1,9 @@
import pkgutil
__all__ = []
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
__all__.append(module_name)
from . import *

View File

@ -14,18 +14,15 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>. along with Accountant. If not, see <http://www.gnu.org/licenses/>.
""" """
from app import app from .. import api
from app import db from ..model import db
from ..model.accounts import Account
from api.model.accounts import Account from ..model.entries import Entry
from api.model.entries import Entry from ..model.operations import Operation
from api.model.operations import Operation
from flask import json, request from flask import json, request
from sqlalchemy import func, case, cast, extract, distinct from sqlalchemy import func, case, cast, extract, distinct
@app.route("/api/accounts", methods=["GET"]) @api.route("/accounts", methods=["GET"])
def get_accounts(): def get_accounts():
""" """
Returns accounts with their solds. Returns accounts with their solds.
@ -50,7 +47,7 @@ def get_accounts():
"future": str(i.future) "future": str(i.future)
} for i in query.all()]) } for i in query.all()])
@app.route("/api/accounts/<account_id>/<year>/<month>/") @api.route("/accounts/<account_id>/<year>/<month>/")
def get_account_status(account_id, year, month): def get_account_status(account_id, year, month):
session = db.session session = db.session
@ -80,7 +77,7 @@ def get_account_status(account_id, year, month):
"balance": str(balance) "balance": str(balance)
}) })
@app.route("/api/accounts/<account_id>/months") @api.route("/accounts/<account_id>/months")
def get_months(account_id): def get_months(account_id):
session = db.session session = db.session
@ -94,7 +91,7 @@ def get_months(account_id):
"month": i.month.rjust(2, '0') "month": i.month.rjust(2, '0')
} for i in query.all()]) } for i in query.all()])
@app.route("/api/accounts", methods=["PUT"]) @api.route("/accounts", methods=["PUT"])
def add_account(): def add_account():
session = db.session session = db.session
@ -110,7 +107,7 @@ def add_account():
raise raise
@app.route("/api/accounts/<account_id>", methods=["PUT"]) @api.route("/accounts/<account_id>", methods=["PUT"])
def update_account(account_id): def update_account(account_id):
session = db.session session = db.session
@ -128,16 +125,16 @@ def update_account(account_id):
session.rollback() session.rollback()
raise raise
@app.route("/api/accounts/<account_id>", methods=["DELETE"]) @api.route("/accounts/<account_id>", methods=["DELETE"])
def delete_account(account_id): def delete_account(account_id):
session = db.session session = db.session
try: try:
account = session.query(Account).filter(Account.id == account_id).first() account = session.query(Account).filter(Account.id == account_id).first()
session.delete(account) session.delete(account)
session.commit() session.commit()
return json.dumps("Account #%s deleted." % account_id) return json.dumps("Account #%s deleted." % account_id)
except: except:
session.rollback() session.rollback()

View File

@ -14,23 +14,18 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>. along with Accountant. If not, see <http://www.gnu.org/licenses/>.
""" """
from app import app from .. import api
from app import db from ..model import db
from ..model.entries import Entry
from api.model.entries import Entry from ..model.operations import Operation
from api.model.operations import Operation from ..model.scheduled_operations import ScheduledOperation
from api.model.scheduled_operations import ScheduledOperation from flask import json, request
from sqlalchemy import func, desc from sqlalchemy import func, desc
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
from sqlalchemy.orm import sessionmaker, column_property, aliased from sqlalchemy.orm import sessionmaker, column_property, aliased
from sqlalchemy.sql import func, select, case from sqlalchemy.sql import func, select, case
#from sqlalchemy import * @api.route("/entries/<account_id>/<year>/<month>")
from flask import json, request
@app.route("/api/entries/<account_id>/<year>/<month>")
def get_entries(account_id, year, month): def get_entries(account_id, year, month):
""" """
Return entries for an account, year, and month. Return entries for an account, year, and month.
@ -61,7 +56,7 @@ def get_entries(account_id, year, month):
"scheduled_operation_id": i.scheduled_operation_id "scheduled_operation_id": i.scheduled_operation_id
} for i in query.all()]) } for i in query.all()])
@app.route("/api/entries", methods=["PUT"]) @api.route("/entries", methods=["PUT"])
def add_entry(): def add_entry():
session = db.session session = db.session
@ -75,22 +70,22 @@ def add_entry():
account_id = request.json['account_id'], account_id = request.json['account_id'],
scheduled_operation_id = request.json['scheduled_operation_id'] scheduled_operation_id = request.json['scheduled_operation_id']
) )
session.add(entry) session.add(entry)
session.commit() session.commit()
return json.dumps("Entry added.") return json.dumps("Entry added.")
except: except:
session.rollback() session.rollback()
raise raise
@app.route("/api/entries/<entry_id>", methods=["PUT"]) @api.route("/entries/<entry_id>", methods=["PUT"])
def update_entry(entry_id): def update_entry(entry_id):
session = db.session session = db.session
try: try:
entry = session.query(Entry).filter(Entry.id == entry_id).first() entry = session.query(Entry).filter(Entry.id == entry_id).first()
entry.id = entry_id entry.id = entry_id
entry.operation_date = request.json['operation_date'] entry.operation_date = request.json['operation_date']
entry.pointed = request.json['pointed'] entry.pointed = request.json['pointed']
@ -99,25 +94,25 @@ def update_entry(entry_id):
entry.category = request.json['category'] entry.category = request.json['category']
entry.account_id = request.json['account_id'] entry.account_id = request.json['account_id']
entry.scheduled_operation_id = request.json['scheduled_operation_id'] entry.scheduled_operation_id = request.json['scheduled_operation_id']
session.merge(entry) session.merge(entry)
session.commit() session.commit()
return json.dumps("Entry #%s updated." % entry_id) return json.dumps("Entry #%s updated." % entry_id)
except: except:
session.rollback() session.rollback()
raise raise
@app.route("/api/entries/<entry_id>", methods=["DELETE"]) @api.route("/entries/<entry_id>", methods=["DELETE"])
def delete_entry(entry_id): def delete_entry(entry_id):
session = db.session session = db.session
try: try:
entry = session.query(Entry).filter(Entry.id == entry_id).first() entry = session.query(Entry).filter(Entry.id == entry_id).first()
session.delete(entry) session.delete(entry)
session.commit() session.commit()
return json.dumps("Entry #%s deleted." % entry_id) return json.dumps("Entry #%s deleted." % entry_id)
except: except:
session.rollback() session.rollback()

View File

@ -1,16 +1,13 @@
from app import app from .. import api
from app import db from ..model import db
from ..model.scheduled_operations import ScheduledOperation
from api.model.scheduled_operations import ScheduledOperation from flask import json, request
from sqlalchemy import func, desc from sqlalchemy import func, desc
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
from sqlalchemy.orm import sessionmaker, column_property from sqlalchemy.orm import sessionmaker, column_property
from sqlalchemy.sql import func, select from sqlalchemy.sql import func, select
from flask import json, request @api.route("/scheduled_operations/<account_id>")
@app.route("/api/scheduled_operations/<account_id>")
def get_scheduled_operations(account_id): def get_scheduled_operations(account_id):
""" """
Return entries for an account, year, and month. Return entries for an account, year, and month.
@ -41,7 +38,7 @@ def get_scheduled_operations(account_id):
"account_id": i.account_id "account_id": i.account_id
} for i in query.all()]) } for i in query.all()])
@app.route("/api/scheduled_operations", methods=["PUT"]) @api.route("/scheduled_operations", methods=["PUT"])
def add_scheduled_operation(): def add_scheduled_operation():
session = db.session session = db.session
@ -56,22 +53,22 @@ def add_scheduled_operation():
category = request.json['category'], category = request.json['category'],
account_id = request.json['account_id'] account_id = request.json['account_id']
) )
session.add(scheduledOperation) session.add(scheduledOperation)
session.commit() session.commit()
return json.dumps("Scheduled operation added.") return json.dumps("Scheduled operation added.")
except: except:
session.rollback() session.rollback()
raise raise
@app.route("/api/scheduled_operations/<scheduled_operation_id>", methods=["PUT"]) @api.route("/scheduled_operations/<scheduled_operation_id>", methods=["PUT"])
def update_scheduled_operation(scheduled_operation_id): def update_scheduled_operation(scheduled_operation_id):
session = db.session session = db.session
try: try:
scheduledOperation = session.query(ScheduledOperation).filter(ScheduledOperation.id == scheduled_operation_id).first() scheduledOperation = session.query(ScheduledOperation).filter(ScheduledOperation.id == scheduled_operation_id).first()
scheduledOperation.id = scheduled_operation_id scheduledOperation.id = scheduled_operation_id
scheduledOperation.start_date = request.json['start_date'], scheduledOperation.start_date = request.json['start_date'],
scheduledOperation.stop_date = request.json['stop_date'], scheduledOperation.stop_date = request.json['stop_date'],
@ -81,25 +78,25 @@ def update_scheduled_operation(scheduled_operation_id):
scheduledOperation.value = request.json['value'] scheduledOperation.value = request.json['value']
scheduledOperation.category = request.json['category'] scheduledOperation.category = request.json['category']
scheduledOperation.account_id = request.json['account_id'] scheduledOperation.account_id = request.json['account_id']
session.merge(scheduledOperation) session.merge(scheduledOperation)
session.commit() session.commit()
return json.dumps("Scheduled operation #%s updated." % scheduled_operation_id) return json.dumps("Scheduled operation #%s updated." % scheduled_operation_id)
except: except:
session.rollback() session.rollback()
raise raise
@app.route("/api/scheduled_operations/<scheduled_operation_id>", methods=["DELETE"]) @api.route("/scheduled_operations/<scheduled_operation_id>", methods=["DELETE"])
def delete_scheduled_operation(scheduled_operation_id): def delete_scheduled_operation(scheduled_operation_id):
session = db.session session = db.session
try: try:
scheduledOperation = session.query(ScheduledOperation).filter(ScheduledOperation.id == scheduled_operation_id).first() scheduledOperation = session.query(ScheduledOperation).filter(ScheduledOperation.id == scheduled_operation_id).first()
session.delete(scheduledOperation) session.delete(scheduledOperation)
session.commit() session.commit()
return json.dumps("Scheduled operation #%s deleted." % scheduled_operation_id) return json.dumps("Scheduled operation #%s deleted." % scheduled_operation_id)
except: except:
session.rollback() session.rollback()

View File

@ -0,0 +1,31 @@
from contextlib import contextmanager
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
db = SQLAlchemy()
@contextmanager
def session_scope(engine):
if engine:
session = scoped_session(sessionmaker(autocommit = False, autoflush = False, bind = engine))
#Base.query = session.query_property()
try:
yield session
session.commit()
except:
session.rollback()
raise
finally:
session.close()
import pkgutil
__all__ = []
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
__all__.append(module_name)
from . import *

View File

@ -14,8 +14,7 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>. along with Accountant. If not, see <http://www.gnu.org/licenses/>.
""" """
from app import app from . import db
from app import db
class Account(db.Model): class Account(db.Model):
id = db.Column(db.Integer, primary_key = True) id = db.Column(db.Integer, primary_key = True)

View File

@ -14,12 +14,9 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>. along with Accountant. If not, see <http://www.gnu.org/licenses/>.
""" """
from app import app from . import db
from app import db from .accounts import Account
from .scheduled_operations import ScheduledOperation
from api.model.accounts import Account
from api.model.scheduled_operations import ScheduledOperation
from sqlalchemy import func, desc from sqlalchemy import func, desc
from sqlalchemy.orm import column_property from sqlalchemy.orm import column_property
from sqlalchemy.sql import func, select from sqlalchemy.sql import func, select

View File

@ -14,11 +14,10 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>. along with Accountant. If not, see <http://www.gnu.org/licenses/>.
""" """
from app import app from . import db
from app import db
from api.model.accounts import Account from .accounts import Account
from api.model.scheduled_operations import ScheduledOperation from .scheduled_operations import ScheduledOperation
from sqlalchemy import func, desc from sqlalchemy import func, desc
from sqlalchemy.orm import column_property from sqlalchemy.orm import column_property

View File

@ -1,8 +1,5 @@
from app import app from . import db
from app import db from .accounts import Account
from api.model.accounts import Account
from sqlalchemy import func, desc from sqlalchemy import func, desc
from sqlalchemy.orm import column_property from sqlalchemy.orm import column_property
from sqlalchemy.sql import func, select from sqlalchemy.sql import func, select

10
app.py
View File

@ -18,13 +18,19 @@
from flask import Flask from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
from frontend import frontend
from api import api
from api.model import db
import config import config
# The app # The app
app = Flask(__name__) app = Flask(__name__, static_folder = None)
app.config['SQLALCHEMY_DATABASE_URI'] = config.db_uri app.config['SQLALCHEMY_DATABASE_URI'] = config.db_uri
app.config['SQLALCHEMY_RECORD_QUERIES'] = config.debug app.config['SQLALCHEMY_RECORD_QUERIES'] = config.debug
db = SQLAlchemy(app) db.init_app(app)
app.register_blueprint(frontend, url_prefix='')
app.register_blueprint(api, url_prefix='/api')

43
main.py
View File

@ -1,43 +0,0 @@
"""
This file is part of Accountant.
Accountant is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Foobar is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
from app import app
from api.controller.entries import *
from api.controller.accounts import *
from api.controller.scheduled_operations import *
from flask import redirect, render_template, jsonify
@app.route('/')
def root():
return redirect('index.html')
@app.route('/index.html')
def index():
return render_template('index.html')
@app.route('/scheduler.html')
def scheduler():
return render_template('scheduler.html')
@app.errorhandler(BaseException)
def default_errorhandler(error):
return jsonify(title="Error", text="Error %s" % str(error)), 500
if __name__ == '__main__':
app.run(debug=True)

10
manage.py Normal file
View File

@ -0,0 +1,10 @@
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from app import app
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == "__main__":
manager.run()