fix broken files after faulty update
This commit is contained in:
@@ -5,9 +5,9 @@ import tempfile
|
||||
from pathlib import Path
|
||||
from src import settings
|
||||
from typing import Any, Dict, List, Optional, Tuple, Union
|
||||
# from icecream import ic
|
||||
from omegaconf import OmegaConf
|
||||
import datetime
|
||||
from src import logger
|
||||
from src.backend.db import (
|
||||
CREATE_ELSA_FILES_TABLE,
|
||||
CREATE_ELSA_MEDIA_TABLE,
|
||||
@@ -22,15 +22,14 @@ from src.backend.db import (
|
||||
CREATE_TABLE_USER,
|
||||
)
|
||||
from src.errors import AppPresentError, NoResultError
|
||||
from src.logic import ApparatData, BookData, Prof, Apparat, ELSA, logger as log
|
||||
from src.logic import ApparatData, BookData, Prof, Apparat, ELSA
|
||||
from src.logic.constants import SEMAP_MEDIA_ACCOUNTS
|
||||
from src.utils import create_blob, dump_pickle, load_pickle
|
||||
from .semester import generateSemesterByDate
|
||||
from icecream import ic
|
||||
from string import ascii_lowercase as lower, digits
|
||||
from .semester import Semester
|
||||
from string import ascii_lowercase as lower, digits, punctuation
|
||||
|
||||
|
||||
ascii_lowercase = lower + digits
|
||||
ascii_lowercase = lower + digits + punctuation
|
||||
# get the line that called the function
|
||||
class Database:
|
||||
database = settings.database
|
||||
@@ -62,7 +61,7 @@ class Database:
|
||||
# print(path)
|
||||
os.makedirs(path)
|
||||
if self.get_db_contents() == []:
|
||||
log.critical("Database does not exist, creating tables")
|
||||
logger.critical("Database does not exist, creating tables")
|
||||
self.create_tables()
|
||||
self.insertSubjects()
|
||||
|
||||
@@ -146,12 +145,12 @@ class Database:
|
||||
"""
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
log.info(f"Inserting {params} into database with query {query}")
|
||||
logger.debug(f"Inserting {params} into database with query {query}")
|
||||
cursor.execute(query, params)
|
||||
conn.commit()
|
||||
self.close_connection(conn)
|
||||
|
||||
@log.catch
|
||||
@logger.catch
|
||||
def query_db(
|
||||
self, query: str, args: Tuple = (), one: bool = False
|
||||
) -> Union[Tuple, List[Tuple]]:
|
||||
@@ -181,14 +180,14 @@ class Database:
|
||||
# if "INSERT" in query:
|
||||
# log_message = f"Querying database with query {query}"
|
||||
|
||||
log.info(log_message)
|
||||
logger.debug(log_message)
|
||||
try:
|
||||
cursor.execute(query, args)
|
||||
rv = cursor.fetchall()
|
||||
conn.commit()
|
||||
self.close_connection(conn)
|
||||
except sql.OperationalError as e:
|
||||
log.error(f"Error in query: {e}")
|
||||
logger.error(f"Error in query: {e}")
|
||||
return None
|
||||
return (rv[0] if rv else None) if one else rv
|
||||
|
||||
@@ -211,7 +210,7 @@ class Database:
|
||||
t_query = (
|
||||
f"SELECT bookdata FROM media WHERE app_id={app_id} AND prof_id={prof_id}"
|
||||
)
|
||||
log.info(t_query)
|
||||
logger.debug(t_query)
|
||||
# # print(t_query)
|
||||
result = cursor.execute(t_query).fetchall()
|
||||
result = [load_pickle(i[0]) for i in result]
|
||||
@@ -238,7 +237,7 @@ class Database:
|
||||
params = (converted, app_id, prof_id, 0)
|
||||
cursor.execute(query, params)
|
||||
logMessage = f"Added book with signature {bookdata.signature} to database, data: {converted}"
|
||||
log.info(logMessage)
|
||||
logger.info(logMessage)
|
||||
conn.commit()
|
||||
self.close_connection(conn)
|
||||
|
||||
@@ -307,7 +306,7 @@ class Database:
|
||||
list[tuple[BookData, int]]: A list of tuples containing the wrapped Metadata and the id of the book
|
||||
"""
|
||||
rdata = self.query_db("SELECT * FROM media WHERE deleted=0")
|
||||
# ic(rdata, len(rdata))
|
||||
# logger.debug(rdata, len(rdata))
|
||||
mode = 0
|
||||
if len(data) == 1:
|
||||
if "signature" in data.keys():
|
||||
@@ -335,7 +334,7 @@ class Database:
|
||||
and data["title"] in bookdata.title
|
||||
):
|
||||
ret.append((bookdata, app_id, prof_id))
|
||||
# ic(ret)
|
||||
# logger.debug(ret)
|
||||
return ret
|
||||
|
||||
def setAvailability(self, book_id: str, available: str):
|
||||
@@ -514,7 +513,7 @@ class Database:
|
||||
(app_id, prof_id),
|
||||
)
|
||||
|
||||
def getSemersters(self) -> list[str]:
|
||||
def getSemesters(self) -> list[str]:
|
||||
"""Return all the unique semesters in the database
|
||||
|
||||
Returns:
|
||||
@@ -640,7 +639,7 @@ class Database:
|
||||
Args:
|
||||
message_id (str): the id of the message
|
||||
"""
|
||||
log.info(f"Deleting message with id {message_id}")
|
||||
logger.debug(f"Deleting message with id {message_id}")
|
||||
self.query_db("DELETE FROM messages WHERE id=?", (message_id,))
|
||||
|
||||
# Prof data
|
||||
@@ -710,7 +709,6 @@ class Database:
|
||||
(profname.replace(",", ""),),
|
||||
one=True,
|
||||
)
|
||||
print(data)
|
||||
person = Prof()
|
||||
return person.from_tuple(data)
|
||||
|
||||
@@ -770,20 +768,21 @@ class Database:
|
||||
if result is None:
|
||||
raise NoResultError("No result found")
|
||||
apparat = ApparatData()
|
||||
apparat.appname = result[1]
|
||||
apparat.appnr = result[4]
|
||||
apparat.dauerapp = True if result[7] == 1 else False
|
||||
prof_data = self.getProfData(self.getProfNameById(result[2]))
|
||||
apparat.profname = self.getProfNameById(result[2])
|
||||
apparat.prof_mail = prof_data.mail
|
||||
apparat.prof_tel = prof_data.telnr
|
||||
apparat.prof_title = prof_data.title
|
||||
apparat.app_fach = result[3]
|
||||
apparat.erstellsemester = result[5]
|
||||
apparat.semester = result[8]
|
||||
apparat.deleted = result[9]
|
||||
apparat.apparat_adis_id = result[11]
|
||||
apparat.prof_adis_id = result[12]
|
||||
apparat.apparat.id = result[0]
|
||||
apparat.apparat.name = result[1]
|
||||
apparat.apparat.appnr = result[4]
|
||||
apparat.apparat.eternal = True if result[7] == 1 else False
|
||||
apparat.prof = self.getProfData(self.getProfNameById(result[2]))
|
||||
apparat.prof.fullname = self.getProfNameById(result[2])
|
||||
apparat.apparat.prof_id = result[2]
|
||||
|
||||
apparat.apparat.subject = result[3]
|
||||
apparat.apparat.created_semester = result[5]
|
||||
apparat.apparat.extend_until = result[8]
|
||||
apparat.apparat.deleted = result[9]
|
||||
apparat.apparat.apparat_id_adis = result[11]
|
||||
apparat.apparat.prof_id_adis = result[12]
|
||||
apparat.apparat.konto = result[13]
|
||||
return apparat
|
||||
|
||||
def getUnavailableApparatNumbers(self) -> List[int]:
|
||||
@@ -796,7 +795,7 @@ class Database:
|
||||
"SELECT appnr FROM semesterapparat WHERE deletion_status=0"
|
||||
)
|
||||
numbers = [i[0] for i in numbers]
|
||||
log.info(f"Currently used apparat numbers: {numbers}")
|
||||
logger.info(f"Currently used apparat numbers: {numbers}")
|
||||
return numbers
|
||||
|
||||
def setNewSemesterDate(self, app_id: Union[str, int], newDate, dauerapp=False):
|
||||
@@ -850,21 +849,22 @@ class Database:
|
||||
Returns:
|
||||
Optional[int]: the id of the apparat
|
||||
"""
|
||||
ic(apparat)
|
||||
prof = self.getProfByName(apparat.prof_details.fullname)
|
||||
prof_id = prof.id
|
||||
ic(prof_id, apparat.profname)
|
||||
|
||||
app_id = self.getApparatId(apparat.appname)
|
||||
logger.debug(apparat)
|
||||
app = apparat.apparat
|
||||
prof = apparat.prof
|
||||
present_prof = self.getProfByName(prof.name())
|
||||
prof_id = present_prof.id
|
||||
logger.debug(present_prof)
|
||||
|
||||
app_id = self.getApparatId(app.name)
|
||||
if app_id:
|
||||
return AppPresentError(app_id)
|
||||
if not prof_id:
|
||||
print("prof id not present, creating prof with data", apparat.prof_details)
|
||||
prof_id = self.createProf(apparat.prof_details)
|
||||
# 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]}')"
|
||||
log.info(query)
|
||||
logger.debug("prof id not present, creating prof with data", prof)
|
||||
prof_id = self.createProf(prof)
|
||||
logger.debug(prof_id)
|
||||
query = f"INSERT OR IGNORE INTO semesterapparat (appnr, name, erstellsemester, dauer, prof_id, fach,deletion_status,konto) VALUES ('{app.appnr}', '{app.name}', '{app.created_semester}', '{app.eternal}', {prof_id}, '{app.subject}', '{0}', '{SEMAP_MEDIA_ACCOUNTS[app.appnr]}')"
|
||||
logger.debug(query)
|
||||
self.query_db(query)
|
||||
return None
|
||||
def getApparatsByProf(self, prof_id: Union[str, int]) -> list[tuple]:
|
||||
@@ -934,7 +934,7 @@ class Database:
|
||||
"""
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
semesters = self.getSemersters()
|
||||
semesters = self.getSemesters()
|
||||
created = []
|
||||
deleted = []
|
||||
for semester in semesters:
|
||||
@@ -946,7 +946,6 @@ class Database:
|
||||
deleted.append(result[0])
|
||||
# store data in a tuple
|
||||
ret = []
|
||||
e_tuple = ()
|
||||
for sem in semesters:
|
||||
e_tuple = (
|
||||
sem,
|
||||
@@ -957,17 +956,16 @@ class Database:
|
||||
self.close_connection(conn)
|
||||
return ret
|
||||
|
||||
def deleteApparat(self, app_id: Union[str, int]):
|
||||
def deleteApparat(self, app_id: Union[str, int], semester):
|
||||
"""Delete an apparat from the database
|
||||
|
||||
Args:
|
||||
app_id (Union[str, int]): the id of the apparat
|
||||
semester (str): the semester the apparat should be deleted from
|
||||
"""
|
||||
today = datetime.datetime.now().strftime("%Y-%m-%d")
|
||||
self.query_db(
|
||||
"UPDATE semesterapparat SET deletion_status=1, deleted_date=? WHERE appnr=?",
|
||||
(today, app_id),
|
||||
(semester, app_id),
|
||||
)
|
||||
|
||||
def isEternal(self, id):
|
||||
@@ -1007,15 +1005,15 @@ class Database:
|
||||
"""
|
||||
query = "UPDATE semesterapparat SET name = ?, fach = ?, dauer = ?, prof_id = ?, prof_id_adis = ?, apparat_id_adis = ? WHERE appnr = ?"
|
||||
params = (
|
||||
apparat_data.appname,
|
||||
apparat_data.app_fach,
|
||||
apparat_data.dauerapp,
|
||||
self.getProfData(apparat_data.prof_details.fullname).id,
|
||||
apparat_data.prof_adis_id,
|
||||
apparat_data.apparat_adis_id,
|
||||
apparat_data.appnr,
|
||||
apparat_data.apparat.name,
|
||||
apparat_data.apparat.subject,
|
||||
apparat_data.apparat.eternal,
|
||||
self.getProfData(apparat_data.prof.fullname).id,
|
||||
apparat_data.apparat.prof_id_adis,
|
||||
apparat_data.apparat.apparat_id_adis,
|
||||
apparat_data.apparat.appnr,
|
||||
)
|
||||
log.info(f"Updating apparat with query {query} and params {params}")
|
||||
logger.debug(f"Updating apparat with query {query} and params {params}")
|
||||
self.query_db(query, params)
|
||||
|
||||
def checkApparatExists(self, app_name: str):
|
||||
@@ -1067,7 +1065,8 @@ class Database:
|
||||
Returns:
|
||||
list: the result of the query
|
||||
"""
|
||||
ic(query)
|
||||
logger.debug(query)
|
||||
logger.debug(f"Query: {query}")
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
result = cursor.execute(query).fetchall()
|
||||
@@ -1080,15 +1079,15 @@ class Database:
|
||||
result_a = tuple(result_a)
|
||||
result[result.index(orig_value)] = result_a
|
||||
self.close_connection(conn)
|
||||
log.info(f"Query result: {result}")
|
||||
logger.debug(f"Query result: {result}")
|
||||
return result
|
||||
|
||||
if "deletable" in kwargs.keys():
|
||||
query = f"""SELECT * FROM semesterapparat
|
||||
WHERE deletion_status=0 AND dauer=0 AND
|
||||
(
|
||||
(erstellsemester!='{kwargs['deletesemester']}' AND verlängerung_bis IS NULL) OR
|
||||
(erstellsemester!='{kwargs['deletesemester']}' AND verlängerung_bis!='{kwargs['deletesemester']}' AND verlängerung_bis!='{generateSemesterByDate(True)}')
|
||||
(erstellsemester!='{kwargs["deletesemester"]}' AND verlängerung_bis IS NULL) OR
|
||||
(erstellsemester!='{kwargs["deletesemester"]}' AND verlängerung_bis!='{kwargs["deletesemester"]}' AND verlängerung_bis!='{Semester()}')
|
||||
)"""
|
||||
return __query(query)
|
||||
if "dauer" in kwargs.keys():
|
||||
@@ -1119,8 +1118,10 @@ class Database:
|
||||
f"(erstellsemester='{kwargs['endsemester']}' OR verlängerung_bis='{kwargs['endsemester']}') AND ",
|
||||
)
|
||||
# remove all x="" parts from the query where x is a key in kwargs
|
||||
query = query[:-5]
|
||||
logger.info(f"Query before: {query}")
|
||||
query = query.strip()
|
||||
query = query[:-4]
|
||||
logger.info(f"Query after: {query}")
|
||||
# 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]
|
||||
@@ -1471,22 +1472,19 @@ class Database:
|
||||
)
|
||||
###
|
||||
|
||||
|
||||
|
||||
def createProf(self, profdata:Prof):
|
||||
print("createProf")
|
||||
ic(profdata)
|
||||
def createProf(self, profdata: Prof):
|
||||
logger.debug(profdata)
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
fname = profdata.firstname#profdata["profname"].split(", ")[1].strip()
|
||||
lname = profdata.lastname#profdata["profname"].split(", ")[0].strip()
|
||||
fname = profdata.firstname
|
||||
lname = profdata.lastname
|
||||
fullname = f"{lname} {fname}"
|
||||
mail = profdata.mail#profdata["prof_mail"]
|
||||
telnr = profdata.telnr#profdata["prof_tel"]
|
||||
title = profdata.title #profdata["title"]
|
||||
mail = profdata.mail
|
||||
telnr = profdata.telnr
|
||||
title = profdata.title
|
||||
|
||||
query = f"INSERT INTO prof (fname, lname, fullname, mail, telnr,titel) VALUES ('{fname}','{lname}','{fullname}','{mail}','{telnr}','{title}')"
|
||||
log.info(query)
|
||||
logger.debug(query)
|
||||
cursor.execute(query)
|
||||
|
||||
conn.commit()
|
||||
@@ -1519,7 +1517,7 @@ class Database:
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
if isinstance(profdata, Prof):
|
||||
fullname = profdata.fullname
|
||||
fullname = profdata.name()
|
||||
else:
|
||||
name = profdata["profname"]
|
||||
if ","in name:
|
||||
@@ -1529,7 +1527,7 @@ class Database:
|
||||
else:
|
||||
fullname = profdata["profname"]
|
||||
query = f"SELECT id FROM prof WHERE fullname = '{fullname}'"
|
||||
log.info(query)
|
||||
logger.debug(query)
|
||||
|
||||
cursor.execute(query)
|
||||
result = cursor.fetchone()
|
||||
@@ -1546,7 +1544,7 @@ class Database:
|
||||
conn = self.connect()
|
||||
cursor = conn.cursor()
|
||||
query = f"SELECT * FROM prof WHERE fullname = '{fullname}'"
|
||||
log.info(query)
|
||||
logger.debug(query)
|
||||
|
||||
result = cursor.execute(query).fetchone()
|
||||
if result:
|
||||
|
||||
Reference in New Issue
Block a user