-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo_list_flask.py
69 lines (53 loc) · 2.14 KB
/
todo_list_flask.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
'''
Created on Apr 11, 2016
Copyright (c) 2015-2016 Teodoro Montanaro
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License
@author: tmontanaro
'''
from flask import Flask, url_for, render_template, request, redirect
import db_interaction
app = Flask(__name__)
@app.route('/')
def hello_world():
# if no address is given, redirect to the index page
return redirect(url_for('index'))
@app.route('/index.html')
def index():
#get the ordered list from the database
tasks_list = db_interaction.get_sorted_tasks_list()
return render_template('index.html', tasks_list=tasks_list)
@app.route('/insert_task.html', methods=['POST'])
def insert_task():
# check for parameter received by POST method
if ('description' in request.form and request.form['description']!=''):
#get description (the task)
description = request.form['description']
# transform value received through the checkbox in a 0/1 value
if ('urgent' in request.form and request.form['urgent'] == 'on'):
urgent = 1
else:
urgent = 0
#insert the new task in the database
db_interaction.db_insert_task(description, urgent)
# redirect to the home page
return redirect(url_for('index'))
@app.route('/delete_task.html', methods=['GET'])
def delete_task():
# check for parameter received by GET method
if 'id_task' in request.args:
#get the id of the task we want to remove
id_task = request.args.get('id_task')
# remove the item from the db
db_interaction.db_remove_task_by_id(id_task)
# redirect to the home page
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)