From a20f01cc9a15737c07e0759176571ce2daca7c90 Mon Sep 17 00:00:00 2001 From: Joel Thompson Date: Tue, 17 Feb 2015 23:47:34 -0500 Subject: [PATCH 1/2] can't delete entries yet --- templates/show_list.html | 23 +++++++++++++++ to_do.py | 64 ++++++++++++++++++++++++++++++++++++++++ todo_schema.sql | 5 ++++ 3 files changed, 92 insertions(+) create mode 100644 templates/show_list.html create mode 100644 to_do.py create mode 100644 todo_schema.sql diff --git a/templates/show_list.html b/templates/show_list.html new file mode 100644 index 0000000..3557898 --- /dev/null +++ b/templates/show_list.html @@ -0,0 +1,23 @@ +
+

Todo List

+
+{% block body %} +
+
+
Create a new todo: +
+ +
+
+ +{% endblock %} diff --git a/to_do.py b/to_do.py new file mode 100644 index 0000000..f2f57c3 --- /dev/null +++ b/to_do.py @@ -0,0 +1,64 @@ +import sqlite3 +from contextlib import closing +from flask import Flask, request, session, g, redirect, url_for, \ + abort, render_template, flash + +DATABASE = '/tmp/flaskr.db' +DEBUG = True +SECRET_KEY = 'development key' +USERNAME = 'joel' +PASSWORD = 'password' + +app = Flask(__name__) +app.config.from_object(__name__) + + +def connect_db(): + return sqlite3.connect(app.config['DATABASE']) + + +def init_db(): + with closing(connect_db()) as db: + with app.open_resource('todo_schema.sql', mode='r') as f: + db.cursor().executescript(f.read()) + db.commit() + + +@app.before_request +def before_request(): + g.db = connect_db() + + +@app.teardown_request +def teardown_request(exception): + db = getattr(g, 'db', None) + if db is not None: + db.close() + + +@app.route('/') +def show_list(): + cur = g.db.execute('select task from entries order by id desc') + entries = [row[0] for row in cur.fetchall()] + return render_template('show_list.html', entries=entries) + + +@app.route('/', methods=['POST']) +def add_task(): + g.db.execute('insert into entries (task) values (?)', [request.form['new_task']]) + g.db.commit() + flash('New task added to the list') + return redirect(url_for('show_list')) + + +#@app.route('/', methods=['POST']) +#def remove_task(): +# g.db.execute('delete from entries WHERE id = ?', [postID]) +# g.db.commit() +# flash('Selected tasks have been removed') +# return redirect(url_for('show_list')) + + + +if __name__ == '__main__': + app.run() diff --git a/todo_schema.sql b/todo_schema.sql new file mode 100644 index 0000000..a802711 --- /dev/null +++ b/todo_schema.sql @@ -0,0 +1,5 @@ +drop table if exists entries; +create table entries ( + id integer primary key autoincrement, + task text +); From 9ca1da203d484d806812f3d8a7dc4cf93afe60fe Mon Sep 17 00:00:00 2001 From: Joel Thompson Date: Wed, 18 Feb 2015 09:50:38 -0500 Subject: [PATCH 2/2] got delete working --- templates/show_list.html | 6 +++--- to_do.py | 19 ++++++++++--------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/templates/show_list.html b/templates/show_list.html index 3557898..76ca981 100644 --- a/templates/show_list.html +++ b/templates/show_list.html @@ -11,12 +11,12 @@

Todo List

    Current Tasks:

    -
    + {% for task in entries %} - {{ task }} + {{ task[1] }}
    {% else %}
  • You are all caught up! - {% endfor %} + {% endfor %}
diff --git a/to_do.py b/to_do.py index f2f57c3..4e53742 100644 --- a/to_do.py +++ b/to_do.py @@ -38,12 +38,12 @@ def teardown_request(exception): @app.route('/') def show_list(): - cur = g.db.execute('select task from entries order by id desc') - entries = [row[0] for row in cur.fetchall()] + cur = g.db.execute('select id, task from entries order by id desc') + entries = [(row[0], row[1]) for row in cur.fetchall()] return render_template('show_list.html', entries=entries) -@app.route('/', methods=['POST']) +@app.route('/add', methods=['POST']) def add_task(): g.db.execute('insert into entries (task) values (?)', [request.form['new_task']]) g.db.commit() @@ -51,12 +51,13 @@ def add_task(): return redirect(url_for('show_list')) -#@app.route('/', methods=['POST']) -#def remove_task(): -# g.db.execute('delete from entries WHERE id = ?', [postID]) -# g.db.commit() -# flash('Selected tasks have been removed') -# return redirect(url_for('show_list')) +@app.route('/delete', methods=['POST']) +def remove_task(): + for checked in request.form.getlist('selected'): + g.db.execute('delete from entries where id = ?', checked) + g.db.commit() + flash('Selected tasks have been removed') + return redirect(url_for('show_list'))