-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrester.py
104 lines (69 loc) · 3.03 KB
/
rester.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
from collections import defaultdict
from flask import Flask, request, g
from flask_restful import abort, Api, Resource
app = Flask(__name__)
api = Api(app)
from database import *
if not os.path.exists(DATABASE):
init_db(app)
@app.teardown_appcontext
def close_connection(exception):
close_db()
def abort_item(list_id, item_id):
abort(404, message=f"Item {item_id} doesn't exist in list {list_id}")
class Item(Resource):
def get(self, list_id, item_id):
data = query_db("select data from data where list = ? and item_id = ?", [list_id, item_id], one=True)
if data is None:
abort_item(list_id, item_id)
else:
# create json to return, includes item id
item = { 'id': item_id, **json.loads(data['data']) }
return item
def delete(self, list_id, item_id):
perform_db("delete from data where list = ? and item_id = ?", [list_id, item_id])
return {}, 204
def put(self, list_id, item_id):
# TODO use transaction
data = query_db("select data from data where list = ? and item_id = ?", [list_id, item_id], one=True)
if data is None:
abort_item(list_id, item_id)
else:
# merge old data with new (new overwrites any old keys)
item_data = json.loads(data['data'])
item_data.update(request.form.to_dict(flat=True))
# save to database
perform_db("update data set data = ? where list = ? and item_id = ?",
[json.dumps(item_data), list_id, item_id])
# create json to return, includes item id
item = { 'id': item_id, **item_data }
return item, 201
class List(Resource):
def get(self, list_id):
# get all items in this list
data = query_db("select item_id, data from data where list = ?", [list_id])
objects = [ { "id": item['item_id'], **json.loads(item['data']) } for item in data ]
search = request.args
# in json, show with id in each item
if search:
return [ obj for obj in objects if all([ nm in obj and obj[nm] == vl for nm,vl in list(search.items()) ]) ]
else:
return objects
def post(self, list_id):
# TODO use transaction
# get previous max id and add 1
data = query_db("select max(item_id) as id from data where list = ?", [list_id], one=True)
if data['id'] is None:
item_id = 1
else:
item_id = data['id'] + 1
# create item and add all POSTed arguments
item_data = request.form.to_dict(flat=True)
perform_db("insert into data (list, item_id, data) values (?, ?, ?)", [list_id, item_id, json.dumps(item_data)])
item = { 'id': item_id, **request.form.to_dict(flat=True) }
return item, 201
# setup the API resource routing
api.add_resource(Item, '/<list_id>/<item_id>')
api.add_resource(List, '/<list_id>')
if __name__ == '__main__':
app.run(debug=True)