raz du 19/8
This commit is contained in:
1
.idea/Ratio_Inventaires.iml
generated
1
.idea/Ratio_Inventaires.iml
generated
@@ -2,6 +2,7 @@
|
|||||||
<module type="PYTHON_MODULE" version="4">
|
<module type="PYTHON_MODULE" version="4">
|
||||||
<component name="NewModuleRootManager">
|
<component name="NewModuleRootManager">
|
||||||
<content url="file://$MODULE_DIR$">
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.idea/dataSources" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="jdk" jdkName="Python 3.11 virtualenv at C:\Users\miche\PycharmProjects\Gestion_sondes\.venv" jdkType="Python SDK" />
|
<orderEntry type="jdk" jdkName="Python 3.11 virtualenv at C:\Users\miche\PycharmProjects\Gestion_sondes\.venv" jdkType="Python SDK" />
|
||||||
|
|||||||
81
connexion.py
81
connexion.py
@@ -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()
|
|
||||||
|
|
||||||
@@ -1,3 +1,3 @@
|
|||||||
Meudon:3
|
Meudon:27
|
||||||
Roissy:8
|
Roissy:15
|
||||||
Saclay:12
|
Saclay:20
|
||||||
|
|||||||
@@ -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()
|
|
||||||
|
|
||||||
13
mkhash.py
13
mkhash.py
@@ -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()
|
|
||||||
115
sms_alert.py
115
sms_alert.py
@@ -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()
|
|
||||||
@@ -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}")
|
|
||||||
Reference in New Issue
Block a user