feat: refactor user settings #4

Merged
s3lph merged 8 commits from weva/matemat:main into main 2024-12-07 22:19:40 +01:00
6 changed files with 107 additions and 74 deletions
Showing only changes of commit 2883593eeb - Show all commits

View file

@ -74,10 +74,12 @@ class DatabaseWrapper(object):
# Enable foreign key enforcement # Enable foreign key enforcement
cursor = self._sqlite_db.cursor() cursor = self._sqlite_db.cursor()
cursor.execute('PRAGMA foreign_keys = 1') cursor.execute('PRAGMA foreign_keys=ON')
def _upgrade(self, from_version: int, to_version: int) -> None: def _upgrade(self, from_version: int, to_version: int) -> None:
with self.transaction() as c: with self.transaction() as c:
c.execute('PRAGMA foreign_keys=OFF')
c.execute('PRAGMA legacy_alter_table=ON')
if from_version <= 1 and to_version >= 2: if from_version <= 1 and to_version >= 2:
migrate_schema_1_to_2(c) migrate_schema_1_to_2(c)
if from_version <= 2 and to_version >= 3: if from_version <= 2 and to_version >= 3:
@ -98,6 +100,8 @@ class DatabaseWrapper(object):
migrate_schema_8_to_9(c) migrate_schema_8_to_9(c)
if from_version <= 9 and to_version >= 10: if from_version <= 9 and to_version >= 10:
migrate_schema_9_to_10(c) migrate_schema_9_to_10(c)
c.execute('PRAGMA foreign_key_check')
c.execute('PRAGMA foreign_keys=ON')
def connect(self) -> None: def connect(self) -> None:
if self.is_connected(): if self.is_connected():

View file

@ -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))

View file

@ -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))

View file

@ -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

View file

@ -10,6 +10,8 @@ const Mode = {
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;
@ -81,16 +83,16 @@ 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); window.location.href = '/transfer?target=' + target_user + '&n=' + parseInt(deposit) + append_query_string;
break; break;
} }
mode = Mode.Deposit; mode = Mode.Deposit;
@ -127,7 +129,9 @@ deposit_key = (k) => {
} }
}; };
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]);

View file

@ -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 %}