-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.py
46 lines (33 loc) · 1.02 KB
/
app.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
import pymongo
from bson.objectid import ObjectId
import json_util
from flask import Flask, render_template, url_for, json, request
app = Flask(__name__)
connection = pymongo.Connection('localhost', 27017)
todos = connection['demo']['todos']
def json_load(data):
return json.loads(data, object_hook=json_util.object_hook)
def json_dump(data):
return json.dumps(data, default=json_util.default)
@app.route('/')
def hello_world():
return render_template('index.html')
@app.route('/todos')
def list_todos():
return json_dump(list(todos.find()))
@app.route('/todos', methods=['POST'])
def new_todo():
todo = json_load(request.data)
todos.save(todo)
return json_dump(todo)
@app.route('/todos/<todo_id>', methods=['PUT'])
def update_todo(todo_id):
todo = json_load(request.data)
todos.save(todo)
return json_dump(todo)
@app.route('/todos/<todo_id>', methods=['DELETE'])
def delete_todo(todo_id):
todos.remove(ObjectId(todo_id))
return ""
if __name__ == '__main__':
app.run(debug=True)