-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
67 lines (59 loc) · 1.61 KB
/
app.py
File metadata and controls
67 lines (59 loc) · 1.61 KB
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
from flask import Flask, jsonify, request, json
from apps.todo.models import Task
from database import db_session
app = Flask(__name__)
app.debug = True
'''
Get a all todo items
'''
@app.route('/todo/', methods=['GET'])
def index():
tasks = Task.query.all()
response = list(map(lambda task: task.serialize(), tasks))
return jsonify(response)
'''
Get a single todo item
'''
@app.route('/todo/<todo_id>/', methods=['GET'])
def show(todo_id):
task = Task.query.get(todo_id)
return task.as_dict()
'''
Add a new todo item
'''
@app.route('/todo/', methods=['POST'])
def store():
task = Task()
task.title = request.form['title']
task.description = request.form['description']
task.status = request.form['status']
task.start_date = request.form['start_date']
db_session.add(task)
db_session.commit()
return {'success': task.id is not int, 'data': task.as_dict()}
'''
Delete a todo item
'''
@app.route('/todo/<int:id>/', methods=['DELETE'])
def destroy(id):
task = Task.query.get(id)
db_session.delete(task)
deleted = db_session.commit()
return {'success': deleted is None, 'data': task.as_dict()}
'''
Change status of a todo item
'''
@app.route('/todo/<int:id>/done/', methods=['PATCH'])
def update_status(id, new_status=None):
task = Task.query.get(id)
task.status = "done"
updated = db_session.commit()
return {'success': updated is None, 'data': task.as_dict()}
'''
Get information about the todo api
'''
@app.route('/todo/about/')
def about():
return {'name': 'A simple Todo API in Flask', 'version': '0.1.0'}
if __name__ == '__main__':
app.run(debug=True)