From 43ac5d656f20811b65eaa8c68a00df7e18a85e26 Mon Sep 17 00:00:00 2001 From: s3lph Date: Sat, 7 Dec 2024 21:15:46 +0100 Subject: [PATCH] feat: add logout after purchase setting to productlist --- matemat/webserver/pagelets/buy.py | 2 + matemat/webserver/pagelets/deposit.py | 2 + matemat/webserver/pagelets/transfer.py | 2 + static/js/depositlist.js | 150 +++++++++++++------------ templates/productlist.html | 19 ++++ 5 files changed, 102 insertions(+), 73 deletions(-) diff --git a/matemat/webserver/pagelets/buy.py b/matemat/webserver/pagelets/buy.py index 5ce27ba..2257cb2 100644 --- a/matemat/webserver/pagelets/buy.py +++ b/matemat/webserver/pagelets/buy.py @@ -24,6 +24,8 @@ def buy(): # Fetch the authenticated user from the database uid: int = session.get(session_id, 'authenticated_user') user = db.get_user(uid) + if 'logout_after_purchase' in request.params: + db.change_user(user, agent=user, logout_after_purchase=request.params.logout_after_purchase != '0') # Read the product from the database, identified by the product ID passed as request argument if 'pid' in request.params: pid = int(str(request.params.pid)) diff --git a/matemat/webserver/pagelets/deposit.py b/matemat/webserver/pagelets/deposit.py index cd19b26..b608a5e 100644 --- a/matemat/webserver/pagelets/deposit.py +++ b/matemat/webserver/pagelets/deposit.py @@ -23,6 +23,8 @@ def deposit(): # Fetch the authenticated user from the database uid: int = session.get(session_id, 'authenticated_user') user = db.get_user(uid) + if 'logout_after_purchase' in request.params: + db.change_user(user, agent=user, logout_after_purchase=request.params.logout_after_purchase != '0') if 'n' in request.params: # Read the amount of cash to deposit from the request arguments n = int(str(request.params.n)) diff --git a/matemat/webserver/pagelets/transfer.py b/matemat/webserver/pagelets/transfer.py index a0d0695..d522560 100644 --- a/matemat/webserver/pagelets/transfer.py +++ b/matemat/webserver/pagelets/transfer.py @@ -21,6 +21,8 @@ def transfer(): # Fetch the authenticated user from the database uid: int = session.get(session_id, 'authenticated_user') user = db.get_user(uid) + if 'logout_after_purchase' in request.params: + db.change_user(user, agent=user, logout_after_purchase=request.params.logout_after_purchase != '0') if 'target' not in request.params or 'n' not in request.params: redirect('/') return diff --git a/static/js/depositlist.js b/static/js/depositlist.js index a76345d..4044457 100644 --- a/static/js/depositlist.js +++ b/static/js/depositlist.js @@ -1,15 +1,17 @@ Number.prototype.pad = function(size) { - var s = String(this); - while (s.length < (size || 2)) {s = "0" + s;} - return s; + var s = String(this); + while (s.length < (size || 2)) {s = "0" + s;} + return s; } const Mode = { - Deposit: 0, - Buy: 1, - Transfer: 2, + Deposit: 0, + Buy: 1, + Transfer: 2, } +var append_query_string = ''; + let mode = Mode.Deposit; let product_id = null; let target_user = null; @@ -28,106 +30,108 @@ button.classList.add('btn-primary'); button.classList.add('me-2'); button.innerText = 'Deposit'; button.onclick = (ev) => { - mode = Mode.Deposit; + mode = Mode.Deposit; product_id = null; - target_user = null; + target_user = null; deposit = '0'; title.innerText = 'Deposit'; amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad(); input.classList.add('show'); - userlist.classList.remove('show'); - ok_button.classList.remove('disabled'); + userlist.classList.remove('show'); + ok_button.classList.remove('disabled'); }; button_transfer.classList.add('btn'); button_transfer.classList.add('btn-primary'); button_transfer.classList.add('me-2'); button_transfer.innerText = 'Transfer'; button_transfer.onclick = (ev) => { - mode = Mode.Transfer; + mode = Mode.Transfer; product_id = null; - target_user = null; + target_user = null; deposit = '0'; title.innerText = 'Transfer'; amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad(); input.classList.add('show'); - userlist.classList.add('show'); - ok_button.classList.add('disabled'); + userlist.classList.add('show'); + ok_button.classList.add('disabled'); }; setup_custom_price = (pid, pname) => { - mode = Mode.Buy; + mode = Mode.Buy; product_id = pid; - target_user = null; + target_user = null; title.innerText = pname; deposit = '0'; amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad(); input.classList.add('show'); - userlist.classList.remove('show'); - ok_button.classList.remove('disabled'); + userlist.classList.remove('show'); + ok_button.classList.remove('disabled'); }; set_transfer_user = (li, uid) => { - if (target_user_li != null) { - target_user_li.classList.remove('active'); - } - target_user = uid; - target_user_li = li; - ok_button.classList.remove('disabled'); - target_user_li.classList.add('active'); + if (target_user_li != null) { + target_user_li.classList.remove('active'); + } + target_user = uid; + target_user_li = li; + ok_button.classList.remove('disabled'); + target_user_li.classList.add('active'); } scrollUserlist = (delta) => { - userlist_list.scrollBy(0, delta); + userlist_list.scrollBy(0, delta); } deposit_key = (k) => { - if (k == 'ok') { - switch (mode) { - case Mode.Deposit: - window.location.href = '/deposit?n=' + parseInt(deposit); - break; - case Mode.Buy: - window.location.href = '/buy?pid=' + product_id + '&price=' + parseInt(deposit); - break; - case Mode.Transfer: - if (target_user == null) { - return; - } - window.location.href = '/transfer?target=' + target_user + '&n=' + parseInt(deposit); - break; + if (k == 'ok') { + switch (mode) { + case Mode.Deposit: + window.location.href = '/deposit?n=' + parseInt(deposit) + append_query_string; + break; + case Mode.Buy: + window.location.href = '/buy?pid=' + product_id + '&price=' + parseInt(deposit) + append_query_string; + break; + case Mode.Transfer: + if (target_user == null) { + return; } - mode = Mode.Deposit; - deposit = '0'; - product_id = null; - target_user = null; - if (target_user_li != null) { - target_user_li.classList.remove('active'); - } - input.classList.remove('show'); - userlist.classList.remove('show'); + window.location.href = '/transfer?target=' + target_user + '&n=' + parseInt(deposit) + append_query_string; + break; + } + mode = Mode.Deposit; + deposit = '0'; + product_id = null; + target_user = null; + if (target_user_li != null) { + target_user_li.classList.remove('active'); + } + input.classList.remove('show'); + userlist.classList.remove('show'); } else if (k == 'del') { - if (deposit == '0') { - product_id = null; - target_user = null; - if (target_user_li != null) { - target_user_li.classList.remove('active'); - } - userlist.classList.remove('show'); - input.classList.remove('show'); - } - deposit = deposit.substr(0, deposit.length - 1); - if (deposit.length == 0) { - deposit = '0'; - } - amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad(); + if (deposit == '0') { + product_id = null; + target_user = null; + if (target_user_li != null) { + target_user_li.classList.remove('active'); + } + userlist.classList.remove('show'); + input.classList.remove('show'); + } + deposit = deposit.substr(0, deposit.length - 1); + if (deposit.length == 0) { + deposit = '0'; + } + amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad(); + } else { + if (deposit == '0') { + deposit = k; } else { - if (deposit == '0') { - deposit = k; - } else { - deposit += k; - } - amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad(); + deposit += k; + } + amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad(); } }; -let list = document.getElementById('depositlist'); -list.innerHTML = ''; -list.appendChild(button); -list.appendChild(button_transfer); +let list = document.getElementById("depositlist"); +for (let i = 0; i < list.children.length; ++i) { + list.removeChild(list.children[0]); +} +list.insertBefore(button_transfer, list.children[0]); +list.insertBefore(button, list.children[0]); diff --git a/templates/productlist.html b/templates/productlist.html index 5cec250..075d950 100644 --- a/templates/productlist.html +++ b/templates/productlist.html @@ -11,6 +11,10 @@
Deposit CHF 1 Deposit CHF 10 +
+ + +
@@ -72,6 +76,21 @@ {% endfor %}
+ + {{ super() }} {% endblock %}