tutorial 2, adding do/undo functions

master
grgr 2 years ago
parent 8000d11872
commit bb0501a456

Binary file not shown.

@ -22,7 +22,8 @@ app.config['SECRET KEY'] = 'this should be a secret random string'
@app.route('/') @app.route('/')
def index(): def index():
conn = get_db_connection() conn = get_db_connection()
todos = conn.execute('SELECT i.content, l.title FROM items i JOIN lists l \ todos = conn.execute('SELECT i.id, i.done, i.content, l.title \
FROM items i JOIN lists l \
ON i.list_id = l.id ORDER BY l.title;').fetchall() ON i.list_id = l.id ORDER BY l.title;').fetchall()
lists = {} lists = {}
@ -61,3 +62,19 @@ def create():
return render_template('create.html', lists=lists) return render_template('create.html', lists=lists)
@app.route('/<int:id>/do/', methods=('POST',))
def do(id):
conn = get_db_connection()
conn.execute('UPDATE items SET done = 1 WHERE id = ?', (id,))
conn.commit()
conn.close()
return redirect(url_for('index'))
@app.route('/<int:id>/undo/', methods=('POST',))
def undo(id):
conn = get_db_connection()
conn.execute('UPDATE items SET done = 0 WHERE id = ?', (id,))
conn.commit()
conn.close()
return redirect(url_for('index'))

Binary file not shown.

@ -2,7 +2,8 @@ from itertools import groupby
from app import get_db_connection from app import get_db_connection
conn = get_db_connection() conn = get_db_connection()
todos = conn.execute('SELECT i.content, l.title FROM items i JOIN lists l \ todos = conn.execute('SELECT i.id, i.done, i.content, l.title \
FROM items i JOIN lists l \
ON i.list_id = l.id ORDER BY l.title;').fetchall() ON i.list_id = l.id ORDER BY l.title;').fetchall()
lists = {} lists = {}
@ -13,4 +14,5 @@ for k, g in groupby(todos, key=lambda t: t['title']):
for list_, items in lists.items(): for list_, items in lists.items():
print(list_) print(list_)
for item in items: for item in items:
print(' ', item['content']) print(' ', item['content'], '| id:',
item['id'], 'done:', item['done'])

@ -12,5 +12,6 @@ CREATE TABLE items (
list_id INTEGER NOT NULL, list_id INTEGER NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
content TEXT NOT NULL, content TEXT NOT NULL,
done INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (list_id) REFERENCES lists (id) FOREIGN KEY (list_id) REFERENCES lists (id)
); );

@ -9,7 +9,32 @@
</div> </div>
<ul class="list-group list-group-flush"> <ul class="list-group list-group-flush">
{% for item in items %} {% for item in items %}
<li class="list-group-item">{{ item['content'] }}</li> <!-- <li class="list-group-item">{{ item['content'] }}</li> -->
<li class="list-group-item"
{% if item['done'] %}
style="text-decoration: line-through;"
{% endif %}
>{{ item['content'] }}
{% if not item ['done'] %}
{% set URL = 'do' %}
{% set BUTTON = 'Do' %}
{% else %}
{% set URL = 'undo' %}
{% set BUTTON = 'Undo' %}
{% endif %}
<div class="row">
<div class="col-12 col-md-3">
<form action="{{ url_for(URL, id=item['id']) }}"
method="POST">
<input type="submit" value="{{ BUTTON }}"
class="btn btn-success btn-sm">
</form>
</div>
</div>
</li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>

Loading…
Cancel
Save