From 6dc0f6c296728ed1689341ea318ac432d91400dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20Gastinel?= <264859180+gastinelt@users.noreply.github.com> Date: Thu, 29 Oct 2026 15:44:47 +0100 Subject: [PATCH] feat(admin): suivi des candidatures recues, suppression de code et compteur d utilisations --- assets/css/espace.css | 99 +++++++++++++++++++++ beta/server.py | 42 +++++++++ beta/tests/test_http.py | 22 +++++ index.html | 187 ++++++++++++++++++++++++++++++++-------- 4 files changed, 313 insertions(+), 37 deletions(-) diff --git a/assets/css/espace.css b/assets/css/espace.css index 4e9de2a..9c7c4f8 100644 --- a/assets/css/espace.css +++ b/assets/css/espace.css @@ -382,3 +382,102 @@ footer{max-width:1080px;margin:0 auto;padding:0 16px 32px;color:var(--doux);font padding: 12px 0; margin: 0; } + + +/* Badges d'utilisations & actions */ +.badge-usages { + display: inline-block; + font-size: 12px; + font-weight: 700; + padding: 3px 8px; + border-radius: 6px; + background: var(--filet); + color: var(--texte); +} +.badge-usages.actif { + background: #D8E0DA; + color: #2F4D35; +} +.date-derniere { + display: block; + font-size: 11px; + color: var(--doux); + margin-top: 3px; +} +.btn-action-suppr { + font-family: inherit; + font-size: 12px; + font-weight: 600; + background: transparent; + color: #A0554B; + border: 1px solid #F5D6D1; + padding: 4px 10px; + border-radius: 6px; + cursor: pointer; + transition: all 0.2s ease; +} +.btn-action-suppr:hover { + background: #A0554B; + color: #FFFFFF; + border-color: #A0554B; +} + +/* Bloc Candidatures */ +.candidatures-bloc { + margin-top: 32px; + border-top: 1.5px solid var(--filet); + padding-top: 24px; +} +.btn-remplir-code { + font-family: inherit; + font-size: 12px; + font-weight: 700; + background: var(--sauge); + color: #FFFFFF; + border: none; + border-radius: 999px; + padding: 5px 12px; + cursor: pointer; + display: inline-flex; + align-items: center; + gap: 4px; + transition: all 0.2s ease; + white-space: nowrap; +} +.btn-remplir-code:hover { + background: #6E8173; +} +.contact-ligne { + font-size: 12.5px; + display: flex; + flex-direction: column; + gap: 2px; +} +.contact-ligne a { + color: var(--texte); + text-decoration: underline; + text-underline-offset: 2px; +} +.contact-ligne a:hover { + color: var(--sauge); +} +.badge-age { + display: inline-block; + font-size: 11px; + font-weight: 700; + padding: 2px 7px; + border-radius: 4px; + background: #EAE8E2; + color: var(--texte); + margin-left: 6px; +} +.badge-age.mineur { + background: #F8E3D8; + color: #8C4824; +} +.parent-info { + display: block; + font-size: 11px; + color: #8C4824; + margin-top: 2px; +} diff --git a/beta/server.py b/beta/server.py index 16b62bc..eeda0f1 100644 --- a/beta/server.py +++ b/beta/server.py @@ -71,6 +71,7 @@ LIMITES_POST = { "/__beta/api/tester": 8000, "/__beta/api/feedback": 2_600_000, "/_admin/api/codes/creer": 4000, + "/_admin/api/codes/supprimer": 1000, } @@ -276,6 +277,8 @@ class Handler(BaseHTTPRequestHandler): return self.fichier(RACINE / "assets" / "img", p[len("/assets/img/"):]) if p == "/_admin/api/codes/liste": return self.admin_liste_codes() + if p == "/_admin/api/candidatures/liste": + return self.admin_liste_candidatures() s = self.session() if not s: if p.endswith(".html") or p == "/": @@ -305,6 +308,8 @@ class Handler(BaseHTTPRequestHandler): return self.formulaire_testeur() if p == "/_admin/api/codes/creer": return self.admin_creer_code() + if p == "/_admin/api/codes/supprimer": + return self.admin_supprimer_code() s = self.session() if not s: return self.json(401, {"erreur": "session expirée, ressaisir le code"}) @@ -534,6 +539,43 @@ class Handler(BaseHTTPRequestHandler): "created_at": time.time() }) + def admin_supprimer_code(self): + if self.headers.get("X-Admin") != "1": + return self.json(403, {"erreur": "accès réservé à l'administration"}) + try: + d = json.loads(self._corps.decode("utf-8")) + code_id = int(d.get("id")) + except Exception: + return self.json(400, {"erreur": "ID de code invalide"}) + + self.c.execute("DELETE FROM sessions WHERE code_id=?", (code_id,)) + self.c.execute("DELETE FROM codes WHERE id=?", (code_id,)) + self.c.execute("UPDATE audit SET code_id=NULL WHERE code_id=?", (code_id,)) + self.c.commit() + db.journal(self.c, "code_supprime", None, None, f"id={code_id}") + return self.json(200, {"ok": True, "id": code_id}) + + def admin_liste_candidatures(self): + if self.headers.get("X-Admin") != "1": + return self.json(403, {"erreur": "accès réservé à l'administration"}) + + lignes = [] + for r in self.c.execute("SELECT id, ts, nom, prenom, email, tel, naissance, mineur, parent_nom, parent_email, suivi_reseaux FROM testers ORDER BY id DESC LIMIT 100"): + lignes.append({ + "id": r["id"], + "ts": r["ts"], + "nom": r["nom"], + "prenom": r["prenom"], + "email": r["email"], + "tel": r["tel"], + "naissance": r["naissance"], + "mineur": bool(r["mineur"]), + "parent_nom": r["parent_nom"], + "parent_email": r["parent_email"], + "suivi_reseaux": bool(r["suivi_reseaux"]), + }) + return self.json(200, {"ok": True, "candidatures": lignes}) + def admin_liste_codes(self): if self.headers.get("X-Admin") != "1": return self.json(403, {"erreur": "accès réservé à l'administration"}) diff --git a/beta/tests/test_http.py b/beta/tests/test_http.py index ce3b74e..d6e35aa 100644 --- a/beta/tests/test_http.py +++ b/beta/tests/test_http.py @@ -236,6 +236,28 @@ class T(unittest.TestCase): self.assertTrue(res.get("ok")) labels = [c["label"] for c in res.get("codes", [])] self.assertIn("Testeur Admin", labels) + code_id = res["codes"][0]["id"] + + # 4. Supprimer le code + s, _, d = self.req("POST", "/_admin/api/codes/supprimer", {"id": code_id}, headers={"X-Admin": "1"}) + self.assertEqual(s, 200) + res = json.loads(d.decode()) + self.assertTrue(res.get("ok")) + self.assertEqual(res.get("id"), code_id) + + # Vérification qu'il n'est plus dans la liste + s, _, d = self.req("GET", "/_admin/api/codes/liste", headers={"X-Admin": "1"}) + self.assertEqual(s, 200) + res = json.loads(d.decode()) + labels_after = [c["label"] for c in res.get("codes", [])] + self.assertNotIn("Testeur Admin", labels_after) + + # 5. Liste des candidatures + s, _, d = self.req("GET", "/_admin/api/candidatures/liste", headers={"X-Admin": "1"}) + self.assertEqual(s, 200) + res = json.loads(d.decode()) + self.assertTrue(res.get("ok")) + self.assertIsInstance(res.get("candidatures"), list) if __name__ == "__main__": diff --git a/index.html b/index.html index 38ddd2d..184d709 100644 --- a/index.html +++ b/index.html @@ -113,13 +113,24 @@
-

Codes testeurs récents

+

Codes testeurs émis 0

Chargement des codes en cours...

+ + +
+
+

📬 Candidatures reçues via le formulaire 0

+ +
+
+

Chargement des candidatures...

+
+
@@ -178,8 +189,13 @@ const btnCopierMsg = document.getElementById('btn-copier-msg'); const whatsappTexte = document.getElementById('texte-msg-whatsapp'); - const conteneurTableau = document.getElementById('conteneur-tableau-codes'); - const btnRafraichir = document.getElementById('btn-rafraichir-codes'); + const conteneurTableauCodes = document.getElementById('conteneur-tableau-codes'); + const btnRafraichirCodes = document.getElementById('btn-rafraichir-codes'); + const compteCodesEl = document.getElementById('compte-codes'); + + const conteneurTableauCandidatures = document.getElementById('conteneur-tableau-candidatures'); + const btnRafraichirCandidatures = document.getElementById('btn-rafraichir-candidatures'); + const compteCandidaturesEl = document.getElementById('compte-candidatures'); let dernierCode = ''; @@ -189,38 +205,11 @@ return d.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: 'numeric' }); } - async function chargerCodes() { - try { - const res = await fetch('/_admin/api/codes/liste'); - if (!res.ok) { - conteneurTableau.innerHTML = '

Impossible de charger les codes (' + res.status + ').

'; - return; - } - const data = await res.json(); - afficherTableau(data.codes || []); - } catch (e) { - conteneurTableau.innerHTML = '

Impossible de contacter le serveur.

'; - } - } - - function afficherTableau(codes) { - if (!codes.length) { - conteneurTableau.innerHTML = '

Aucun code émis pour le moment.

'; - return; - } - let html = ''; - for (const c of codes) { - html += '' + - '' + - '' + - '' + - '' + - '' + - '' + - ''; - } - html += '
IDTesteurCréé leExpireUsagesStatut
#' + c.id + '' + escapeHtml(c.label) + '' + fmtDate(c.created_at) + '' + (c.expires_at ? fmtDate(c.expires_at) : 'Illimité') + '' + (c.uses || 0) + '' + escapeHtml(c.status) + '
'; - conteneurTableau.innerHTML = html; + function fmtDateHeure(ts) { + if (!ts) return '-'; + const d = new Date(ts * 1000); + return d.toLocaleDateString('fr-FR', { day: '2-digit', month: '2-digit', year: 'numeric' }) + + ' à ' + d.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' }); } function escapeHtml(str) { @@ -229,6 +218,127 @@ return div.innerHTML; } + function escapeJs(str) { + return (str || '').replace(/\\/g, '\\\\').replace(/'/g, "\\'"); + } + + // --- Gestion des codes --- + async function chargerCodes() { + try { + const res = await fetch('/_admin/api/codes/liste'); + if (!res.ok) { + conteneurTableauCodes.innerHTML = '

Impossible de charger les codes (' + res.status + ').

'; + return; + } + const data = await res.json(); + afficherTableauCodes(data.codes || []); + } catch (e) { + conteneurTableauCodes.innerHTML = '

Impossible de contacter le serveur.

'; + } + } + + function afficherTableauCodes(codes) { + if (compteCodesEl) compteCodesEl.textContent = codes.length; + if (!codes.length) { + conteneurTableauCodes.innerHTML = '

Aucun code émis pour le moment.

'; + return; + } + let html = ''; + for (const c of codes) { + const usesHtml = c.uses > 0 + ? '' + c.uses + ' connexion' + (c.uses > 1 ? 's' : '') + '' + + (c.last_used_at ? 'Dernière : ' + fmtDateHeure(c.last_used_at) + '' : '') + : '0 connexion'; + + html += '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + + ''; + } + html += '
IDTesteurCréé leExpireUtilisationsStatutAction
#' + c.id + '' + escapeHtml(c.label) + '' + fmtDate(c.created_at) + '' + (c.expires_at ? fmtDate(c.expires_at) : 'Illimité') + '' + usesHtml + '' + escapeHtml(c.status) + '
'; + conteneurTableauCodes.innerHTML = html; + } + + // --- Gestion des candidatures --- + async function chargerCandidatures() { + try { + const res = await fetch('/_admin/api/candidatures/liste'); + if (!res.ok) { + conteneurTableauCandidatures.innerHTML = '

Impossible de charger les candidatures (' + res.status + ').

'; + return; + } + const data = await res.json(); + afficherTableauCandidatures(data.candidatures || []); + } catch (e) { + conteneurTableauCandidatures.innerHTML = '

Impossible de contacter le serveur.

'; + } + } + + function afficherTableauCandidatures(liste) { + if (compteCandidaturesEl) compteCandidaturesEl.textContent = liste.length; + if (!liste.length) { + conteneurTableauCandidatures.innerHTML = '

Aucune candidature reçue via le formulaire pour le moment.

'; + return; + } + let html = ''; + for (const item of liste) { + const ageBadge = item.mineur + ? '15-17 ans (mineur)' + : 'Majeur'; + const parentHtml = (item.mineur && item.parent_nom) + ? 'Resp. légal : ' + escapeHtml(item.parent_nom) + ' (' + escapeHtml(item.parent_email || '') + ')' + : ''; + + html += '' + + '' + + '' + + '' + + '' + + '' + + ''; + } + html += '
DateCandidatContactRéseauxAction
' + fmtDateHeure(item.ts) + '' + escapeHtml(item.prenom) + ' ' + escapeHtml(item.nom) + ' ' + ageBadge + parentHtml + '' + (item.suivi_reseaux ? '✓ Insta' : '-') + '
'; + conteneurTableauCandidatures.innerHTML = html; + } + + // --- Actions globales exposées --- + window.jobrunnerBeta = { + supprimerCode: async function(id, label) { + if (!confirm('Supprimer définitivement le code de "' + label + '" ?\nSes sessions éventuelles seront immédiatement coupées.')) { + return; + } + try { + const res = await fetch('/_admin/api/codes/supprimer', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ id: id }) + }); + const data = await res.json().catch(() => ({})); + if (res.ok && data.ok) { + chargerCodes(); + } else { + alert(data.erreur || 'Erreur lors de la suppression.'); + } + } catch (e) { + alert('Erreur réseau lors de la suppression.'); + } + }, + preparerCodePour: function(nomComplet) { + inputLabel.value = nomComplet; + inputLabel.focus(); + inputLabel.scrollIntoView({ behavior: 'smooth', block: 'center' }); + } + }; + + // --- Formulaire de création --- form.addEventListener('submit', async function(e) { e.preventDefault(); errBox.style.display = 'none'; @@ -295,9 +405,12 @@ }); }); - btnRafraichir.addEventListener('click', chargerCodes); + btnRafraichirCodes.addEventListener('click', chargerCodes); + btnRafraichirCandidatures.addEventListener('click', chargerCandidatures); + chargerCodes(); + chargerCandidatures(); })(); - + \ No newline at end of file