diff --git a/db_interaction.py b/db_interaction.py new file mode 100644 index 0000000..6b149b9 --- /dev/null +++ b/db_interaction.py @@ -0,0 +1,108 @@ +''' +Created on Apr 04, 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 +''' + +import sqlite3 + + +def db_insert_task(text, urgent): + ''' + :param text: text that we want to insert as task in the db + :param urgent: 0 if the task is not urgent, 1 otherwise + + This method insert a task in the database + ''' + + # prepare the query text + sql = """INSERT INTO task(todo, urgent) VALUES (?, ?)""" + + #connect to the db + conn = sqlite3.connect("task_list.db") + cursor = conn.cursor() + + try: + #execute the query passing the needed parameters + cursor.execute(sql, (text, urgent) ) + #commit all pending queries + conn.commit() + except Exception,e: + print str(e) + # if something goes wrong: rollback + conn.rollback() + + #close the connection + conn.close() + + +def get_sorted_tasks_list(): + ''' + :param tasks_list: list of existing tasks + + Get existing tasks from the database + ''' + + tasks_list = [] + sql = "SELECT todo FROM task order by todo ASC" #here we order data using "order by" + conn = sqlite3.connect("task_list.db") + + # to remove u from sqlite3 cursor.fetchall() results + conn.text_factory = sqlite3.OptimizedUnicode + + + cursor = conn.cursor() + cursor.execute(sql) + + results = cursor.fetchall() + + # print results + + for task in results: + tasks_list.append(task[0]) #each "task" is a tuple, so we have to take the first element of it + + conn.close() + + return tasks_list + +def db_remove_task(text): + ''' + :param text: text (or part of it) of the task we want to remove from the db + + This method remove from the db all the tasks that contain the specified string + ''' + + # prepare the query text + sql = "delete from task where todo LIKE ?" + + # add percent sign (%) wildcard to select all the strings that contain specified text + # <> + text = "%"+text + "%" + + #connect to the db + conn = sqlite3.connect("task_list.db") + cursor = conn.cursor() + + try: + #execute the query passing the needed parameters + cursor.execute(sql, (text, ) ) + #commit all pending executed queries in the connection + conn.commit() + except Exception,e: + print str(e) + # if something goes wrong: rollback + conn.rollback() + + #close the connection + conn.close() \ No newline at end of file diff --git a/db_interaction.pyc b/db_interaction.pyc new file mode 100644 index 0000000..81f716d Binary files /dev/null and b/db_interaction.pyc differ diff --git a/static/rooster.jpg b/static/rooster.jpg new file mode 100644 index 0000000..fc6ac92 Binary files /dev/null and b/static/rooster.jpg differ diff --git a/task_list.db b/task_list.db new file mode 100644 index 0000000..3e31750 Binary files /dev/null and b/task_list.db differ diff --git a/templates/delete_task.html b/templates/delete_task.html new file mode 100644 index 0000000..a696f6f --- /dev/null +++ b/templates/delete_task.html @@ -0,0 +1,30 @@ + + + + + Delete tasks + + +

Here you can delete all the existing tasks that contain a provided string

+

Existing tasks:

+ + + {% if substring_for_delete!="" %} +

Removal successfully performed!

+ + {% else %} +
+

+ + +

+
+ {% endif %} + +

Come back home

+ + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..52d4721 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,23 @@ + + + + + Welcome to the Todo List Manager + + +

Welcome to the Todo List Manager

+

Select the action you want to perform from the following menu:

+ + + + \ No newline at end of file diff --git a/templates/insert_task.html b/templates/insert_task.html new file mode 100644 index 0000000..33be2b2 --- /dev/null +++ b/templates/insert_task.html @@ -0,0 +1,26 @@ + + + + + Login page + + +

Here you can insert a new task

+ + {% if string_for_insertion!="" %} +

Insertion successfully performed!

+ + {% else %} +
+

+ + + +

+
+ {% endif %} + + +

Come back home

+ + \ No newline at end of file diff --git a/templates/show_tasks.html b/templates/show_tasks.html new file mode 100644 index 0000000..c179165 --- /dev/null +++ b/templates/show_tasks.html @@ -0,0 +1,19 @@ + + + + + Show tasks + + +

Existing tasks

+

Here you can see all existing tasks

+ + +

Come back home

+ + + \ No newline at end of file diff --git a/todo_list_flask.py b/todo_list_flask.py new file mode 100644 index 0000000..19413e7 --- /dev/null +++ b/todo_list_flask.py @@ -0,0 +1,71 @@ +''' +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(): + return render_template('index.html') + + +@app.route('/insert_task.html', methods=['GET', 'POST']) +def insert_task(): + if request.method == 'POST': + if ('string_for_insertion' in request.form and request.form['string_for_insertion']!=''): + string_for_insertion = request.form['string_for_insertion'] + if ('urgent_for_insertion' in request.form and request.form['urgent_for_insertion'] == 'on'): + urgent_for_insertion = 1 + else: + urgent_for_insertion = 0 + db_interaction.db_insert_task(string_for_insertion, urgent_for_insertion) + else: + string_for_insertion = "" + else: + string_for_insertion = "" + return render_template('insert_task.html', string_for_insertion = string_for_insertion) + +@app.route('/show_tasks.html') +def show_tasks(): + tasks_list = db_interaction.get_sorted_tasks_list() + return render_template('show_tasks.html', tasks_list=tasks_list) + +@app.route('/delete_task.html', methods=['GET', 'POST']) +def delete_task(): + if request.method == 'POST': + if 'substring_for_delete' in request.form: + substring_for_delete = request.form['substring_for_delete'] + db_interaction.db_remove_task(substring_for_delete) + else: + substring_for_delete = "" + else: + substring_for_delete = "" + tasks_list = db_interaction.get_sorted_tasks_list() + return render_template('delete_task.html', substring_for_delete = substring_for_delete, tasks_list=tasks_list) + + +if __name__ == '__main__': + app.run(debug=True)