Commit 750ba0f1 authored by Zach Perkins's avatar Zach Perkins

Added some more test cases and made a basic function to search points

parent a471ce06
from flask import Flask, redirect, jsonify, abort from flask import Flask, redirect, jsonify, abort, request
from where.model.field_types import FieldType from where.model.field_types import FieldType
from where.model.sa import Category, Point, Field, with_session from where.model.sa import Category, Point, Field, with_session
...@@ -32,6 +32,21 @@ def test_data(session): ...@@ -32,6 +32,21 @@ def test_data(session):
wf.icon = "https://karel.pw/water.png" wf.icon = "https://karel.pw/water.png"
session.add(wf) session.add(wf)
session.commit() session.commit()
# Building
bd = Category()
bd.name = "Building"
bd.icon = "https://hips.hearstapps.com/hmg-prod.s3.amazonaws.com/images/basket-building-news-photo-1572015168.jpg?resize=980:*"
session.add(bd)
session.commit()
# Radius (Really the simplest metric we can have for building size)
rd = Field()
rd.name = "Radius"
rd.slug = "radius"
rd.type = FieldType.FLOAT
rd.category_id = bd.id
session.add(rd)
session.commit()
# coldness # coldness
cd = Field() cd = Field()
cd.name = "Coldness" cd.name = "Coldness"
...@@ -47,14 +62,29 @@ def test_data(session): ...@@ -47,14 +62,29 @@ def test_data(session):
session.add(cd) session.add(cd)
session.add(fl) session.add(fl)
session.commit() session.commit()
# an actual instance!
# The johnson center
jc = Point()
jc.category = bd
jc.name = "Johnson Center"
jc.lat = 38
jc.lon = -77
jc.parent = None
jc.attributes = {
"radius": {
"value": 2.0
}
}
session.add(jc)
# A water fountain inside the JC
fn = Point() fn = Point()
fn.name = None fn.name = None
fn.lat = 38.829791 fn.lat = 38.829791
fn.lon = -77.307043 fn.lon = -77.307043
# fn.category_id = wf.id # fn.category_id = wf.id
fn.category = wf fn.category = wf
fn.parent_id = None fn.parent = jc
fn.attributes = { fn.attributes = {
"coldness": { "coldness": {
"num_reviews": 32, "num_reviews": 32,
...@@ -64,6 +94,8 @@ def test_data(session): ...@@ -64,6 +94,8 @@ def test_data(session):
"value": True "value": True
} }
} }
session.add(fn) session.add(fn)
session.commit() session.commit()
return redirect('/') return redirect('/')
...@@ -88,10 +120,21 @@ def get_point(session, id): ...@@ -88,10 +120,21 @@ def get_point(session, id):
else: else:
abort(404) abort(404)
@app.route('/point', methods=['POST']) @app.route('/query_points', methods=['POST'])
@with_session @with_session
def query_point(): def query_points(session):
pass data = request.get_json()
q = session.query(Point)
if 'category' in data:
q = q.filter(Point.category_id == data['category'])
if 'parent' in data:
q = q.filter(Point.parent_id == data['parent'])
return jsonify(list(map(lambda p: p.as_json(), q.all())))
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -6,7 +6,7 @@ from sqlalchemy.orm import relationship, validates ...@@ -6,7 +6,7 @@ from sqlalchemy.orm import relationship, validates
from sqlalchemy.schema import Column from sqlalchemy.schema import Column
from .field_types import FieldType from .field_types import FieldType
from .meta import Session from .meta import Session, engine
@contextmanager @contextmanager
...@@ -25,7 +25,7 @@ def session_context(): ...@@ -25,7 +25,7 @@ def session_context():
def with_session(func): def with_session(func):
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
with session_context() as session: with session_context() as session:
func(session, *args, **kwargs) return func(session, *args, **kwargs)
# Flask identifies endpoint handlers based on their name # Flask identifies endpoint handlers based on their name
wrapper.__name__ = func.__name__ wrapper.__name__ = func.__name__
...@@ -130,3 +130,6 @@ class Field(Base): ...@@ -130,3 +130,6 @@ class Field(Base):
"name": self.name, "name": self.name,
"type": self.type.name "type": self.type.name
} }
# uh putting this here doesn't feel right but there's not anything *too* wrong with it
Base.metadata.create_all(engine)
\ No newline at end of file
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