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
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
from app import app
from app import db
from api.model.accounts import Account
from api.model.entries import Entry
from api.model.operations import Operation
from .. import api
from ..model import db
from ..model.accounts import Account
from ..model.entries import Entry
from ..model.operations import Operation
from flask import json, request
from sqlalchemy import func, case, cast, extract, distinct
@app.route("/api/accounts", methods=["GET"])
@api.route("/accounts", methods=["GET"])
def get_accounts():
"""
Returns accounts with their solds.
@ -50,7 +47,7 @@ def get_accounts():
"future": str(i.future)
} 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):
session = db.session
@ -80,7 +77,7 @@ def get_account_status(account_id, year, month):
"balance": str(balance)
})
@app.route("/api/accounts/<account_id>/months")
@api.route("/accounts/<account_id>/months")
def get_months(account_id):
session = db.session
@ -94,7 +91,7 @@ def get_months(account_id):
"month": i.month.rjust(2, '0')
} for i in query.all()])
@app.route("/api/accounts", methods=["PUT"])
@api.route("/accounts", methods=["PUT"])
def add_account():
session = db.session
@ -110,7 +107,7 @@ def add_account():
raise
@app.route("/api/accounts/<account_id>", methods=["PUT"])
@api.route("/accounts/<account_id>", methods=["PUT"])
def update_account(account_id):
session = db.session
@ -128,7 +125,7 @@ def update_account(account_id):
session.rollback()
raise
@app.route("/api/accounts/<account_id>", methods=["DELETE"])
@api.route("/accounts/<account_id>", methods=["DELETE"])
def delete_account(account_id):
session = db.session

View File

@ -14,23 +14,18 @@
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 app import db
from api.model.entries import Entry
from api.model.operations import Operation
from api.model.scheduled_operations import ScheduledOperation
from .. import api
from ..model import db
from ..model.entries import Entry
from ..model.operations import Operation
from ..model.scheduled_operations import ScheduledOperation
from flask import json, request
from sqlalchemy import func, desc
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
from sqlalchemy.orm import sessionmaker, column_property, aliased
from sqlalchemy.sql import func, select, case
#from sqlalchemy import *
from flask import json, request
@app.route("/api/entries/<account_id>/<year>/<month>")
@api.route("/entries/<account_id>/<year>/<month>")
def get_entries(account_id, year, 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
} for i in query.all()])
@app.route("/api/entries", methods=["PUT"])
@api.route("/entries", methods=["PUT"])
def add_entry():
session = db.session
@ -84,7 +79,7 @@ def add_entry():
session.rollback()
raise
@app.route("/api/entries/<entry_id>", methods=["PUT"])
@api.route("/entries/<entry_id>", methods=["PUT"])
def update_entry(entry_id):
session = db.session
@ -108,7 +103,7 @@ def update_entry(entry_id):
session.rollback()
raise
@app.route("/api/entries/<entry_id>", methods=["DELETE"])
@api.route("/entries/<entry_id>", methods=["DELETE"])
def delete_entry(entry_id):
session = db.session

View File

@ -1,16 +1,13 @@
from app import app
from app import db
from api.model.scheduled_operations import ScheduledOperation
from .. import api
from ..model import db
from ..model.scheduled_operations import ScheduledOperation
from flask import json, request
from sqlalchemy import func, desc
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
from sqlalchemy.orm import sessionmaker, column_property
from sqlalchemy.sql import func, select
from flask import json, request
@app.route("/api/scheduled_operations/<account_id>")
@api.route("/scheduled_operations/<account_id>")
def get_scheduled_operations(account_id):
"""
Return entries for an account, year, and month.
@ -41,7 +38,7 @@ def get_scheduled_operations(account_id):
"account_id": i.account_id
} for i in query.all()])
@app.route("/api/scheduled_operations", methods=["PUT"])
@api.route("/scheduled_operations", methods=["PUT"])
def add_scheduled_operation():
session = db.session
@ -65,7 +62,7 @@ def add_scheduled_operation():
session.rollback()
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):
session = db.session
@ -90,7 +87,7 @@ def update_scheduled_operation(scheduled_operation_id):
session.rollback()
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):
session = db.session

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
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
from app import app
from app import db
from . import db
class Account(db.Model):
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
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
from app import app
from app import db
from api.model.accounts import Account
from api.model.scheduled_operations import ScheduledOperation
from . import db
from .accounts import Account
from .scheduled_operations import ScheduledOperation
from sqlalchemy import func, desc
from sqlalchemy.orm import column_property
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
along with Accountant. If not, see <http://www.gnu.org/licenses/>.
"""
from app import app
from app import db
from . import db
from api.model.accounts import Account
from api.model.scheduled_operations import ScheduledOperation
from .accounts import Account
from .scheduled_operations import ScheduledOperation
from sqlalchemy import func, desc
from sqlalchemy.orm import column_property

View File

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

10
app.py
View File

@ -18,13 +18,19 @@
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.orm import sessionmaker
from frontend import frontend
from api import api
from api.model import db
import config
# The app
app = Flask(__name__)
app = Flask(__name__, static_folder = None)
app.config['SQLALCHEMY_DATABASE_URI'] = config.db_uri
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()