Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_server.py
More file actions
Latest commit
125 lines (88 loc) · 3.31 KB
/
Copy pathtask_server.py
File metadata and controls
125 lines (88 loc) · 3.31 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
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
#!/usr/bin/python
"""
Created on Apr 13, 2015
@author: Luigi De Russis
"""
fromflaskimportFlask, jsonify, abort, request, Response
importdb_interaction
app=Flask(__name__)
# ---------- REST SERVER ----------
@app.route('/api/v1.0/tasks', methods=['GET'])
defget_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
ifsearch_substringisNone:
# init
tasks= []
# get the task list from the db
task_list=db_interaction.get_tasks()
# prepare the task list for jsonify
foritemintask_list:
task=prepare_for_json(item)
tasks.append(task)
# return the task data
returnjsonify({'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
foritemintask_list:
task=prepare_for_json(item)
tasks.append(task)
# return the task data
returnjsonify({'tasks': tasks})
@app.route('/api/v1.0/tasks/<int:task_id>', methods=['GET'])
defget_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
iftaskisNone:
abort(404)
# convert the task in a JSON representation
returnjsonify({'task': prepare_for_json(task)})
@app.route('/api/v1.0/tasks', methods=['POST'])
definsert_task():
# get the request body
add_request=request.json
# check whether a task is present in the request or not
if (add_requestisnotNone) and ('description'inadd_request) and ('urgent'inadd_request):
text=add_request['description']
urgent=add_request['urgent']
# insert in the database
db_interaction.insert_task(text, urgent)
returnResponse(status=200)
# return an error in case of problems
abort(403)
@app.route('/api/v1.0/tasks/<int:task_id>', methods=['PUT'])
defupdate_task(task_id):
# get the request body
add_request=request.json
# check whether a task is present in the request or not
ifadd_requestisnotNoneand ('description'and'urgent') inadd_request:
text=add_request['description']
urgent=add_request['urgent']
# update the task
task=db_interaction.update_task(int(task_id),text,urgent)
returnResponse(status=200)
# return an error in case of problems
abort(403)
@app.route('/api/v1.0/tasks/<int:task_id>', methods=['DELETE'])
defdelete_task(task_id):
# delete the task
task=db_interaction.delete_task(int(task_id))
returnResponse(status=200)
defprepare_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]
returntask
if__name__=='__main__':
app.run(debug=True)