From 1aef1b3e3afc5500d75c4b002bd269131291a604 Mon Sep 17 00:00:00 2001 From: WorldTeacher <41587052+WorldTeacher@users.noreply.github.com> Date: Tue, 18 Jun 2024 14:11:31 +0200 Subject: [PATCH] fixes + updates add ascii-lowercase symbols that can be at the end of a query add inspect module to name database logger based on calling class --- src/backend/database.py | 78 ++++++++++++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 13 deletions(-) diff --git a/src/backend/database.py b/src/backend/database.py index 88c69b6..3a413e8 100644 --- a/src/backend/database.py +++ b/src/backend/database.py @@ -1,6 +1,5 @@ -import datetime import os -import re +import inspect import sqlite3 as sql import tempfile from pathlib import Path @@ -30,8 +29,7 @@ from src.logic.log import MyLogger from src.utils import create_blob, dump_pickle, load_pickle config = OmegaConf.load("config.yaml") -logger = MyLogger(__name__) - +ascii_lowercase = "abcdefghijklmnopqrstuvwxyz0123456789)" class Database: """ @@ -45,14 +43,23 @@ class Database: Args: db_path (str, optional): Optional Path for testing / specific purposes. Defaults to None. """ + caller_frame = inspect.stack()[1] + + script_name = ( + caller_frame.filename.replace("\\", "/").split("/")[-1].split(".")[0] + ) + print(script_name) + name = f"Database.{script_name}" + self.logger = MyLogger(name) if db_path is None: self.db_path = config.database.path + config.database.name self.db_path = self.db_path.replace("~", str(Path.home())) else: self.db_path = db_path - if self.get_db_contents() is None: - logger.log_critical("Database does not exist, creating tables") + if self.get_db_contents() == []: + self.logger.log_critical("Database does not exist, creating tables") self.create_tables() + self.insertSubjects() def get_db_contents(self) -> Union[List[Tuple], None]: """ @@ -119,7 +126,7 @@ class Database: """ conn = self.connect() cursor = conn.cursor() - logger.log_info(f"Inserting {params} into database with query {query}") + self.logger.log_info(f"Inserting {params} into database with query {query}") cursor.execute(query, params) conn.commit() self.close_connection(conn) @@ -140,7 +147,8 @@ class Database: """ conn = self.connect() cursor = conn.cursor() - logger.log_info(f"Querying database with query {query}, args: {args}") + self.logger.log_info(f"Querying database with query {query}, args: {args}") + cursor.execute(query, args) rv = cursor.fetchall() conn.commit() @@ -471,6 +479,41 @@ class Database: data = self.query_db("SELECT DISTINCT erstellsemester FROM semesterapparat") return [i[0] for i in data] + def insertSubjects(self): + print("Inserting subjects") + subjects = [ + "Biologie", + "Chemie", + "Deutsch", + "Englisch", + "Erziehungswissenschaft", + "Französisch", + "Geographie", + "Geschichte", + "Gesundheitspädagogik", + "Haushalt / Textil", + "Kunst", + "Mathematik / Informatik", + "Medien in der Bildung", + "Musik", + "Philosophie", + "Physik", + "Poitikwissenschaft", + "Prorektorat Lehre und Studium", + "Psychologie", + "Soziologie", + "Sport", + "Technik", + "Theologie", + "Wirtschaftslehre", + ] + conn = self.connect() + cursor = conn.cursor() + for subject in subjects: + cursor.execute("INSERT INTO subjects (name) VALUES (?)", (subject,)) + conn.commit() + self.close_connection(conn) + def getSubjects(self): """Get all the subjects in the database @@ -553,7 +596,7 @@ class Database: Args: message_id (str): the id of the message """ - logger.log_info(f"Deleting message with id {message_id}") + self.logger.log_info(f"Deleting message with id {message_id}") self.query_db("DELETE FROM messages WHERE id=?", (message_id,)) # Prof data @@ -746,7 +789,7 @@ class Database: "SELECT appnr FROM semesterapparat WHERE deletion_status=0" ) numbers = [i[0] for i in numbers] - logger.log_info(f"Currently used apparat numbers: {numbers}") + self.logger.log_info(f"Currently used apparat numbers: {numbers}") return numbers def setNewSemesterDate(self, app_id: Union[str, int], newDate, dauerapp=False): @@ -808,7 +851,7 @@ class Database: prof_id = self.getProfId(apparat.profname) # ic(prof_id) query = f"INSERT OR IGNORE INTO semesterapparat (appnr, name, erstellsemester, dauer, prof_id, fach,deletion_status,konto) VALUES ('{apparat.appnr}', '{apparat.appname}', '{apparat.semester}', '{apparat.dauerapp}', {prof_id}, '{apparat.app_fach}', '{0}', '{SEMAP_MEDIA_ACCOUNTS[apparat.appnr]}')" - logger.log_info(query) + self.logger.log_info(query) self.query_db(query) return self.getApparatId(apparat.appname) @@ -954,7 +997,7 @@ class Database: apparat_data.apparat_adis_id, apparat_data.appnr, ) - logger.log_info(f"Updating apparat with query {query} and params {params}") + self.logger.log_info(f"Updating apparat with query {query} and params {params}") self.query_db(query, params) def checkApparatExists(self, apparat_name: str): @@ -1018,6 +1061,7 @@ class Database: result_a = tuple(result_a) result[result.index(orig_value)] = result_a self.close_connection(conn) + self.logger.log_info(f"Query result: {result}") return result if "deletable" in kwargs.keys(): @@ -1052,8 +1096,16 @@ class Database: ) # remove all x="" parts from the query where x is a key in kwargs query = query[:-5] + query = query.strip() + # check if query ends with lowercase letter or a '. if not, remove last symbol and try again + while query[-1] not in ascii_lowercase and query[-1] != "'": + query = query[:-1] + query = query.strip() + print(query) - return __query(query) + res = __query(query) + print(res) + return res # Admin data def getUser(self):