diff --git a/main.py b/main.py index c3cba4a..06bce37 100644 --- a/main.py +++ b/main.py @@ -1,20 +1,15 @@ -import queue +import queue, sqlite3, time, atexit, sys, uuid, json, urllib.parse, logging from flask import Flask, render_template, request, make_response, session from flask_socketio import SocketIO, join_room, leave_room from flask_session import Session -import sqlite3 -import time -import atexit -import sys -import uuid -import json -import urllib.parse from markupsafe import escape +#db_config db_path = 'mate.db' conn = sqlite3.connect(db_path, check_same_thread=False) c = conn.cursor() +#flask_config app = Flask(__name__) key = str(uuid.uuid4().hex) app.config['SESSION_TYPE'] = 'filesystem' @@ -22,8 +17,18 @@ app.config['SECRET_KEY'] = key Session(app) socketio = SocketIO(app) +#logging_config +logging.basicConfig(filename='matekasse.log', encoding='utf-8', level=logging.INFO) +db_log = logging.getLogger('db') +db_consol_handler = logging.StreamHandler() +db_consol_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) +db_log.addHandler(db_consol_handler) +website_log = logging.getLogger('website') +website_consol_handler = logging.StreamHandler() +website_consol_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) +website_log.addHandler(db_consol_handler) - +#var status = True users = queue.Queue() finished = None @@ -111,7 +116,9 @@ def remove_user(): if users != []: user_name = users[0][1] c.execute(f"DELETE FROM tags WHERE userid={user_id}") + db_log.info(f"Deleted all tags from user {user_id}") c.execute(f"DELETE FROM users WHERE id={user_id}") + db_log.info(f"Deleted user {user_id}") conn.commit() socketio.emit("update", "update") return f'

Deleted user {user_name}

return to the tags and user list' @@ -139,8 +146,13 @@ def change(): except ValueError: return '

Error: 095

tags and user list' c.execute(f"SELCET * FROM users WHERE id={user_id}") - if c.fetchall != []: + users = c.fetchall() + if users != []: + balance_old = users[0][2] c.execute(f"UPDATE users SET balance = balance + {change} WHERE id={user_id}") + c.execute(f"SELECT * FROM users WHERE id={user_id}") + user = c.fetchall()[0] + db_log.info(f"Changed the balance from user {user[0]} from {balance_old} to {user[2]}") socketio.emit("update", "update") return """ @@ -254,7 +266,9 @@ def get_id(): else: c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})") message = f"Added {tag_id} to {username}" + db_log.info(message) finished = queue_item + conn.commit() return make_response(json.dumps({"mode":"2","username":username,"code":"1"})) elif state == "remove": c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = {user})") @@ -262,10 +276,12 @@ def get_id(): if tags != []: c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = {user}) ") message = f"Removed {tag_id} from {username}" + db_log.info(message) finished = queue_item + conn.commit() return make_response(json.dumps({"mode":"2","username":username,"code":"2"})) else: - message = "Tag does not exist" + message = "054" finished = queue_item return make_response(json.dumps({"mode":"0","error":"054"})) finished = queue_item @@ -274,13 +290,20 @@ def get_id(): elif tag_list != []: tag = tag_list[0] - if users.qsize() == 0: - c.execute(f"UPDATE users SET balance = balance - 1 WHERE id={tag[1]}") - conn.commit() - c.execute(f"SELECT * FROM users WHERE id={tag[1]}") - user = c.fetchall()[0] - socketio.emit("update", "upfinished = queue_itemdate") - return make_response(json.dumps({"mode":"1", "username":user[1], "balance":user[2]})) + c.execute(f"SELECT * FROM users WHERE id={tag[1]}") + users = c.fetchall() + if users != []: + balance_old = users[0][2] + if users.qsize() == 0: + c.execute(f"UPDATE users SET balance = balance - 1 WHERE id={tag[1]}") + conn.commit() + c.execute(f"SELECT * FROM users WHERE id={tag[1]}") + user = c.fetchall()[0] + db_log.info(f"Changed the balance from user {user[0]} from {balance_old} to {user[2]}") + socketio.emit("update", "update") + return make_response(json.dumps({"mode":"1", "username":user[1], "balance":user[2]})) + else: + return make_response(json.dumps({"mode":"0", "error":"043"})) socketio.emit("update", "update") return make_response(json.dumps({"mode":"0","error":"054"}))