-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.py
47 lines (40 loc) · 1.55 KB
/
dashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from __future__ import print_function, unicode_literals
from flask import Flask, make_response
import rethinkdb as r
import json
app = Flask(__name__, static_url_path="/static")
@app.route("/")
def root():
return app.send_static_file('index.html')
@app.route("/users")
@app.route('/users/<user_id>')
def user(user_id):
conn = r.connect(host='localhost', port=28015, db="users_dashboard")
if user_id == "":
users = r.table("users")\
.has_fields("geo_point")\
.merge(lambda d: {'coords': d['geo_point'].to_geojson()['coordinates']})\
.pluck("coords", "id", "login", "location")\
.coerce_to("array")\
.run(conn)
json_string = json.dumps(dict(
users = users
))
else:
user = r.table("users")\
.get(user_id)\
.merge(lambda d: {'coords': d['geo_point'].to_geojson()['coordinates']})\
.pluck("coords", "id", "login", "location")\
.run(conn)
json_string = json.dumps(dict(
user = user
))
response = make_response(json_string)
response.headers['Content-Type'] = "application/json"
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Headers'] = 'X-AUTH-TOKEN, X-API-VERSION, X-Requested-With, Content-Type, Accept, Origin'
response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE, OPTIONS'
response.headers['Access-Control-Max-Age'] = "1728000"
return response
if __name__ == '__main__':
app.run(debug=True)