forum : photos épinglées sur la fiche d'une station et route JSON publique

GET /forum/api/station/<id>.json (lecture seule, sans cookie, cache 60 s
vidé à chaque changement, max-age 300) : url du sujet et 3 photos épinglées
au plus. Bouton « Épingler sur la fiche » sous les photos du sujet de la
station pour le Propriétaire, l'adjoint et le propriétaire de cette station.
61 tests passent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
Théo GastineletClaude Opus 5.5 a révisé 2026-11-05 11:19:37 +01:00
1 Parent 6f15a7ed2d
révision b5fc68b8c2
6 fichiers modifiés
+243 -7

Aucun fichier ne correspond à votre recherche

+63
Voir le fichier
@@ -14,6 +14,7 @@ import roles
TITRE_MAX, CORPS_MAX = 150, 20000
EPINGLES_MAX = 5 # sujet fixe compris
FICHE_PHOTOS_MAX = 3 # photos épinglées sur la fiche publique d'une station
PHOTOS_MAX, OCTETS_MAX = 10, 25 * 1024 * 1024 # par message, sauf propriétaires (et par demande d'adhésion)
ADHESION_MAX = 4000 # longueur du message d'une demande d'adhésion
SESSION_JOURS, ETAT_MINUTES = 30, 10
@@ -34,6 +35,7 @@ class Forum:
self.dossier_photos = dossier_photos
self.proprietaire_id = proprietaire_id or None # id Gastinel de Théo (FORUM_PROPRIETAIRE_ID)
self.apres_roles = None # fonction(hub_id, roles) : envoie les rôles au Hub (branchée par server.py)
self.version = 0 # augmente à chaque changement visible sur les fiches de station (cache de server.py)
def _pousser_roles(self, hub_id):
if not self.apres_roles:
@@ -270,6 +272,7 @@ class Forum:
raise Invalide("rien à valider")
self.con.execute("UPDATE topics SET status=? WHERE id=?", ("publie" if accepte else "refuse", topic_id))
self.con.commit()
self.version += 1
def repondre(self, user, topic_id, corps, ip=None):
self._exiger(user, "repondre")
@@ -452,6 +455,7 @@ class Forum:
self._post(post_id)
self.con.execute("UPDATE posts SET hidden=? WHERE id=?", (int(masque), post_id))
self.con.commit()
self.version += 1
def masquer_sujet(self, user, topic_id, masque=True):
self._exiger(user, "masquer")
@@ -460,6 +464,7 @@ class Forum:
raise Refus("le Bulletin de la semaine ne se masque pas")
self.con.execute("UPDATE topics SET hidden=? WHERE id=?", (int(masque), topic_id))
self.con.commit()
self.version += 1
def verrouiller(self, user, topic_id, verrou=True):
self._exiger(user, "verrouiller")
@@ -467,6 +472,64 @@ class Forum:
self.con.execute("UPDATE topics SET locked=? WHERE id=?", (int(verrou), topic_id))
self.con.commit()
# --- fiche publique d'une station (page station de vercors.me) ------
def _photo_de_station(self, attachment_id):
r = self.con.execute(
"SELECT a.id, a.post_id, p.topic_id, p.hidden AS message_masque, t.station, t.status, t.hidden AS sujet_masque"
" FROM attachments a JOIN posts p ON p.id=a.post_id JOIN topics t ON t.id=p.topic_id WHERE a.id=?",
(attachment_id,)).fetchone()
if r is None:
raise Invalide("photo introuvable")
if not r["station"]:
raise Invalide("cette photo n'est pas dans le sujet d'une station")
return r
def peut_epingler_photo(self, user, station):
"""Propriétaire et adjoint : toutes les stations ; propriétaire de station : la sienne seulement."""
rs = self.roles_de(user)
if not station or not roles.peut(rs, "epingler_photo_fiche"):
return False
return bool(rs & roles.PROPRIOS) or station in self.stations_de(user)
def epingler_photo(self, user, attachment_id, epingle=True):
"""Épingle (ou retire) une photo du sujet de la station sur sa fiche publique, FICHE_PHOTOS_MAX au plus."""
ph = self._photo_de_station(attachment_id)
if not self.peut_epingler_photo(user, ph["station"]):
raise Refus("épingler une photo de cette station")
if epingle:
if ph["status"] != "publie" or ph["sujet_masque"] or ph["message_masque"]:
raise Invalide("seule une photo publiée peut aller sur la fiche")
if self.con.execute("SELECT 1 FROM fiche_photos WHERE attachment_id=?", (attachment_id,)).fetchone():
return ph["topic_id"], ph["post_id"]
n = self.con.execute("SELECT COUNT(*) FROM fiche_photos WHERE station=?", (ph["station"],)).fetchone()[0]
if n >= FICHE_PHOTOS_MAX:
raise Refus("%d photos au plus sur la fiche ; retires-en une d'abord" % FICHE_PHOTOS_MAX)
self.con.execute("INSERT INTO fiche_photos(attachment_id, station, pinned_by) VALUES (?, ?, ?)",
(attachment_id, ph["station"], user["id"]))
else:
self.con.execute("DELETE FROM fiche_photos WHERE attachment_id=?", (attachment_id,))
self.con.commit()
self.version += 1
return ph["topic_id"], ph["post_id"]
def photos_fiche_ids(self, station):
return {r[0] for r in self.con.execute("SELECT attachment_id FROM fiche_photos WHERE station=?", (station,))}
def fiche_station(self, station):
"""Lecture publique : sujet publié de la station et ses photos épinglées encore visibles."""
if station not in self.stations:
raise Invalide("station inconnue")
t = self.con.execute("SELECT id FROM topics WHERE station=? AND status='publie' AND hidden=0",
(station,)).fetchone()
photos_ = []
if t is not None:
photos_ = [{"fichier": os.path.basename(r["stored_path"]), "auteur": r["auteur"]} for r in self.con.execute(
"SELECT a.stored_path, u.display_name AS auteur FROM fiche_photos f"
" JOIN attachments a ON a.id=f.attachment_id JOIN posts p ON p.id=a.post_id JOIN users u ON u.id=p.author_id"
" WHERE f.station=? AND p.topic_id=? AND p.hidden=0 ORDER BY f.pinned_at, f.attachment_id LIMIT ?",
(station, t["id"], FICHE_PHOTOS_MAX))]
return {"station": station, "sujet": t["id"] if t is not None else None, "photos": photos_}
# --- RGPD ----------------------------------------------------------
def purger_ips(self, mois=12):
"""Efface les adresses IP de plus de `mois` mois (obligation légale : un an). Retourne le nombre de lignes."""