Compare commits

...

3 commits

Author SHA1 Message Date
2d6d14ebba new log 2023-11-25 00:05:33 +01:00
dcd7a71d83 added gitlab link to the Documentation 2023-11-24 21:34:35 +01:00
26a453b507 added gitlab link to the Documentation 2023-11-24 21:33:32 +01:00
4 changed files with 43 additions and 31 deletions

View file

@ -4,17 +4,25 @@ from flask_socketio import SocketIO, join_room, leave_room
from flask_session import Session from flask_session import Session
from markupsafe import escape from markupsafe import escape
from .db import get_db from .db import get_db
from datetime import datetime
#flask_config #flask_config
DATABASE = './Website/mate.db' DATABASE = './Website/mate.db'
def create_logs(app): #def create_logs(app):
now = datetime.datetime.now().strftime('%d-%m-%Y-%H-%M-%S') # now = datetime.datetime.now().strftime('%d-%m-%Y-%H-%M-%S')
logging.basicConfig(filename=f"logs/matekasse-{now}.log",filemode='w', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ,encoding='utf-8', level=logging.INFO) # logging.basicConfig(filename=f"logs/matekasse-{now}.log",filemode='w', format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' ,encoding='utf-8', level=logging.INFO)
app.logger = logging.getLogger('db') # app.logger = logging.getLogger('db')
#
# app.logger.info("Website is starting")
app.logger.info("Website is starting") def log(type=None, userid=None, before=None, after=None):
db = get_db()
c = db.cursor()
c.execute("INSERT or IGNORE INTO transaction_log (timestamp, userid, type, before, after) VALUES (?, ?, ?, ?, ?)", [datetime.now(), userid, type, before, after])
db.commit()
db.close()
def create_app(test_config=None): def create_app(test_config=None):
app = Flask(__name__) app = Flask(__name__)
@ -30,8 +38,8 @@ def create_app(test_config=None):
os.makedirs(app.instance_path) os.makedirs(app.instance_path)
except OSError: except OSError:
pass pass
with app.app_context(): #with app.app_context():
create_logs(app) # create_logs(app)
Session(app) Session(app)
socketio = SocketIO(app) socketio = SocketIO(app)
@ -93,6 +101,17 @@ def create_app(test_config=None):
<br></br> <br></br>
''' + text + '</html>' ''' + text + '</html>'
@app.route("/transactionlist")
def transactionlist():
db = get_db()
c = db.cursor()
text = ""
c.execute("SELECT * FROM transaction_log ORDER BY ROWID DESC LIMIT 100")
transactionlist = c.fetchall()
for i in transactionlist:
text = text + f"<p>{i[0]} userid: {i[1]} {i[2]} {i[3]} to {i[4]}</p>"
return text
@app.route("/list/user", methods=['GET']) @app.route("/list/user", methods=['GET'])
def user_info(): def user_info():
db = get_db() db = get_db()
@ -147,8 +166,8 @@ def create_app(test_config=None):
c.execute(f"DELETE FROM tags WHERE userid=?", [user_id]) c.execute(f"DELETE FROM tags WHERE userid=?", [user_id])
app.logger.info(f"Deleted all tags from user ?", [user_id]) app.logger.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])
app.logger.info(f"Deleted user ?", [user_id])
db.commit() db.commit()
log(type="removeuser", userid=user_id, before=user_name)
socketio.emit("update", "update") socketio.emit("update", "update")
return f'<title>remove user</title><p><p><a href="/list">user and tag list</a> | <a href="/documentation">Documentation</a></p> <p>Deleted user {escape(user_name)}</p><a href="/list">return to the tags and user list</a></p>' return f'<title>remove user</title><p><p><a href="/list">user and tag list</a> | <a href="/documentation">Documentation</a></p> <p>Deleted user {escape(user_name)}</p><a href="/list">return to the tags and user list</a></p>'
else: else:
@ -168,7 +187,7 @@ def create_app(test_config=None):
socketio.emit("update", "update") socketio.emit("update", "update")
c.execute(f"SELECT * FROM users WHERE username=?", [username]) c.execute(f"SELECT * FROM users WHERE username=?", [username])
user = c.fetchone() user = c.fetchone()
app.logger.info(f"Added user id: {user[0]} name: {user[2]}") log(type="adduser", userid=user[0], after=user[1])
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>
<script type="text/javascript" charset="utf-8"> <script type="text/javascript" charset="utf-8">
@ -198,7 +217,7 @@ def create_app(test_config=None):
db.commit() db.commit()
c.execute(f"SELECT * FROM users WHERE id={user_id}") c.execute(f"SELECT * FROM users WHERE id={user_id}")
user = c.fetchone() user = c.fetchone()
app.logger.info(f"Changed the balance from user {user[0]} from {balance_old} to {user[2]}") log(type="balance", userid=user[0], before=balance_old, after=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>
@ -304,7 +323,7 @@ def create_app(test_config=None):
db.commit() db.commit()
c.execute(f"SELECT * FROM users WHERE id = {userid}") c.execute(f"SELECT * FROM users WHERE id = {userid}")
user_new = c.fetchone() user_new = c.fetchone()
app.logger.info(f"Changed the balance from user {user[0]} from {user[2]} to {user_new[2]}") log(type="balance", userid=user[0], before=user[2], after=user_new[2])
socketio.emit("update", "update") socketio.emit("update", "update")
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]})) return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]}))
else: else:
@ -327,7 +346,7 @@ def create_app(test_config=None):
state = queue_item[1] state = queue_item[1]
c.execute(f"SELECT * FROM users WHERE id=?", [user]) c.execute(f"SELECT * FROM users WHERE id=?", [user])
username = c.fetchone()[1] user_id = c.fetchone()[0]
if state == "add": if state == "add":
c.execute(f"SELECT * FROM tags WHERE tagid={tag_id}") c.execute(f"SELECT * FROM tags WHERE tagid={tag_id}")
if c.fetchall() != []: if c.fetchall() != []:
@ -336,8 +355,7 @@ def create_app(test_config=None):
return make_response(json.dumps({"mode":"error","error":"170"})) return make_response(json.dumps({"mode":"error","error":"170"}))
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}" log(type="addtag", userid=user_id ,after=tag_id)
app.logger.info(message)
finished = queue_item finished = queue_item
db.commit() db.commit()
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was added"})) return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was added"}))
@ -346,8 +364,7 @@ def create_app(test_config=None):
tags = c.fetchall() tags = c.fetchall()
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}" log(type="removetag", userid=user_id, before=tag_id)
app.logger.info(message)
finished = queue_item finished = queue_item
db.commit() db.commit()
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was removed"})) return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was removed"}))
@ -370,7 +387,7 @@ def create_app(test_config=None):
db.commit() db.commit()
c.execute(f"SELECT * FROM users WHERE id={tag[1]}") c.execute(f"SELECT * FROM users WHERE id={tag[1]}")
user = c.fetchone() user = c.fetchone()
app.logger.info(f"Changed the balance from user {user[0]} from {balance_old} to {user[2]}") log(type="balance", userid=user[0], before=balance_old, after=user[2])
socketio.emit("update", "update") socketio.emit("update", "update")
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user[2]})) return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user[2]}))
else: else:

View file

@ -1,13 +0,0 @@
import sys
filename = sys.argv[1]
db_log = []
with open(filename, 'r') as f:
for line in f:
if "db" in line:
db_log.append(line)
with open("restore_log", "x") as f:
for i in db_log:
f.write(i)
f.close

View file

@ -6,9 +6,16 @@ CREATE TABLE IF NOT EXISTS "users" (
PRIMARY KEY("id") PRIMARY KEY("id")
); );
CREATE TABLE IF NOT EXISTS "tags" ( CREATE TABLE IF NOT EXISTS "tags" (
"tagid" INEGER NOT NULL, "tagid" INTEGER NOT NULL,
"userid" INTEGER, "userid" INTEGER,
FOREIGN KEY("userid") REFERENCES "users"("id"), FOREIGN KEY("userid") REFERENCES "users"("id"),
PRIMARY KEY("tagid") PRIMARY KEY("tagid")
); );
CREATE TABLE IF NOT EXISTS "transaction_log" (
"timestamp" INTEGER NOT NULL,
"userid" INTEGER NOT NULL,
"type" TEXT NOT NULL,
"before" TEXT,
"after" TEXT
);
COMMIT; COMMIT;

View file

@ -9,6 +9,7 @@
<body> <body>
<div id="Infos"> <div id="Infos">
<p> <a href="/">index page</a> | <a href="/list">user and tag list</a></p> <p> <a href="/">index page</a> | <a href="/list">user and tag list</a></p>
<p><a href="https://hannover.ccc.de/gitlab/anton/matekasse">https://hannover.ccc.de/gitlab/anton/matekasse</a></p>
</div> </div>
<h1 class="header"> <u>Documentation</u> </h1> <h1 class="header"> <u>Documentation</u> </h1>