Ajout d'instructions de fermeture des curseurs

This commit is contained in:
Valentin Moguérou 2023-05-15 05:37:58 +02:00
parent 38f24a3ec3
commit 9112028826

View File

@ -15,6 +15,7 @@ class Database:
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("DELETE FROM sessions WHERE expiry_date <= CURRENT_TIMESTAMP") cursor.execute("DELETE FROM sessions WHERE expiry_date <= CURRENT_TIMESTAMP")
self.db.commit() self.db.commit()
cursor.close()
def create_session(self, username, password): def create_session(self, username, password):
cursor = self.db.cursor() cursor = self.db.cursor()
@ -28,19 +29,21 @@ class Database:
uuid = str(uuid4()) uuid = str(uuid4())
cursor.execute("INSERT INTO sessions (session_id, user_id) VALUES (%s, %s)", (uuid, user_id)) cursor.execute("INSERT INTO sessions (session_id, user_id) VALUES (%s, %s)", (uuid, user_id))
self.db.commit() self.db.commit()
cursor.close()
return uuid return uuid
def destroy_session(self, uuid): def destroy_session(self, uuid):
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("DELETE FROM sessions WHERE session_id=%s", (uuid,)) cursor.execute("DELETE FROM sessions WHERE session_id=%s", (uuid,))
self.db.commit() self.db.commit()
cursor.close()
def check_connection(self, uuid): def check_connection(self, uuid):
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("SELECT session_id FROM sessions WHERE session_id=%s", (uuid,)) cursor.execute("SELECT session_id FROM sessions WHERE session_id=%s", (uuid,))
result = cursor.fetchall()
return cursor.fetchall() cursor.close()
return result
class User: class User:
def __init__(self, user_id, username, nom, prenom, creation_date): def __init__(self, user_id, username, nom, prenom, creation_date):
@ -61,6 +64,7 @@ class Session:
cursor.execute("SELECT utilisateurs.user_id, username, nom, prenom, creation_date FROM utilisateurs " cursor.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,)) "JOIN sessions ON utilisateurs.user_id=sessions.user_id WHERE session_id=%s", (self.uuid,))
result = cursor.fetchall() result = cursor.fetchall()
cursor.close()
return User(*result[0]) return User(*result[0])
def fetch_grades(self): def fetch_grades(self):
@ -70,6 +74,7 @@ class Session:
"JOIN matieres ON devoirs.id_matiere=matieres.id_matiere WHERE id_eleve=%s", (self.user.id,)) "JOIN matieres ON devoirs.id_matiere=matieres.id_matiere WHERE id_eleve=%s", (self.user.id,))
result = cursor.fetchall() result = cursor.fetchall()
cursor.close()
dico = {} dico = {}
for note in result: for note in result:
if note[0] in dico: if note[0] in dico: