-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtask_server.py
132 lines (95 loc) · 3.59 KB
/
task_server.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
"""
Created on Apr 13, 2015
Last updated on Apr 21, 2017
@author: Luigi De Russis, Teodoro Montanaro
"""
from flask import Flask, jsonify, abort, request, Response, render_template
from flask_bootstrap import Bootstrap # needed for the simple web client, only
import db_interaction
app = Flask(__name__)
# ---------- SIMPLE CLIENT ----------
Bootstrap(app)
@app.route('/')
def index():
return render_template('index.html')
# ---------- REST SERVER ----------
@app.route('/api/v1.0/tasks', methods=['GET'])
def get_tasks():
#we try to get the search_substring
search_substring = request.args.get('search_substring')
#if the search_substring is not set we have to return the full list of tasks
if search_substring is None:
# init
tasks = []
# get the task list from the db
task_list = db_interaction.get_tasks()
# prepare the task list for jsonify
for item in task_list:
task = prepare_for_json(item)
tasks.append(task)
# return the task data
return jsonify({'tasks': tasks})
else:
#if the search_substring is set we have to return a filtered list of tasks
# init
tasks = []
# get the filtered task list from the db
task_list = db_interaction.get_filtered_tasks(search_substring)
# prepare the task list for jsonify
for item in task_list:
task = prepare_for_json(item)
tasks.append(task)
# return the task data
return jsonify({'tasks': tasks})
@app.route('/api/v1.0/tasks/<int:task_id>', methods=['GET'])
def get_task(task_id):
# get the task
task = db_interaction.get_task(int(task_id))
# return 404 not found if no task has the given id
if task is None:
abort(404)
# convert the task in a JSON representation
return jsonify({'task': prepare_for_json(task)})
@app.route('/api/v1.0/tasks', methods=['POST'])
def insert_task():
# get the request body
add_request = request.json
# check whether a task is present in the request or not
if (add_request is not None) and ('description' in add_request) and ('urgent' in add_request):
text = add_request['description']
urgent = add_request['urgent']
# insert in the database
db_interaction.insert_task(text, urgent)
return Response(status=201)
# return an error in case of problems
abort(403)
@app.route('/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
def update_task(task_id):
# get the request body
add_request = request.json
# check whether a task is present in the request or not
if add_request is not None and ('description' and 'urgent') in add_request:
text = add_request['description']
urgent = add_request['urgent']
# update the task
task = db_interaction.update_task(int(task_id),text,urgent)
return Response(status=200)
# return an error in case of problems
abort(403)
@app.route('/api/v1.0/tasks/<int:task_id>', methods=['DELETE'])
def delete_task(task_id):
# delete the task
task = db_interaction.remove_task_by_id(int(task_id))
return Response(status=200)
def prepare_for_json(item):
"""
Convert the task in a dictionary for easing the JSON creation
"""
task = dict()
task['id'] = item[0]
task['description'] = item[1]
task['urgent'] = item[2]
return task
if __name__ == '__main__':
app.run(debug=True)