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 import Category, Point, Field, session_context
from import Category, Point, Field, with_session
app = Flask(__name__)
......@@ -20,8 +20,8 @@ def index():
def test_data():
with session_context() as session:
def test_data(session):
# session = Session()
......@@ -70,8 +70,8 @@ def test_data():
def get_category(id):
with session_context() as 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):
def get_point(id):
with session_context() as session:
def get_point(session, id):
result = session.query(Point).filter_by(id=id).first()
if result:
return jsonify(result.as_json())
@app.route('/point', methods=['POST'])
def query_point():
if __name__ == '__main__':
......@@ -21,6 +21,16 @@ def session_context():
# 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
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