Compare commits

..

No commits in common. "2d6d14ebba576f693a248b64fae35e41f81f2ec3" and "5b5dd1a17bf9df3948ff4dc4932db8b30a7b633e" have entirely different histories.

4 changed files with 31 additions and 43 deletions

View file

@ -4,25 +4,17 @@ from flask_socketio import SocketIO, join_room, leave_room
from flask_session import Session
from markupsafe import escape
from .db import get_db
from datetime import datetime
#flask_config
DATABASE = './Website/mate.db'
#def create_logs(app):
# 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)
# app.logger = logging.getLogger('db')
#
# app.logger.info("Website is starting")
def create_logs(app):
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)
app.logger = logging.getLogger('db')
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()
app.logger.info("Website is starting")
def create_app(test_config=None):
app = Flask(__name__)
@ -38,8 +30,8 @@ def create_app(test_config=None):
os.makedirs(app.instance_path)
except OSError:
pass
#with app.app_context():
# create_logs(app)
with app.app_context():
create_logs(app)
Session(app)
socketio = SocketIO(app)
@ -101,17 +93,6 @@ def create_app(test_config=None):
<br></br>
''' + 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'])
def user_info():
db = get_db()
@ -166,8 +147,8 @@ def create_app(test_config=None):
c.execute(f"DELETE FROM tags WHERE userid=?", [user_id])
app.logger.info(f"Deleted all tags from user ?", [user_id])
c.execute(f"DELETE FROM users WHERE id=?", [user_id])
app.logger.info(f"Deleted user ?", [user_id])
db.commit()
log(type="removeuser", userid=user_id, before=user_name)
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>'
else:
@ -187,7 +168,7 @@ def create_app(test_config=None):
socketio.emit("update", "update")
c.execute(f"SELECT * FROM users WHERE username=?", [username])
user = c.fetchone()
log(type="adduser", userid=user[0], after=user[1])
app.logger.info(f"Added user id: {user[0]} name: {user[2]}")
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 type="text/javascript" charset="utf-8">
@ -217,7 +198,7 @@ def create_app(test_config=None):
db.commit()
c.execute(f"SELECT * FROM users WHERE id={user_id}")
user = c.fetchone()
log(type="balance", userid=user[0], before=balance_old, after=user[2])
app.logger.info(f"Changed the balance from user {user[0]} from {balance_old} to {user[2]}")
socketio.emit("update", "update")
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>
@ -323,7 +304,7 @@ def create_app(test_config=None):
db.commit()
c.execute(f"SELECT * FROM users WHERE id = {userid}")
user_new = c.fetchone()
log(type="balance", userid=user[0], before=user[2], after=user_new[2])
app.logger.info(f"Changed the balance from user {user[0]} from {user[2]} to {user_new[2]}")
socketio.emit("update", "update")
return make_response(json.dumps({"mode":"balance", "username":user[1], "balance":user_new[2]}))
else:
@ -346,7 +327,7 @@ def create_app(test_config=None):
state = queue_item[1]
c.execute(f"SELECT * FROM users WHERE id=?", [user])
user_id = c.fetchone()[0]
username = c.fetchone()[1]
if state == "add":
c.execute(f"SELECT * FROM tags WHERE tagid={tag_id}")
if c.fetchall() != []:
@ -355,7 +336,8 @@ def create_app(test_config=None):
return make_response(json.dumps({"mode":"error","error":"170"}))
else:
c.execute(f"INSERT OR IGNORE INTO tags (tagid, userid) VALUES ({tag_id}, ?)", [user])
log(type="addtag", userid=user_id ,after=tag_id)
message = f"Added {tag_id} to {username}"
app.logger.info(message)
finished = queue_item
db.commit()
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was added"}))
@ -364,7 +346,8 @@ def create_app(test_config=None):
tags = c.fetchall()
if tags != []:
c.execute(f"DELETE FROM tags WHERE (tagid = {tag_id} AND userid = ?)", [user])
log(type="removetag", userid=user_id, before=tag_id)
message = f"Removed {tag_id} from {username}"
app.logger.info(message)
finished = queue_item
db.commit()
return make_response(json.dumps({"mode":"message","username":"{}".format(username),"message":"A tag was removed"}))
@ -387,7 +370,7 @@ def create_app(test_config=None):
db.commit()
c.execute(f"SELECT * FROM users WHERE id={tag[1]}")
user = c.fetchone()
log(type="balance", userid=user[0], before=balance_old, after=user[2])
app.logger.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":"balance", "username":user[1], "balance":user[2]}))
else:

View file

@ -0,0 +1,13 @@
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,16 +6,9 @@ CREATE TABLE IF NOT EXISTS "users" (
PRIMARY KEY("id")
);
CREATE TABLE IF NOT EXISTS "tags" (
"tagid" INTEGER NOT NULL,
"tagid" INEGER NOT NULL,
"userid" INTEGER,
FOREIGN KEY("userid") REFERENCES "users"("id"),
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;

View file

@ -9,7 +9,6 @@
<body>
<div id="Infos">
<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>
<h1 class="header"> <u>Documentation</u> </h1>