refactor database class

This commit is contained in:
WorldTeacher
2024-01-31 14:17:30 +01:00
parent 155958e187
commit 6fa71edc2d
3 changed files with 403 additions and 712 deletions

View File

@@ -24,21 +24,21 @@ class AdminCommands:
def create_admin(self):
salt = self.create_salt()
hashed_password = self.hash_password("admin")
self.db.create_user("admin", salt+hashed_password, "admin", salt)
self.db.createUser("admin", salt+hashed_password, "admin", salt)
def hash_password(self, password):
hashed = hashlib.sha256((password).encode("utf-8")).hexdigest()
return hashed
def list_users(self):
return self.db.get_users()
return self.db.getUsers()
def delete_user(self, username):
self.db.delete_user(username)
self.db.deleteUser(username)
def change_password(self, username, password):
hashed_password = self.hash_password(password)
self.db.change_password(username, hashed_password)
self.db.changePassword(username, hashed_password)
if __name__ == "__main__":

File diff suppressed because it is too large Load Diff

View File

@@ -1,87 +0,0 @@
import datetime
import os
import re
import sqlite3 as sql
import tempfile
import pickle
from src.logic.log import MyLogger
from icecream import ic
from typing import List, Tuple, Dict, Any, Optional, Union
from omegaconf import OmegaConf
from src.backend.db import CREATE_TABLE_APPARAT, CREATE_TABLE_MESSAGES, CREATE_TABLE_MEDIA, CREATE_TABLE_APPKONTOS, CREATE_TABLE_FILES, CREATE_TABLE_MESSAGES, CREATE_TABLE_PROF, CREATE_TABLE_USER, CREATE_TABLE_SUBJECTS
from src.logic.constants import SEMAP_MEDIA_ACCOUNTS
from src.logic.dataclass import ApparatData, BookData
config = OmegaConf.load("config.yaml")
logger = MyLogger(__name__)
def load_pickle(data):
return pickle.loads(data)
def dump_pickle(data):
return pickle.dumps(data)
def create_blob(data):
with open(data, "rb") as f:
return f.read()
def create_file(filename:str, blob):
with tempfile.TemporaryDirectory(delete=False) as tmpdir:
filepath = os.path.join(tmpdir, filename)
with open(filepath, "wb") as f:
f.write(blob)
return filepath
class Database:
def __init__(self, db_path: str = None):
if db_path is None:
self.db_path = config.database.path
else:
self.db_path = db_path
if self.get_db_contents() is None:
self.create_tables()
def get_db_contents(self):
try:
with sql.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM sqlite_master WHERE type='table'")
return cursor.fetchall()
except sql.OperationalError:
return None
def connect(self):
return sql.connect(self.db_path)
def close_connection(self, conn: sql.Connection):
conn.close()
def create_tables(self):
conn = self.connect()
cursor = conn.cursor()
cursor.execute(CREATE_TABLE_APPARAT)
cursor.execute(CREATE_TABLE_MESSAGES)
cursor.execute(CREATE_TABLE_MEDIA)
cursor.execute(CREATE_TABLE_APPKONTOS)
cursor.execute(CREATE_TABLE_FILES)
cursor.execute(CREATE_TABLE_PROF)
cursor.execute(CREATE_TABLE_USER)
cursor.execute(CREATE_TABLE_SUBJECTS)
conn.commit()
self.close_connection(conn)
def query_db(self, query: str, args: Tuple = (), one: bool = False):
conn = self.connect()
cursor = conn.cursor()
cursor.execute(query, args)
rv = cursor.fetchall()
conn.commit()
self.close_connection(conn)
return (rv[0] if rv else None) if one else rv