Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tmontanaro committed Apr 10, 2016
0 parents commit 9a1f2ac
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 0 deletions.
108 changes: 108 additions & 0 deletions db_interaction.py
Original file line number Diff line number Diff line change
@@ -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
# <<the multiple character percent sign (%) wildcardcan be used to represent any number of characters in a value match>>
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()
Binary file added db_interaction.pyc
Binary file not shown.
Binary file added static/rooster.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added task_list.db
Binary file not shown.
30 changes: 30 additions & 0 deletions templates/delete_task.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Delete tasks</title>
</head>
<body>
<p>Here you can delete all the existing tasks that contain a provided string</p>
<p>Existing tasks: </p>
<ul>
{% for task in tasks_list %}
<li>{{ task }}</li>
{% endfor %}
</ul>

{% if substring_for_delete!="" %}
<p><b style="color:green;">Removal successfully performed!</b></p>

{% else %}
<form action="{{ url_for('delete_task') }}" method="post">
<p>
<label>Type the substring you want to use to remove all tasks that contain it: <input type="text" name="substring_for_delete"></label>
<input type="submit" value="Enter">
</p>
</form>
{% endif %}

<p><a href="{{ url_for('index') }}">Come back home</a></p>
</body>
</html>
23 changes: 23 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome to the Todo List Manager</title>
</head>
<body>
<h1>Welcome to the Todo List Manager</h1>
<p>Select the action you want to perform from the following menu:</p>
<ul>
<li>
<a href="{{ url_for('insert_task') }}">insert a new task</a>
</li>
<li>
<a href="{{ url_for('show_tasks') }}">show all existing tasks sorted in alphabetic order</a>
</li>
<li>
<a href="{{ url_for('delete_task') }}">remove all the existing tasks that contain a provided string</a>
</li>
</ul>

</body>
</html>
26 changes: 26 additions & 0 deletions templates/insert_task.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login page</title>
</head>
<body>
<p>Here you can insert a new task</p>

{% if string_for_insertion!="" %}
<p><b style="color:green;">Insertion successfully performed!</b></p>

{% else %}
<form action="{{ url_for('insert_task') }}" method="post">
<p>
<label>Type the new task: <input type="text" name="string_for_insertion"></label>
<label>Is it urgent? <input type="checkbox" name="urgent_for_insertion"></label>
<input type="submit" value="Enter">
</p>
</form>
{% endif %}


<p><a href="{{ url_for('index') }}">Come back home</a></p>
</body>
</html>
19 changes: 19 additions & 0 deletions templates/show_tasks.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Show tasks</title>
</head>
<body>
<h1>Existing tasks</h1>
<p>Here you can see all existing tasks</p>
<ul>
{% for task in tasks_list %}
<li>{{ task }}</li>
{% endfor %}
</ul>

<p><a href="{{ url_for('index') }}">Come back home</a></p>

</body>
</html>
71 changes: 71 additions & 0 deletions todo_list_flask.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 9a1f2ac

Please sign in to comment.