diff --git a/.idea/Ratio_Inventaires.iml b/.idea/Ratio_Inventaires.iml index 5c9e85e..4520d40 100644 --- a/.idea/Ratio_Inventaires.iml +++ b/.idea/Ratio_Inventaires.iml @@ -2,6 +2,7 @@ + diff --git a/creer_user.py b/app/creer_user.py similarity index 100% rename from creer_user.py rename to app/creer_user.py diff --git a/connexion.py b/connexion.py deleted file mode 100644 index d03e796..0000000 --- a/connexion.py +++ /dev/null @@ -1,81 +0,0 @@ -import tkinter as tk -from tkinter import messagebox -import mysql.connector -import os -import subprocess -from dotenv import load_dotenv - -# Charger les variables d'environnement -load_dotenv() - -DB_HOST = os.getenv("DB_HOST") -DB_USER = os.getenv("DB_USER") -DB_PASSWORD = os.getenv("DB_PASSWORD") -DB_NAME = os.getenv("DB_NAME") - - -def ouvrir_excel(site, login): - dossier_excel = os.path.join(os.path.dirname(__file__), "excel") - chemin_excel = os.path.join(dossier_excel, "Ratio.xlsm") - subprocess.Popen(["start", chemin_excel], shell=True) - - # Écrit le nom du site - with open(os.path.join(dossier_excel, "site_selectionne.txt"), "w") as f: - f.write(site) - - # Écrit le nom de l'utilisateur connecté - with open(os.path.join(dossier_excel, "login_utilisateur.txt"), "w") as f: - f.write(login) - os.chdir(dossier_excel) # On se place dans le dossier pour lancer Excel - subprocess.Popen(["start", "Ratio.xlsm"], shell=True) - -from datetime import date - -def verifier_connexion(): - login = entry_login.get() - mdp = entry_mdp.get() - - try: - conn = mysql.connector.connect( - host=DB_HOST, - user=DB_USER, - password=DB_PASSWORD, - database=DB_NAME - ) - cursor = conn.cursor() - - cursor.execute("SELECT Site, DateExpiration FROM AccesUtilisateurs WHERE NomUtilisateur=%s AND MotDePasse=%s", (login, mdp)) - result = cursor.fetchone() - - if result: - site, expiration = result - if expiration and expiration < date.today(): - messagebox.showerror("Compte expiré", f"Votre compte a expiré le {expiration}.") - else: - root.destroy() - ouvrir_excel(site, login) - else: - messagebox.showerror("Erreur", "Identifiants invalides.") - - except Exception as e: - messagebox.showerror("Erreur de connexion", str(e)) - - - -# Interface utilisateur -root = tk.Tk() -root.title("Connexion à l'inventaire") - -tk.Label(root, text="Nom d'utilisateur").grid(row=0, column=0) -tk.Label(root, text="Mot de passe").grid(row=1, column=0) - -entry_login = tk.Entry(root) -entry_mdp = tk.Entry(root, show="*") - -entry_login.grid(row=0, column=1) -entry_mdp.grid(row=1, column=1) - -tk.Button(root, text="Connexion", command=verifier_connexion).grid(row=2, columnspan=2, pady=10) - -root.mainloop() - diff --git a/last_ids.txt b/last_ids.txt index a3bb71b..ead5af7 100644 --- a/last_ids.txt +++ b/last_ids.txt @@ -1,3 +1,3 @@ -Meudon:3 -Roissy:8 -Saclay:12 +Meudon:27 +Roissy:15 +Saclay:20 diff --git a/migre_bcrypt.py b/migre_bcrypt.py deleted file mode 100644 index 70e77ea..0000000 --- a/migre_bcrypt.py +++ /dev/null @@ -1,44 +0,0 @@ -import os, sys, bcrypt, mysql.connector -from dotenv import load_dotenv - -load_dotenv() - -conn = mysql.connector.connect( - host=os.getenv("DB_HOST"), - database=os.getenv("DB_NAME"), - user=os.getenv("DB_USER"), - password=os.getenv("DB_PASSWORD"), -) -try: - cur = conn.cursor(dictionary=True) - cur.execute(""" - SELECT NomUtilisateur, MotDePasse - FROM Utilisateurs - WHERE MotDePasseHash IS NULL OR MotDePasseHash = '' - """) - rows = cur.fetchall() - print(f"{len(rows)} utilisateur(s) à migrer…") - - for r in rows: - login = r["NomUtilisateur"] - plain = (r["MotDePasse"] or "").encode("utf-8") - if not plain: - print(f"- {login}: mot de passe vide — ignoré") - continue - hashed = bcrypt.hashpw(plain, bcrypt.gensalt(rounds=12)).decode("ascii") - cur.execute(""" - UPDATE Utilisateurs - SET MotDePasseHash = %s - WHERE NomUtilisateur = %s - """, (hashed, login)) - print(f"- {login}: OK") - conn.commit() - print("Migration terminée.") -except Exception as e: - conn.rollback() - print("Erreur:", e) - sys.exit(1) -finally: - cur.close() - conn.close() - diff --git a/mkhash.py b/mkhash.py deleted file mode 100644 index 717b671..0000000 --- a/mkhash.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python3 -import sys, getpass, bcrypt - -def main(): - if len(sys.argv) > 1: - password = sys.argv[1] - else: - password = getpass.getpass("Mot de passe: ") - h = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt(rounds=12)).decode("ascii") - print(h) - -if __name__ == "__main__": - main() diff --git a/sms_alert.py b/sms_alert.py deleted file mode 100644 index 97bce78..0000000 --- a/sms_alert.py +++ /dev/null @@ -1,115 +0,0 @@ -import os -import mysql.connector -import ovh -from dotenv import load_dotenv - -load_dotenv() - -# 📍 Liste des numéros de téléphone par site -PHONE_NUMBERS_BY_SITE = { - "Roissy": ["+336XXXXXXXX"], - "Meudon": ["+336XXXXXXXX"], - "Saclay": ["+336XXXXXXXX"] -} - -STATE_FILE = "last_ids.txt" - -# ✅ Lecture des derniers IDs connus -def load_last_ids(): - if not os.path.exists(STATE_FILE): - return {} - with open(STATE_FILE, "r") as f: - return dict(line.strip().split(":") for line in f) - -# ✅ Sauvegarde des derniers IDs -def save_last_ids(ids): - with open(STATE_FILE, "w") as f: - for site, id_val in ids.items(): - f.write(f"{site}:{id_val}\n") - -# ✅ Fonction SMS via OVH -def send_sms(message: str, site: str) -> None: - phone_numbers = PHONE_NUMBERS_BY_SITE.get(site) - if not phone_numbers or phone_numbers == ['']: - print(f"[!] Aucun numéro défini pour le site {site}. SMS non envoyé.") - return - - try: - client = ovh.Client( - endpoint=os.getenv("OVH_ENDPOINT"), - application_key=os.getenv("OVH_APP_KEY"), - application_secret=os.getenv("OVH_APP_SECRET"), - consumer_key=os.getenv("OVH_CONSUMER_KEY"), - ) - - sender = os.getenv("OVH_SMS_SENDER") - account = os.getenv("OVH_SMS_ACCOUNT") - - result = client.post(f"/sms/{account}/jobs", - sender=sender, - message=message, - receivers=phone_numbers, - priority='high', - noStopClause=True, - charset='UTF-8', - class_='phoneDisplay', - coding='7bit', - senderForResponse=False, - validityPeriod=2880) - - print(f"[✓] SMS envoyé à {phone_numbers} pour {site}") - except Exception as e: - print(f"[!] Erreur envoi SMS OVH : {e}") - -# ✅ Connexion à la base Commun pour lire les identifiants des sites -def get_db_connections(): - conn = mysql.connector.connect( - host=os.getenv("DB_HOST"), - user=os.getenv("DB_USER"), - password=os.getenv("DB_PASSWORD"), - database=os.getenv("DB_NAME") - ) - cursor = conn.cursor(dictionary=True) - cursor.execute("SELECT BDD, UID, PWD FROM Connexions") - connexions = cursor.fetchall() - cursor.close() - conn.close() - return connexions - -# ✅ Vérifie chaque site pour nouvelle ligne -def check_new_logs(): - last_ids = load_last_ids() - connexions = get_db_connections() - - for conn_info in connexions: - site = conn_info['BDD'] - try: - conn = mysql.connector.connect( - host=os.getenv("DB_HOST"), - user=conn_info['UID'], - password=conn_info['PWD'], - database=site - ) - cursor = conn.cursor() - cursor.execute("SELECT MAX(Id) FROM Log_Inventaires") - result = cursor.fetchone() - latest_id = result[0] or 0 - old_id = int(last_ids.get(site, 0)) - - print(f"{site} → dernier ID enregistré : {old_id}, ID actuel : {latest_id}") - - if latest_id > old_id: - message = f"🔔 Nouveau log dans {site} (Id: {latest_id})" - send_sms(message, site) - last_ids[site] = str(latest_id) - - cursor.close() - conn.close() - except Exception as e: - print(f"[{site}] Erreur : {e}") - - save_last_ids(last_ids) - -# ✅ Point d'entrée principal -if __name__ == "__main__": - check_new_logs() diff --git a/test_sms_ovh.py b/test_sms_ovh.py deleted file mode 100644 index 280e40b..0000000 --- a/test_sms_ovh.py +++ /dev/null @@ -1,33 +0,0 @@ -def send_sms(message: str, site: str) -> None: - phone_numbers = PHONE_NUMBERS_BY_SITE.get(site) - if not phone_numbers or phone_numbers == ['']: - print(f"[!] Aucun numéro défini pour le site {site}. SMS non envoyé.") - return - - try: - client = ovh.Client( - endpoint=os.getenv("OVH_ENDPOINT"), - application_key=os.getenv("OVH_APP_KEY"), - application_secret=os.getenv("OVH_APP_SECRET"), - consumer_key=os.getenv("OVH_CONSUMER_KEY"), - ) - - sender = os.getenv("OVH_SMS_SENDER") - account = os.getenv("OVH_SMS_ACCOUNT") - - result = client.post(f"/sms/{account}/jobs", - sender=sender, - message=message, - receivers=phone_numbers, - priority='high', - noStopClause=True, - charset='UTF-8', - class_='phoneDisplay', - coding='7bit', - senderForResponse=False, - validityPeriod=2880) - - print(f"[✓] SMS envoyé à {phone_numbers} pour {site}") - # Optionnel : journaliser_sms(message, site, phone_numbers) - except Exception as e: - print(f"[!] Erreur envoi SMS OVH : {e}")