Commit a471ce06 authored by Zach Perkins's avatar Zach Perkins
Browse files

Added a decorator to make building endpoints easier

parent ef44a098
from flask import Flask, redirect, jsonify, abort
from where.model.field_types import FieldType
from where.model.sa import Category, Point, Field, session_context
from where.model.sa import Category, Point, Field, with_session
app = Flask(__name__)
......@@ -20,8 +20,8 @@ def index():
@app.route('/test_data')
def test_data():
with session_context() as session:
@with_session
def test_data(session):
# session = Session()
session.query(Point).delete()
session.query(Field).delete()
......@@ -70,8 +70,8 @@ def test_data():
@app.route('/category/<id>')
def get_category(id):
with session_context() as session:
@with_session
def get_category(session, id):
result = session.query(Category).filter_by(id=id).first()
if result:
return jsonify(result.as_json())
......@@ -80,14 +80,19 @@ def get_category(id):
@app.route('/point/<id>')
def get_point(id):
with session_context() as session:
@with_session
def get_point(session, id):
result = session.query(Point).filter_by(id=id).first()
if result:
return jsonify(result.as_json())
else:
abort(404)
@app.route('/point', methods=['POST'])
@with_session
def query_point():
pass
if __name__ == '__main__':
app.run()
......@@ -21,6 +21,16 @@ def session_context():
finally:
session.close()
# Decorator for convenience when building endpoints
def with_session(func):
def wrapper(*args, **kwargs):
with session_context() as session:
func(session, *args, **kwargs)
# Flask identifies endpoint handlers based on their name
wrapper.__name__ = func.__name__
return wrapper
@as_declarative()
class Base(object):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment