-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
70 lines (55 loc) · 1.7 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
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
from bson import json_util, dbref
import pprint
import json
from flask import Flask, abort, g, request
from mongokit import Connection
# ---
MONGODB_HOST = 'localhost'
MONGODB_PORT = 27017
app = Flask(__name__)
app.debug = True
app.config.from_object(__name__)
mongo = Connection(app.config['MONGODB_HOST'],
app.config['MONGODB_PORT'])
# ---
@app.route('/', methods=['GET'])
def index():
abort(403)
# ---
@app.route('/clients', methods=['GET', 'POST'])
def clients():
if request.method == 'POST':
data = json.loads(request.data)
coll = mongo['tt'].clients
oid = coll.insert(data)
return json.dumps(data, default=json_util.default)
elif request.method == 'GET':
data = list(mongo['tt'].clients.find())
return json.dumps(data, default=json_util.default)
# ---
@app.route('/projects', methods=['GET', 'POST'])
def projects():
if request.method == 'POST':
data = json.loads(request.data)
data["client"] = dbref.DBRef("clients", data["client"])
coll = mongo['tt'].projects
oid = coll.insert(data)
return json.dumps(data, default=json_util.default)
elif request.method == 'GET':
data = list(mongo['tt'].projects.find())
return json.dumps(data, default=json_util.default)
# ---
@app.route('/tasks', methods=['GET', 'POST'])
def tasks():
if request.method == 'POST':
data = json.loads(request.data)
data["project"] = dbref.DBRef("projects", data["project"])
coll = mongo['tt'].tasks
oid = coll.insert(data)
return json.dumps(data, default=json_util.default)
elif request.method == 'GET':
data = list(mongo['tt'].tasks.find())
return json.dumps(data, default=json_util.default)
# ---
if __name__ == '__main__':
app.run()