/
server.py
110 lines (80 loc) · 2.86 KB
/
server.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from geopy import geocoders
from bson import json_util
import json
import pymongo
from bson.objectid import ObjectId
from flask import Flask, render_template, request, make_response, jsonify
app = Flask(__name__)
app.debug = True
@app.route('/')
def index():
return render_template('index.html')
@app.route('/locations/', methods=['GET'])
def get_locations():
location_collection = get_collection()
locations = location_collection.find().sort('order', pymongo.ASCENDING)
data = []
for l in locations:
# Fix for funky ObjectID - otherwise things blow up
l['id'] = str(l['_id'])
del l['_id']
data.append(l)
resp = make_response(json.dumps(data, default=json_util.default))
resp.status_code = 200
resp.mimetype = 'application/json'
return resp
@app.route('/locations/<location_id>', methods=['GET'])
def get_location(location_id):
oid = None
try:
oid = ObjectId(location_id)
except:
return jsonify(message='invalid id')
locations = get_collection()
l = locations.find_one({'_id': oid})
if l is None:
return jsonify(message='no todo with id: ' + location_id)
# Fix for funky ObjectID
l['id'] = str(l['_id'])
del l['_id']
return jsonify(address=l['address'], nickname=l['nickname'], latitude=l['latitude'],
longitude=l['longitude'], id=l['id'], order=l['order'])
@app.route('/locations/', methods=['POST'])
def create_location():
data = request.json
address = data['address']
# Send posted address to google geocoding service
g = geocoders.Google('api-goes-here')
(place, (latitude, longitude)) = g.geocode(address)
data['address'] = place
data['latitude'] = latitude
data['longitude'] = longitude
# fix for missing order in post requests from curl
if 'order' in data:
print "There's order"
else:
data['order'] = 0
locations = get_collection()
oid = locations.insert(data)
location = locations.find_one({'_id': ObjectId(oid)})
location['id'] = str(location['_id'])
del location['_id']
return jsonify(address=address, nickname=data['nickname'], latitude=latitude, longitude=longitude,
id=location['id'], order=data['order'])
@app.route('/locations/<location_id>', methods=['PUT'])
def update_location(location_id):
data = request.json
locations = get_collection()
locations.update({'_id': ObjectId(location_id)}, {'$set': data})
return jsonify(message='OK')
@app.route('/locations/<location_id>', methods=['DELETE'])
def delete_location(location_id):
locations = get_collection()
locations.remove(ObjectId(location_id))
return jsonify(message='OK')
def get_collection():
conn = pymongo.Connection('localhost:27017')
return conn[app.db_name].locations
if __name__ == '__main__':
app.db_name = 'locations_prod'
app.run(host='0.0.0.0')