diff --git a/app/database.py b/app/database.py index a706d41..8ca48b9 100644 --- a/app/database.py +++ b/app/database.py @@ -10,37 +10,33 @@ class Database: password=app.config["DB_PASSWORD"], database=app.config["DB_DATABASE"] ) + self.cur = self.db.cursor() def create_session(self, username, password): - cursor = self.db.cursor() - cursor.execute("SELECT user_id FROM utilisateurs WHERE username=%s AND password=%s", (username, password)) + self.cur.execute("SELECT user_id FROM utilisateurs WHERE username=%s AND password=%s", (username, password)) - if result := cursor.fetchall(): + if result := self.cur.fetchall(): user_id = result[0][0] else: return None uuid = str(uuid4()) - cursor.execute("INSERT INTO sessions (session_id, user_id) VALUES (%s, %s)", (uuid, user_id)) + self.cur.execute("INSERT INTO sessions (session_id, user_id) VALUES (%s, %s)", (uuid, user_id)) self.db.commit() - cursor.close() return uuid def destroy_session(self, uuid): - cursor = self.db.cursor() - cursor.execute("DELETE FROM sessions WHERE session_id=%s", (uuid,)) + self.cur.execute("DELETE FROM sessions WHERE session_id=%s", (uuid,)) self.db.commit() - cursor.close() def check_connection(self, uuid): - cursor = self.db.cursor() - cursor.execute("DELETE FROM sessions WHERE expiry_date <= CURRENT_TIMESTAMP") - cursor.execute("SELECT session_id FROM sessions WHERE session_id=%s", (uuid,)) - result = cursor.fetchall() - cursor.close() + self.cur.execute("DELETE FROM sessions WHERE expiry_date <= CURRENT_TIMESTAMP") + self.cur.execute("SELECT session_id FROM sessions WHERE session_id=%s", (uuid,)) + result = self.cur.fetchall() return result def __del__(self): + self.cur.close() self.db.close() class User: @@ -58,21 +54,17 @@ class Session: self.user = self.get_user() def get_user(self): - cursor = self.db.db.cursor() - cursor.execute("SELECT utilisateurs.user_id, username, nom, prenom, creation_date FROM utilisateurs " + self.db.cur.execute("SELECT utilisateurs.user_id, username, nom, prenom, creation_date FROM utilisateurs " "JOIN sessions ON utilisateurs.user_id=sessions.user_id WHERE session_id=%s", (self.uuid,)) - result = cursor.fetchall() - cursor.close() + result = self.db.cur.fetchall() return User(*result[0]) def fetch_grades(self): - cursor = self.db.db.cursor() - cursor.execute("SELECT matieres.libelle, devoirs.libelle, date, coefficient, valeur, maximum FROM notes " + self.db.cur.execute("SELECT matieres.libelle, devoirs.libelle, date, coefficient, valeur, maximum FROM notes " "JOIN devoirs ON notes.id_devoir=devoirs.id_devoir " "JOIN matieres ON devoirs.id_matiere=matieres.id_matiere WHERE id_eleve=%s", (self.user.id,)) - result = cursor.fetchall() - cursor.close() + result = self.db.cur.fetchall() dico = {} for note in result: if note[0] in dico: