matekasse/Website/db.py

86 lines
2.4 KiB
Python
Raw Permalink Normal View History

from re import M
2024-03-06 20:31:00 +00:00
from markupsafe import escape
2023-07-28 21:30:45 +00:00
import sqlite3
2024-02-25 16:24:44 +00:00
from datetime import datetime
2023-07-28 21:30:45 +00:00
import click
from flask import current_app, g
2024-03-13 18:29:15 +00:00
def log(statement, user_id=None, before=None, after=None, change=None):
2024-02-25 16:24:44 +00:00
db = get_db()
c = db.cursor()
2024-03-19 22:52:49 +00:00
c.execute("INSERT INTO transaction_log (type, user_id, before, after, change) VALUES (?, ?, ?, ?, ?)", [ statement, user_id, before, after, change])
2024-02-25 16:24:44 +00:00
db.commit()
2024-03-01 20:59:30 +00:00
def add_user(after):
2024-02-25 16:24:44 +00:00
db = get_db()
c = db.cursor()
2024-03-06 20:38:53 +00:00
c.execute("INSERT or IGNORE INTO users (username, balance) VALUES (?, 0)", [after])
2024-03-01 20:59:30 +00:00
user_id = c.lastrowid
2024-03-06 20:38:53 +00:00
log("add_user", user_id=user_id, after=after)
2024-03-01 20:59:30 +00:00
db.commit()
def remove_user(user_id):
db = get_db()
c = db.cursor()
2024-03-06 20:38:53 +00:00
c.execute("SELECT * FROM users WHERE id = ?", [user_id])
2024-03-01 20:59:30 +00:00
user_name = c.fetchone()[1]
2024-03-06 20:38:53 +00:00
c.execute("SELECT * FROM tags WHERE userid = ?", [user_id])
2024-03-01 20:59:30 +00:00
for tag in c.fetchall():
remove_tag(tag[0])
2024-03-06 20:38:53 +00:00
c.execute("DELETE FROM users WHERE id = ?", [user_id])
log("remove_user", user_id=user_id, before=user_name)
2024-03-01 20:59:30 +00:00
db.commit()
def add_tag(user_id, tag_id):
db = get_db()
c = db.cursor()
2024-03-13 20:20:45 +00:00
c.execute("INSERT OR IGNORE INTO tags (tagid, userid) VALUES (?, ?)", [tag_id, user_id])
2024-03-01 20:59:30 +00:00
db.commit()
2024-03-13 19:50:34 +00:00
log("add_tag", after=tag_id, user_id=user_id)
2024-03-01 20:59:30 +00:00
def remove_tag(tag_id):
db = get_db()
c = db.cursor()
2024-03-06 20:38:53 +00:00
c.execute("SELECT * FROM tags WHERE tagid = ?", [tag_id])
2024-03-01 20:59:30 +00:00
user_id = c.fetchone()[1]
2024-03-06 20:38:53 +00:00
c.execute("DELETE FROM tags WHERE tagid = ?", [tag_id])
2024-03-13 19:50:34 +00:00
log("remove_tag", before=tag_id, user_id=user_id)
2024-03-01 20:59:30 +00:00
db.commit()
def change_balance(user_id, change):
db = get_db()
c = db.cursor()
2024-03-06 20:38:53 +00:00
c.execute("UPDATE users SET balance = balance + ? WHERE id=?", [change, user_id])
log("balance", user_id=user_id, change=change)
2024-02-25 16:24:44 +00:00
db.commit()
2023-07-28 21:30:45 +00:00
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
2023-07-28 21:30:45 +00:00
g.db.row_factory = sqlite3.Row
init_db()
2023-07-28 21:30:45 +00:00
return g.db
def close_db(e=None):
db = g.pop('db', None)
if db is not None:
db.close()
def init_db():
db = get_db()
with current_app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8'))
@click.command('init-db')
def init_db_command():
"""Clear the existing data and create new tables."""
init_db()
click.echo('Initialized the database.')