Commit ae1e43f5 authored by Zach Perkins's avatar Zach Perkins

waddup REST

parent 149ce9f2
from flask import Flask, redirect, jsonify, abort, request
from flask import Flask, redirect, jsonify, abort, request, url_for, Response, make_response
from werkzeug.datastructures import Headers
from where.model.field_types import FieldType
from where.model.sa import Category, Point, Field, with_session
......@@ -121,20 +122,40 @@ def get_point(session, id):
abort(404)
@app.route('/add-point')
@app.route('/add-point', methods=['POST'])
@with_session
def add_point(session):
allowed_params = {'name', 'lat', 'lon', 'attributes', 'category_id', 'parent_id'}
data = request.get_json()
data = {key:val for key, val in data.items() if key in allowed_params}
# TODO: For some reason point.category is NULL when we do validation, even though the category ID is present
# this is causing an exception whenever a non-null attributes object is passed
point = Point(category_id=data.pop('category_id'))
for key, val in data.items():
setattr(point, key, val)
session.add(point)
session.commit()
response = make_response(jsonify(point.as_json()), 201)
response.headers['Location'] = url_for('get_point', id=point.id)
return response
@app.route('/point', methods=['GET'])
@with_session
def search_points(session):
q = session.query(Point)
if 'category' in request.args:
q = q.filter(Point.category_id == request.args.get('category'))
if 'category_id' in request.args:
q = q.filter(Point.category_id == request.args.get('category_id'))
if 'parent' in request.args:
q = q.filter(Point.parent_id == request.args.get('parent'))
if 'parent_id' in request.args:
q = q.filter(Point.parent_id == request.args.get('parent_id'))
return jsonify(list(map(lambda p: p.as_json(), q.limit(100).all())))
if __name__ == '__main__':
app.run()
\ No newline at end of file
......@@ -59,7 +59,9 @@ class Point(Base):
def validate_data(self, _, data):
if data is None:
return
fields = self.category.fields
for key in data:
# Find Field object that corresponds to this key
for field in fields:
......
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