Skip to content

Commit

Permalink
Delete with task id
Browse files Browse the repository at this point in the history
  • Loading branch information
tmontanaro committed Apr 11, 2016
1 parent 6765135 commit b1b0abb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
14 changes: 6 additions & 8 deletions db_interaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'''

import sqlite3
import operator


def db_insert_task(text, urgent):
Expand Down Expand Up @@ -55,7 +56,7 @@ def get_sorted_tasks_list():
'''

tasks_list = []
sql = "SELECT todo FROM task order by todo ASC" #here we order data using "order by"
sql = "SELECT id_task, 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
Expand All @@ -70,33 +71,30 @@ def get_sorted_tasks_list():
# 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
tasks_list.append((task[0],task[1])) #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):
def db_remove_task_by_id(id_task):
'''
: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 ?"
sql = "delete from task where id_task = ?"

# 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, ) )
cursor.execute(sql, (id_task, ) )
#commit all pending executed queries in the connection
conn.commit()
except Exception,e:
Expand Down
Binary file modified task_list.db
Binary file not shown.
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h1>Welcome to the Todo List Manager</h1>
<p>Here you can see all existing tasks</p>
<ul>
{% for task in tasks_list %}
<li>{{ task }} <a href="{{ url_for('delete_task',string_for_delete=task) }}">Delete</a></li>
<li>{{ task[1] }} <a href="{{ url_for('delete_task',id_task=task[0]) }}">Delete</a></li>
{% endfor %}
</ul>

Expand Down
6 changes: 3 additions & 3 deletions todo_list_flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ def insert_task():

@app.route('/delete_task.html', methods=['GET'])
def delete_task():
if 'string_for_delete' in request.args:
string_for_delete = request.args.get('string_for_delete')
db_interaction.db_remove_task(string_for_delete)
if 'id_task' in request.args:
id_task = request.args.get('id_task')
db_interaction.db_remove_task_by_id(id_task)

# back to the home page
return redirect(url_for('index'))
Expand Down

0 comments on commit b1b0abb

Please sign in to comment.