app.py 3.9 KB
Newer Older
1
from flask import Flask, redirect, jsonify, abort, request, url_for, make_response
Dylan Jones's avatar
no slug    
Dylan Jones committed
2

3
from where.model import with_session, Point, Category, Field
Dylan Jones's avatar
Dylan Jones committed
4
from where.model.field_types import FieldType
Dylan Jones's avatar
app.py    
Dylan Jones committed
5
6
7

app = Flask(__name__)

8

Dylan Jones's avatar
app.py    
Dylan Jones committed
9
10
@app.route('/')
def index():
Dylan Jones's avatar
no slug    
Dylan Jones committed
11
12
13
14
15
16
17
18
19
20
21
22
    return """
<head>
</head>
<body>
    <h1>W H E R E</h1>
    <p>This is the WHERE project.</p>
    <a href='/test_data'>Click here to nuke the database and make it all be test data.</a>
</body>
    """


@app.route('/test_data')
23
24
25
26
27
28
29
30
31
32
33
@with_session
def test_data(session):
    session.query(Point).delete()
    session.query(Field).delete()
    session.query(Category).delete()
    # Water Fountain, the class.
    wf = Category()
    wf.name = "Water Fountain"
    wf.icon = "https://karel.pw/water.png"
    session.add(wf)
    session.commit()
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
    # 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()

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    # coldness
    cd = Field()
    cd.name = "Coldness"
    cd.slug = "coldness"
    cd.type = FieldType.RATING
    cd.category_id = wf.id
    # filler
    fl = Field()
    fl.slug = "bottle_filler"
    fl.name = "Has Bottle Filler"
    fl.type = FieldType.BOOLEAN
    fl.category_id = wf.id
    session.add(cd)
    session.add(fl)
    session.commit()
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    # 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
80
81
82
83
84
85
    fn = Point()
    fn.name = None
    fn.lat = 38.829791
    fn.lon = -77.307043
    # fn.category_id = wf.id
    fn.category = wf
86
    fn.parent = jc
87
88
89
90
91
92
93
    fn.attributes = {
        "coldness": {
            "num_reviews": 32,
            "average_rating": 0.5
        },
        "bottle_filler": {
            "value": True
Dylan Jones's avatar
Dylan Jones committed
94
        }
95
    }
96

97
98
99
    session.add(fn)
    session.commit()
    return redirect('/')
Dylan Jones's avatar
Dylan Jones committed
100
101
102


@app.route('/category/<id>')
103
104
105
106
107
108
109
@with_session
def get_category(session, id):
    result = session.query(Category).filter_by(id=id).first()
    if result:
        return jsonify(result.as_json())
    else:
        abort(404)
Dylan Jones's avatar
Dylan Jones committed
110
111
112


@app.route('/point/<id>')
113
114
115
116
117
118
119
@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)
Dylan Jones's avatar
app.py    
Dylan Jones committed
120

Zach Perkins's avatar
Zach Perkins committed
121

Zach Perkins's avatar
Zach Perkins committed
122
123
124
125
126
@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()
127
128
    data = {key: val for key, val in data.items() if key in allowed_params}

Zach Perkins's avatar
Zach Perkins committed
129
130
131
132
133
134
135
136
137
138
139
140
141
    # 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

Zach Perkins's avatar
Zach Perkins committed
142
143

@app.route('/point', methods=['GET'])
144
@with_session
Zach Perkins's avatar
Zach Perkins committed
145
def search_points(session):
146
    q = session.query(Point)
147

Zach Perkins's avatar
Zach Perkins committed
148
149
    if 'category_id' in request.args:
        q = q.filter(Point.category_id == request.args.get('category_id'))
150

Zach Perkins's avatar
Zach Perkins committed
151
152
    if 'parent_id' in request.args:
        q = q.filter(Point.parent_id == request.args.get('parent_id'))
153

Zach Perkins's avatar
Zach Perkins committed
154
    return jsonify(list(map(lambda p: p.as_json(), q.limit(100).all())))
Dylan Jones's avatar
app.py    
Dylan Jones committed
155

Zach Perkins's avatar
Zach Perkins committed
156

Dylan Jones's avatar
app.py    
Dylan Jones committed
157
if __name__ == '__main__':
158
    app.run()