From ac895c9be8b7399d248b30f7c12352cb6cb1b557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Mogu=C3=A9rou?= Date: Mon, 15 May 2023 04:28:05 +0200 Subject: [PATCH] =?UTF-8?q?Restructuration=20du=20projet=20et=20r=C3=A9cup?= =?UTF-8?q?=C3=A9ration=20des=20modifications=20les=20plus=20r=C3=A9centes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/__init__.py | 30 ++++++++++++++ app/database.py | 63 +++++++++++++++++++++++++++++ app/templates/layout/base.html | 3 +- app/templates/login.html | 17 ++++++++ app/views.py | 74 +++++++++++++++++++++------------- passenger_wsgi.py | 2 +- 6 files changed, 159 insertions(+), 30 deletions(-) create mode 100644 app/database.py diff --git a/app/__init__.py b/app/__init__.py index e69de29..bcac754 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -0,0 +1,30 @@ +import os +import sys +from datetime import datetime + +from flask import Flask + +from .views import views +from .database import Database + +def create_app(): + sys.path.insert(0, os.path.dirname(__file__)) + + app = Flask(__name__) + app.config.from_object("config") + + app.jinja_env.globals.update({ + "year": datetime.now().year, + "menuitems": [ + ('/', '', 'accueil'), + ('/grades/', 'Mes notes', ''), + ('/timetable/', 'Emploi du temps', '') + ] + }) + + db = Database(app) + views(app, db) + + return app + +app = create_app() \ No newline at end of file diff --git a/app/database.py b/app/database.py new file mode 100644 index 0000000..307a363 --- /dev/null +++ b/app/database.py @@ -0,0 +1,63 @@ +from uuid import uuid4 + +import mysql.connector + +class Database: + def __init__(self, app): + self.db = mysql.connector.connect( + host=app.config["DB_HOSTNAME"], + user=app.config["DB_USERNAME"], + password=app.config["DB_PASSWORD"], + database=app.config["DB_DATABASE"] + ) + + def delete_old_sessions(self): + cursor = self.db.cursor() + cursor.execute("DELETE FROM sessions WHERE expiry_date <= CURRENT_TIMESTAMP") + self.db.commit() + + 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)) + + if result := cursor.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.db.commit() + + return uuid + + def destroy_session(self, uuid): + cursor = self.db.cursor() + cursor.execute("DELETE FROM sessions WHERE session_id=%s", (uuid,)) + self.db.commit() + + def check_connection(self, uuid): + cursor = self.db.cursor() + cursor.execute("SELECT session_id FROM sessions WHERE session_id=%s", (uuid,)) + + return cursor.fetchall() + +class User: + def __init__(self, user_id, username, nom, prenom, creation_date): + self.id = user_id + self.username = username + self.nom = nom + self.prenom = prenom + self.creation_date = creation_date + +class Session: + def __init__(self, db, uuid): + self.db = db + self.uuid = uuid + 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 " + "JOIN sessions ON utilisateurs.user_id=sessions.user_id WHERE session_id=%s", (self.uuid,)) + self.user = User(*cursor.fetchall()[0]) \ No newline at end of file diff --git a/app/templates/layout/base.html b/app/templates/layout/base.html index 8769a2c..5da9d87 100644 --- a/app/templates/layout/base.html +++ b/app/templates/layout/base.html @@ -19,8 +19,9 @@
+ Se déconnecter
Lycée polyvalent Alain-René Lesage
-
Espace Élèves - NOM Prénom (Classe)
+
Espace Élèves - {{ s.user.nom }} {{ s.user.prenom }}