forked from imclab/todoapp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
74 lines (62 loc) · 2.08 KB
/
api.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
from flask import Blueprint, request
from flask.ext.restful import Api, Resource, fields, marshal_with
from werkzeug.datastructures import ImmutableDict
from models import Todo, db
api = Api(prefix='/api')
api_bp = Blueprint('api_bp', __name__)
api.init_app(api_bp)
todo_fields = {
'id': fields.Integer,
'task': fields.String,
'date': fields.DateTime,
'done': fields.Boolean
}
class TodoListResource(Resource):
@marshal_with(todo_fields)
def get(self):
return Todo.query.order_by(Todo.done, db.desc(Todo.date)).all()
@marshal_with(todo_fields)
def post(self):
new = Todo(request.json['task'], False)
db.session.add(new)
db.session.commit()
return new, 201
def put(self):
tasks = Todo.query.all()
for task in tasks:
task.done = True
db.session.commit()
return '', 204
def delete(self):
if request.args.get('done'):
to_delete = Todo.query.filter_by(done=True)
else:
to_delete = Todo.query.all()
for item in to_delete:
db.session.delete(item)
db.session.commit()
return '', 204
class TodoResource(Resource):
@marshal_with(todo_fields)
def get(self, todo_id):
res = Todo.query.get(todo_id)
if res:
return res
return {'error': 'The specified id was not found in the DB'}, 400
def delete(self, todo_id):
to_delete = Todo.query.get(todo_id)
if to_delete:
db.session.delete(to_delete)
db.session.commit()
return '', 204
return {'error': 'The specified id was not found in the DB'}, 400
def put(self, todo_id):
to_modify = Todo.query.get(todo_id)
if to_modify and request.json:
to_modify.task = request.json.get('task', to_modify.task)
to_modify.done = request.json.get('done', to_modify.done)
db.session.commit()
return '', 204
return {}, 400
api.add_resource(TodoListResource, '/todo')
api.add_resource(TodoResource, '/todo/<string:todo_id>')