feat: add logout after purchase setting to productlist
This commit is contained in:
parent
51adba2e25
commit
43ac5d656f
5 changed files with 102 additions and 73 deletions
|
@ -24,6 +24,8 @@ def buy():
|
||||||
# Fetch the authenticated user from the database
|
# Fetch the authenticated user from the database
|
||||||
uid: int = session.get(session_id, 'authenticated_user')
|
uid: int = session.get(session_id, 'authenticated_user')
|
||||||
user = db.get_user(uid)
|
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
|
# Read the product from the database, identified by the product ID passed as request argument
|
||||||
if 'pid' in request.params:
|
if 'pid' in request.params:
|
||||||
pid = int(str(request.params.pid))
|
pid = int(str(request.params.pid))
|
||||||
|
|
|
@ -23,6 +23,8 @@ def deposit():
|
||||||
# Fetch the authenticated user from the database
|
# Fetch the authenticated user from the database
|
||||||
uid: int = session.get(session_id, 'authenticated_user')
|
uid: int = session.get(session_id, 'authenticated_user')
|
||||||
user = db.get_user(uid)
|
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:
|
if 'n' in request.params:
|
||||||
# Read the amount of cash to deposit from the request arguments
|
# Read the amount of cash to deposit from the request arguments
|
||||||
n = int(str(request.params.n))
|
n = int(str(request.params.n))
|
||||||
|
|
|
@ -21,6 +21,8 @@ def transfer():
|
||||||
# Fetch the authenticated user from the database
|
# Fetch the authenticated user from the database
|
||||||
uid: int = session.get(session_id, 'authenticated_user')
|
uid: int = session.get(session_id, 'authenticated_user')
|
||||||
user = db.get_user(uid)
|
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:
|
if 'target' not in request.params or 'n' not in request.params:
|
||||||
redirect('/')
|
redirect('/')
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
Number.prototype.pad = function(size) {
|
Number.prototype.pad = function(size) {
|
||||||
var s = String(this);
|
var s = String(this);
|
||||||
while (s.length < (size || 2)) {s = "0" + s;}
|
while (s.length < (size || 2)) {s = "0" + s;}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Mode = {
|
const Mode = {
|
||||||
Deposit: 0,
|
Deposit: 0,
|
||||||
Buy: 1,
|
Buy: 1,
|
||||||
Transfer: 2,
|
Transfer: 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var append_query_string = '';
|
||||||
|
|
||||||
let mode = Mode.Deposit;
|
let mode = Mode.Deposit;
|
||||||
let product_id = null;
|
let product_id = null;
|
||||||
let target_user = null;
|
let target_user = null;
|
||||||
|
@ -28,106 +30,108 @@ button.classList.add('btn-primary');
|
||||||
button.classList.add('me-2');
|
button.classList.add('me-2');
|
||||||
button.innerText = 'Deposit';
|
button.innerText = 'Deposit';
|
||||||
button.onclick = (ev) => {
|
button.onclick = (ev) => {
|
||||||
mode = Mode.Deposit;
|
mode = Mode.Deposit;
|
||||||
product_id = null;
|
product_id = null;
|
||||||
target_user = null;
|
target_user = null;
|
||||||
deposit = '0';
|
deposit = '0';
|
||||||
title.innerText = 'Deposit';
|
title.innerText = 'Deposit';
|
||||||
amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad();
|
amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad();
|
||||||
input.classList.add('show');
|
input.classList.add('show');
|
||||||
userlist.classList.remove('show');
|
userlist.classList.remove('show');
|
||||||
ok_button.classList.remove('disabled');
|
ok_button.classList.remove('disabled');
|
||||||
};
|
};
|
||||||
button_transfer.classList.add('btn');
|
button_transfer.classList.add('btn');
|
||||||
button_transfer.classList.add('btn-primary');
|
button_transfer.classList.add('btn-primary');
|
||||||
button_transfer.classList.add('me-2');
|
button_transfer.classList.add('me-2');
|
||||||
button_transfer.innerText = 'Transfer';
|
button_transfer.innerText = 'Transfer';
|
||||||
button_transfer.onclick = (ev) => {
|
button_transfer.onclick = (ev) => {
|
||||||
mode = Mode.Transfer;
|
mode = Mode.Transfer;
|
||||||
product_id = null;
|
product_id = null;
|
||||||
target_user = null;
|
target_user = null;
|
||||||
deposit = '0';
|
deposit = '0';
|
||||||
title.innerText = 'Transfer';
|
title.innerText = 'Transfer';
|
||||||
amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad();
|
amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad();
|
||||||
input.classList.add('show');
|
input.classList.add('show');
|
||||||
userlist.classList.add('show');
|
userlist.classList.add('show');
|
||||||
ok_button.classList.add('disabled');
|
ok_button.classList.add('disabled');
|
||||||
};
|
};
|
||||||
setup_custom_price = (pid, pname) => {
|
setup_custom_price = (pid, pname) => {
|
||||||
mode = Mode.Buy;
|
mode = Mode.Buy;
|
||||||
product_id = pid;
|
product_id = pid;
|
||||||
target_user = null;
|
target_user = null;
|
||||||
title.innerText = pname;
|
title.innerText = pname;
|
||||||
deposit = '0';
|
deposit = '0';
|
||||||
amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad();
|
amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad();
|
||||||
input.classList.add('show');
|
input.classList.add('show');
|
||||||
userlist.classList.remove('show');
|
userlist.classList.remove('show');
|
||||||
ok_button.classList.remove('disabled');
|
ok_button.classList.remove('disabled');
|
||||||
};
|
};
|
||||||
set_transfer_user = (li, uid) => {
|
set_transfer_user = (li, uid) => {
|
||||||
if (target_user_li != null) {
|
if (target_user_li != null) {
|
||||||
target_user_li.classList.remove('active');
|
target_user_li.classList.remove('active');
|
||||||
}
|
}
|
||||||
target_user = uid;
|
target_user = uid;
|
||||||
target_user_li = li;
|
target_user_li = li;
|
||||||
ok_button.classList.remove('disabled');
|
ok_button.classList.remove('disabled');
|
||||||
target_user_li.classList.add('active');
|
target_user_li.classList.add('active');
|
||||||
|
|
||||||
}
|
}
|
||||||
scrollUserlist = (delta) => {
|
scrollUserlist = (delta) => {
|
||||||
userlist_list.scrollBy(0, delta);
|
userlist_list.scrollBy(0, delta);
|
||||||
}
|
}
|
||||||
deposit_key = (k) => {
|
deposit_key = (k) => {
|
||||||
if (k == 'ok') {
|
if (k == 'ok') {
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case Mode.Deposit:
|
case Mode.Deposit:
|
||||||
window.location.href = '/deposit?n=' + parseInt(deposit);
|
window.location.href = '/deposit?n=' + parseInt(deposit) + append_query_string;
|
||||||
break;
|
break;
|
||||||
case Mode.Buy:
|
case Mode.Buy:
|
||||||
window.location.href = '/buy?pid=' + product_id + '&price=' + parseInt(deposit);
|
window.location.href = '/buy?pid=' + product_id + '&price=' + parseInt(deposit) + append_query_string;
|
||||||
break;
|
break;
|
||||||
case Mode.Transfer:
|
case Mode.Transfer:
|
||||||
if (target_user == null) {
|
if (target_user == null) {
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
window.location.href = '/transfer?target=' + target_user + '&n=' + parseInt(deposit);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
mode = Mode.Deposit;
|
window.location.href = '/transfer?target=' + target_user + '&n=' + parseInt(deposit) + append_query_string;
|
||||||
deposit = '0';
|
break;
|
||||||
product_id = null;
|
}
|
||||||
target_user = null;
|
mode = Mode.Deposit;
|
||||||
if (target_user_li != null) {
|
deposit = '0';
|
||||||
target_user_li.classList.remove('active');
|
product_id = null;
|
||||||
}
|
target_user = null;
|
||||||
input.classList.remove('show');
|
if (target_user_li != null) {
|
||||||
userlist.classList.remove('show');
|
target_user_li.classList.remove('active');
|
||||||
|
}
|
||||||
|
input.classList.remove('show');
|
||||||
|
userlist.classList.remove('show');
|
||||||
} else if (k == 'del') {
|
} else if (k == 'del') {
|
||||||
if (deposit == '0') {
|
if (deposit == '0') {
|
||||||
product_id = null;
|
product_id = null;
|
||||||
target_user = null;
|
target_user = null;
|
||||||
if (target_user_li != null) {
|
if (target_user_li != null) {
|
||||||
target_user_li.classList.remove('active');
|
target_user_li.classList.remove('active');
|
||||||
}
|
}
|
||||||
userlist.classList.remove('show');
|
userlist.classList.remove('show');
|
||||||
input.classList.remove('show');
|
input.classList.remove('show');
|
||||||
}
|
}
|
||||||
deposit = deposit.substr(0, deposit.length - 1);
|
deposit = deposit.substr(0, deposit.length - 1);
|
||||||
if (deposit.length == 0) {
|
if (deposit.length == 0) {
|
||||||
deposit = '0';
|
deposit = '0';
|
||||||
}
|
}
|
||||||
amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad();
|
amount.innerText = (Math.floor(parseInt(deposit) / 100)) + '.' + (parseInt(deposit) % 100).pad();
|
||||||
|
} else {
|
||||||
|
if (deposit == '0') {
|
||||||
|
deposit = k;
|
||||||
} else {
|
} else {
|
||||||
if (deposit == '0') {
|
deposit += k;
|
||||||
deposit = k;
|
}
|
||||||
} else {
|
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');
|
let list = document.getElementById("depositlist");
|
||||||
list.innerHTML = '';
|
for (let i = 0; i < list.children.length; ++i) {
|
||||||
list.appendChild(button);
|
list.removeChild(list.children[0]);
|
||||||
list.appendChild(button_transfer);
|
}
|
||||||
|
list.insertBefore(button_transfer, list.children[0]);
|
||||||
|
list.insertBefore(button, list.children[0]);
|
||||||
|
|
|
@ -11,6 +11,10 @@
|
||||||
<div id="depositlist">
|
<div id="depositlist">
|
||||||
<a class="btn btn-primary me-2" href="/deposit?n=100">Deposit CHF 1</a>
|
<a class="btn btn-primary me-2" href="/deposit?n=100">Deposit CHF 1</a>
|
||||||
<a class="btn btn-primary me-2" href="/deposit?n=1000">Deposit CHF 10</a>
|
<a class="btn btn-primary me-2" href="/deposit?n=1000">Deposit CHF 10</a>
|
||||||
|
<div class="form-check">
|
||||||
|
<input class="form-check-input" id="productlist-logout-after-purchase" type="checkbox"{% if authuser.logout_after_purchase %} checked="checked"{% endif %}>
|
||||||
|
<label class="form-check-label" for="productlist-logout-after-purchase">Logout after purchase</label>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="deposit-wrapper">
|
<div id="deposit-wrapper">
|
||||||
<div id="deposit-input">
|
<div id="deposit-input">
|
||||||
|
@ -72,6 +76,21 @@
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
document.getElementById("productlist-logout-after-purchase").onchange = (e) => {
|
||||||
|
var append_query_string = "&logout_after_purchase=" + (e.target.checked ? "1" : "0");
|
||||||
|
[].forEach.call(document.getElementsByClassName("card"), (a) => {
|
||||||
|
console.log(a.href);
|
||||||
|
if (a.href) {
|
||||||
|
let url = new URL(a.href);
|
||||||
|
url.searchParams.delete("logout_after_purchase");
|
||||||
|
url.searchParams.append("logout_after_purchase", e.target.checked ? "1" : "0");
|
||||||
|
a.href = url.href;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
{{ super() }}
|
{{ super() }}
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Reference in a new issue