From bb0501a456d06c8e639b5e0eebaa6fd2e7128752 Mon Sep 17 00:00:00 2001 From: grgr Date: Tue, 27 Sep 2022 23:04:45 +0200 Subject: [PATCH] tutorial 2, adding do/undo functions --- __pycache__/app.cpython-310.pyc | Bin 1824 -> 2301 bytes app.py | 21 +++++++++++++++++++-- database.db | Bin 16384 -> 16384 bytes list_example.py | 6 ++++-- schema.sql | 1 + templates/index.html | 27 ++++++++++++++++++++++++++- 6 files changed, 50 insertions(+), 5 deletions(-) diff --git a/__pycache__/app.cpython-310.pyc b/__pycache__/app.cpython-310.pyc index 833a6175b780f8930673ee51c79775ec70039c04..a71676456b9d0ca669f3d14035b43e8ff30ab290 100644 GIT binary patch delta 621 zcmZ{hOG_g`5XZZECc`9~jPKXtqY;cogYj__AGq#-$Rc7Qg4fgt<6{Qe79l7eqDK!h z;K9A<(SrvS{1AQw4UqR*U{Iwwq}5{%*mM~gf0W2YUi zj|UD%JZG&I>#!c3BaRK&c)-fA(opF(y?2|jrP6Igo@bfXtJ^#H^*8VSac2s`AM`Ox z#4zx*7Q;Y!fy#=FLFKCrPgITQlwU$uG;3VOIDa+Gn}5a=z z8}|b>=Hp}z!(0Yr7j=uECwUJ}YEsTnJ`WKTE3-Y=g)>0dWvg(`fl$kAD12%n0ko{DaYCdP(4(R?|)j4G^ov-5izwM## zwq+mvBr_UcOqq0DIHxxTG_r(DQ?zcWANma{=43W{w9?(3R419XEWnQ0@dVgL0+O|q IZIreHZ))XxtpET3 delta 127 zcmew>xPXr@pO=@50SLB58Yic*PvnzfY@4Xv%9g?u%%CZ_@zE(p_TpeyA6Ms)$p@K4 zCckG|!zed-4)a=8MyCHP%$xmK9y2mVO*Uc+W>lNp#%9c>3N%Vnc(NHs38U)dtsH7n dVn6{N1`b9ZMg>L=Rt`=sMh<2!Mjn1XMgVfG94P<* diff --git a/app.py b/app.py index b8f126e..0f205bd 100644 --- a/app.py +++ b/app.py @@ -22,7 +22,8 @@ app.config['SECRET KEY'] = 'this should be a secret random string' @app.route('/') def index(): 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() lists = {} @@ -60,4 +61,20 @@ def create(): conn.close() return render_template('create.html', lists=lists) - \ No newline at end of file + +@app.route('//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('//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')) \ No newline at end of file diff --git a/database.db b/database.db index be3b8c5871e0177a2baa899254f9c45aabd04911..70ee5c8d593f82d946120feb677fda62251b0d64 100644 GIT binary patch delta 492 zcmZo@U~Fh$oFFYI$iTqB0>m)DHc`h|Sdc-_zm^v$#LWAEfuD&#g!jS5#ynoBCV3Wi zadC0R7TJ=-q@2{ulGNN{Fu^ogo^P9cN`79df~Q}ItGjEEf}ekgf?ueQkAjP5p9BxV-n=czCgP!*e8lvtjl$^=rT%z@Leg3R>P{5*X|HU>^(OAahn zR+T0vXJqDOD&!X_Bz~ges*SFIsiz;cP{_{ delta 569 zcmaKo%SyvQ7=>qYY3rgH#YNN>ol?6HwB%x`cH<4foeO;cV>+gTX{Jn4iUqM%+$#|u zLh44r#}GHZiEdok1OfpsGt9r3^Ks6c84TcH0B;IY832GZKFJbpm(A%6sP3QV$5)~v z19XT4MSd<#hWWW+U(Bki3a<_V>)0hUAZjM?Jv^t30D40Y=o%fNCFN6jRYv0qcgh9M$pD%KF=c3I4ZYS_t-Ds&Yx1*7 zHX6i$VmI|8(k5hcK@SqY~>$iBjtG`(4{^QIBHA NvCX>NAwfVkz5(Y5h(-Va diff --git a/list_example.py b/list_example.py index 67208ed..cc6bedf 100644 --- a/list_example.py +++ b/list_example.py @@ -2,7 +2,8 @@ from itertools import groupby from app import 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() lists = {} @@ -13,4 +14,5 @@ for k, g in groupby(todos, key=lambda t: t['title']): for list_, items in lists.items(): print(list_) for item in items: - print(' ', item['content']) \ No newline at end of file + print(' ', item['content'], '| id:', + item['id'], 'done:', item['done']) \ No newline at end of file diff --git a/schema.sql b/schema.sql index 8eed966..40c290e 100644 --- a/schema.sql +++ b/schema.sql @@ -12,5 +12,6 @@ CREATE TABLE items ( list_id INTEGER NOT NULL, created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, content TEXT NOT NULL, + done INTEGER NOT NULL DEFAULT 0, FOREIGN KEY (list_id) REFERENCES lists (id) ); \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index c97680d..f6b5bc4 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,7 +9,32 @@
    {% for item in items %} -
  • {{ item['content'] }}
  • + +
  • {{ item['content'] }} + {% if not item ['done'] %} + {% set URL = 'do' %} + {% set BUTTON = 'Do' %} + {% else %} + {% set URL = 'undo' %} + {% set BUTTON = 'Undo' %} + {% endif %} + + + +
    +
    +
    + +
    +
    +
    +
  • {% endfor %}