111 lines
5.4 KiB
Python
111 lines
5.4 KiB
Python
# === Constantes et imports ===
|
||
import streamlit as st
|
||
import requests
|
||
import os
|
||
import pathlib
|
||
|
||
from .utils.tickets.display import afficher_tickets_par_fiche
|
||
from .utils.tickets.creation import formulaire_creation_ticket_dynamique
|
||
from .utils.tickets.core import rechercher_tickets_gitea
|
||
|
||
from config import GITEA_TOKEN, GITEA_URL, ORGANISATION, DEPOT_FICHES, ENV, FICHES_CRITICITE
|
||
|
||
from utils.gitea import charger_arborescence_fiches
|
||
|
||
from .utils.fiche_utils import load_seuils, doit_regenerer_fiche
|
||
|
||
from .generer import generer_fiche
|
||
|
||
def interface_fiches():
|
||
st.markdown("# Découverte des fiches")
|
||
with st.expander("Comment utiliser cet onglet ?", expanded=False):
|
||
st.markdown("""
|
||
1. Parcourez la liste des fiches disponibles par catégorie
|
||
2. Sélectionnez une fiche pour afficher son contenu complet
|
||
3. Consultez les données détaillées, graphiques et analyses supplémentaires
|
||
4. Utilisez ces informations pour approfondir votre compréhension des vulnérabilités identifiées
|
||
|
||
Les catégories sont les suivantes :
|
||
* Assemblage : opération d'assemblage des produits finaux à partir des composants
|
||
* Connexe : opérations diverses nécessaires pour fabriquer le numérique, mais n'entrant pas directement dans sa composition
|
||
* Criticités : indices utilisés pour identifier et évaluer les vulnérabilités
|
||
* Fabrication : opération de fabrication des composants à partir de minerais
|
||
* Minerai : description et opérations d'extraction et de traitement des minerais
|
||
""")
|
||
st.markdown("---")
|
||
|
||
if "fiches_arbo" not in st.session_state:
|
||
st.session_state["fiches_arbo"] = charger_arborescence_fiches()
|
||
|
||
arbo = st.session_state.get("fiches_arbo", {})
|
||
if not arbo:
|
||
st.warning("Aucune fiche disponible pour le moment.")
|
||
return
|
||
|
||
dossiers = sorted(arbo.keys(), key=lambda x: x.lower())
|
||
dossier_choisi = st.selectbox("Choisissez une catégorie de fiches", ["-- Sélectionner un dossier --"] + dossiers)
|
||
|
||
if dossier_choisi and dossier_choisi != "-- Sélectionner un dossier --":
|
||
fiches = arbo.get(dossier_choisi, [])
|
||
noms_fiches = [f['nom'] for f in fiches]
|
||
fiche_choisie = st.selectbox("Choisissez une fiche", ["-- Sélectionner une fiche --"] + noms_fiches)
|
||
|
||
if fiche_choisie and fiche_choisie != "-- Sélectionner une fiche --":
|
||
fiche_info = next((f for f in fiches if f["nom"] == fiche_choisie), None)
|
||
if fiche_info:
|
||
try:
|
||
headers = {"Authorization": f"token {GITEA_TOKEN}"}
|
||
reponse_fiche = requests.get(fiche_info["download_url"], headers=headers)
|
||
reponse_fiche.raise_for_status()
|
||
md_source = reponse_fiche.text
|
||
|
||
if "seuils" not in st.session_state:
|
||
SEUILS = load_seuils("assets/config.yaml")
|
||
st.session_state["seuils"] = SEUILS
|
||
else:
|
||
SEUILS = st.session_state["seuils"]
|
||
|
||
nom_fiche = os.path.splitext(fiche_choisie)[0]
|
||
html_path = os.path.join("HTML", dossier_choisi, nom_fiche + ".html")
|
||
path_relative = f"Documents/{dossier_choisi}/{fiche_choisie}"
|
||
commits_url = f"{GITEA_URL}/repos/{ORGANISATION}/{DEPOT_FICHES}/commits?path={path_relative}&sha={ENV}"
|
||
|
||
regenerate = doit_regenerer_fiche(
|
||
html_path=html_path,
|
||
fiche_type=fiche_info.get("type", ""),
|
||
fiche_choisie=fiche_choisie,
|
||
commit_url=commits_url,
|
||
fichiers_criticite=FICHES_CRITICITE
|
||
)
|
||
|
||
if regenerate:
|
||
html_path = generer_fiche(md_source, dossier_choisi, fiche_choisie, SEUILS)
|
||
|
||
with open(html_path, "r", encoding="utf-8") as f:
|
||
st.markdown(f.read(), unsafe_allow_html=True)
|
||
|
||
if st.session_state.get("logged_in", False):
|
||
pdf_name = nom_fiche + ".pdf"
|
||
pdf_path = os.path.join("static", "Fiches", dossier_choisi, pdf_name)
|
||
|
||
# Bouton de téléchargement du PDF
|
||
if os.path.exists(pdf_path):
|
||
with open(pdf_path, "rb") as pdf_file:
|
||
st.download_button(
|
||
label="Télécharger cette fiche en PDF",
|
||
data=pdf_file,
|
||
file_name=pdf_name,
|
||
mime="application/pdf",
|
||
help="Télécharger la version PDF de cette fiche",
|
||
key="telecharger_fiche_pdf"
|
||
)
|
||
else:
|
||
st.warning("Le fichier PDF de cette fiche n'est pas disponible.")
|
||
|
||
st.markdown("## Gestion des tickets pour cette fiche")
|
||
afficher_tickets_par_fiche(rechercher_tickets_gitea(fiche_choisie))
|
||
formulaire_creation_ticket_dynamique(fiche_choisie)
|
||
|
||
except Exception as e:
|
||
st.error(f"Erreur lors du chargement de la fiche : {e}")
|