Unification du curseur

This commit is contained in:
Valentin Moguérou 2023-05-15 17:33:49 +02:00
parent 792b3fca33
commit 7619f24fce

View File

@ -10,37 +10,33 @@ class Database:
password=app.config["DB_PASSWORD"], password=app.config["DB_PASSWORD"],
database=app.config["DB_DATABASE"] database=app.config["DB_DATABASE"]
) )
self.cur = self.db.cursor()
def create_session(self, username, password): def create_session(self, username, password):
cursor = self.db.cursor() self.cur.execute("SELECT user_id FROM utilisateurs WHERE username=%s AND password=%s", (username, password))
cursor.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] user_id = result[0][0]
else: else:
return None return None
uuid = str(uuid4()) 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() self.db.commit()
cursor.close()
return uuid return uuid
def destroy_session(self, uuid): def destroy_session(self, uuid):
cursor = self.db.cursor() self.cur.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() self.cur.execute("DELETE FROM sessions WHERE expiry_date <= CURRENT_TIMESTAMP")
cursor.execute("DELETE FROM sessions WHERE expiry_date <= CURRENT_TIMESTAMP") self.cur.execute("SELECT session_id FROM sessions WHERE session_id=%s", (uuid,))
cursor.execute("SELECT session_id FROM sessions WHERE session_id=%s", (uuid,)) result = self.cur.fetchall()
result = cursor.fetchall()
cursor.close()
return result return result
def __del__(self): def __del__(self):
self.cur.close()
self.db.close() self.db.close()
class User: class User:
@ -58,21 +54,17 @@ class Session:
self.user = self.get_user() self.user = self.get_user()
def get_user(self): def get_user(self):
cursor = self.db.db.cursor() self.db.cur.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 = self.db.cur.fetchall()
cursor.close()
return User(*result[0]) return User(*result[0])
def fetch_grades(self): def fetch_grades(self):
cursor = self.db.db.cursor() self.db.cur.execute("SELECT matieres.libelle, devoirs.libelle, date, coefficient, valeur, maximum FROM notes "
cursor.execute("SELECT matieres.libelle, devoirs.libelle, date, coefficient, valeur, maximum FROM notes "
"JOIN devoirs ON notes.id_devoir=devoirs.id_devoir " "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,)) "JOIN matieres ON devoirs.id_matiere=matieres.id_matiere WHERE id_eleve=%s", (self.user.id,))
result = cursor.fetchall() result = self.db.cur.fetchall()
cursor.close()
dico = {} dico = {}
for note in result: for note in result:
if note[0] in dico: if note[0] in dico: