Added Logs and db info

This commit is contained in:
2000-Trek 2023-06-22 12:29:27 +02:00
parent 74850ed77b
commit 6a348c3108

59
main.py
View file

@ -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 import Flask, render_template, request, make_response, session
from flask_socketio import SocketIO, join_room, leave_room from flask_socketio import SocketIO, join_room, leave_room
from flask_session import Session from flask_session import Session
import sqlite3
import time
import atexit
import sys
import uuid
import json
import urllib.parse
from markupsafe import escape from markupsafe import escape
#db_config
db_path = 'mate.db' db_path = 'mate.db'
conn = sqlite3.connect(db_path, check_same_thread=False) conn = sqlite3.connect(db_path, check_same_thread=False)
c = conn.cursor() c = conn.cursor()
#flask_config
app = Flask(__name__) app = Flask(__name__)
key = str(uuid.uuid4().hex) key = str(uuid.uuid4().hex)
app.config['SESSION_TYPE'] = 'filesystem' app.config['SESSION_TYPE'] = 'filesystem'
@ -22,8 +17,18 @@ app.config['SECRET_KEY'] = key
Session(app) Session(app)
socketio = SocketIO(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 status = True
users = queue.Queue() users = queue.Queue()
finished = None finished = None
@ -111,7 +116,9 @@ def remove_user():
if users != []: if users != []:
user_name = users[0][1] user_name = users[0][1]
c.execute(f"DELETE FROM tags WHERE userid={user_id}") 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}") c.execute(f"DELETE FROM users WHERE id={user_id}")
db_log.info(f"Deleted user {user_id}")
conn.commit() conn.commit()
socketio.emit("update", "update") socketio.emit("update", "update")
return f'<p>Deleted user {user_name}</p><a href="/list">return to the tags and user list</a>' return f'<p>Deleted user {user_name}</p><a href="/list">return to the tags and user list</a>'
@ -139,8 +146,13 @@ def change():
except ValueError: except ValueError:
return '<p>Error: 095</p><a href="/list">tags and user list</a>' return '<p>Error: 095</p><a href="/list">tags and user list</a>'
c.execute(f"SELCET * FROM users WHERE id={user_id}") 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"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") socketio.emit("update", "update")
return """<html> return """<html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script>
@ -254,7 +266,9 @@ def get_id():
else: else:
c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})") c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, {user})")
message = f"Added {tag_id} to {username}" message = f"Added {tag_id} to {username}"
db_log.info(message)
finished = queue_item finished = queue_item
conn.commit()
return make_response(json.dumps({"mode":"2","username":username,"code":"1"})) return make_response(json.dumps({"mode":"2","username":username,"code":"1"}))
elif state == "remove": elif state == "remove":
c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = {user})") c.execute(f"SELECT * FROM tags WHERE (tagid = {tag_id} AND userid = {user})")
@ -262,10 +276,12 @@ def get_id():
if tags != []: if tags != []:
c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = {user}) ") c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = {user}) ")
message = f"Removed {tag_id} from {username}" message = f"Removed {tag_id} from {username}"
db_log.info(message)
finished = queue_item finished = queue_item
conn.commit()
return make_response(json.dumps({"mode":"2","username":username,"code":"2"})) return make_response(json.dumps({"mode":"2","username":username,"code":"2"}))
else: else:
message = "Tag does not exist" message = "054"
finished = queue_item finished = queue_item
return make_response(json.dumps({"mode":"0","error":"054"})) return make_response(json.dumps({"mode":"0","error":"054"}))
finished = queue_item finished = queue_item
@ -274,13 +290,20 @@ def get_id():
elif tag_list != []: elif tag_list != []:
tag = tag_list[0] tag = tag_list[0]
if users.qsize() == 0: c.execute(f"SELECT * FROM users WHERE id={tag[1]}")
c.execute(f"UPDATE users SET balance = balance - 1 WHERE id={tag[1]}") users = c.fetchall()
conn.commit() if users != []:
c.execute(f"SELECT * FROM users WHERE id={tag[1]}") balance_old = users[0][2]
user = c.fetchall()[0] if users.qsize() == 0:
socketio.emit("update", "upfinished = queue_itemdate") c.execute(f"UPDATE users SET balance = balance - 1 WHERE id={tag[1]}")
return make_response(json.dumps({"mode":"1", "username":user[1], "balance":user[2]})) 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") socketio.emit("update", "update")
return make_response(json.dumps({"mode":"0","error":"054"})) return make_response(json.dumps({"mode":"0","error":"054"}))