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

Dylan Jones's avatar
Dylan Jones committed
3
from where.model.field_types import FieldType
Dylan Jones's avatar
Dylan Jones committed
4
from where.model.sa import Category, Point, Field, session_context
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
23
    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')
def test_data():
Dylan Jones's avatar
Dylan Jones committed
24
    with session_context() as session:
Dylan Jones's avatar
Dylan Jones committed
25
        # session = Session()
Dylan Jones's avatar
Dylan Jones committed
26
27
28
        session.query(Point).delete()
        session.query(Field).delete()
        session.query(Category).delete()
Dylan Jones's avatar
Dylan Jones committed
29
        # Water Fountain, the class.
Dylan Jones's avatar
Dylan Jones committed
30
        wf = Category()
Dylan Jones's avatar
no slug    
Dylan Jones committed
31
        wf.name = "Water Fountain"
Dylan Jones's avatar
Dylan Jones committed
32
33
34
        wf.icon = "https://karel.pw/water.png"
        session.add(wf)
        session.commit()
Dylan Jones's avatar
Dylan Jones committed
35
        # coldness
Dylan Jones's avatar
Dylan Jones committed
36
        cd = Field()
Dylan Jones's avatar
Dylan Jones committed
37
        cd.name = "Coldness"
Dylan Jones's avatar
Dylan Jones committed
38
        cd.slug = "coldness"
Dylan Jones's avatar
Dylan Jones committed
39
40
        cd.type = FieldType.RATING
        cd.category_id = wf.id
Dylan Jones's avatar
Dylan Jones committed
41
        # filler
Dylan Jones's avatar
Dylan Jones committed
42
        fl = Field()
Dylan Jones's avatar
Dylan Jones committed
43
44
45
46
47
48
49
50
        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()
        # an actual instance!
Dylan Jones's avatar
Dylan Jones committed
51
        fn = Point()
Dylan Jones's avatar
Dylan Jones committed
52
        fn.name = None
Dylan Jones's avatar
Dylan Jones committed
53
54
55
56
        fn.lat = 38.829791
        fn.lon = -77.307043
        # fn.category_id = wf.id
        fn.category = wf
Dylan Jones's avatar
Dylan Jones committed
57
58
59
60
61
62
63
64
65
66
67
68
        fn.parent_id = None
        fn.attributes = {
            "coldness": {
                "num_reviews": 32,
                "average_rating": 0.5
            },
            "bottle_filler": {
                "value": True
            }
        }
        session.add(fn)
        session.commit()
Dylan Jones's avatar
Dylan Jones committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
        return redirect('/')


@app.route('/category/<id>')
def get_category(id):
    with session_context() as session:
        result = session.query(Category).filter_by(id=id).first()
        if result:
            return jsonify(result.as_json())
        else:
            abort(404)


@app.route('/point/<id>')
def get_point(id):
    with session_context() as session:
        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
90
91
92
93


if __name__ == '__main__':
    app.run()