Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtask_server.py
More file actions
Latest commit
85 lines (60 loc) · 2.04 KB
/
Copy pathtask_server.py
File metadata and controls
85 lines (60 loc) · 2.04 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
#!/usr/bin/python
"""
Created on Apr 13, 2015
@author: Luigi De Russis
"""
fromflaskimportFlask, jsonify, abort, request, Response, render_template
fromflask_bootstrapimportBootstrap# needed for the simple web client, only
importdb_interaction
app=Flask(__name__)
# ---------- SIMPLE CLIENT ----------
Bootstrap(app)
@app.route('/')
defindex():
returnrender_template('index.html')
# ---------- REST SERVER ----------
@app.route('/api/v1.0/tasks', methods=['GET'])
defget_tasks():
# 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})
@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)
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)