2025-06-11 14:57:53 +02:00

77 lines
3.1 KiB
Python

import streamlit as st
import networkx as nx
from utils.translations import _
from utils.persistance import maj_champ_statut, get_champ_statut
def ajouter_produit(G: nx.DiGraph) -> nx.DiGraph:
"""
Ajoute un produit personnalisé dans le graphe en cours.
Args:
G (nx.DiGraph): graphe en cours.
Returns:
nx.DiGraph: le graphe avec le nouveau produit
Notes:
Cette fonction ajoute un nouveau produit final temporaire
au graphe de référence.
"""
st.markdown(f"## {str(_('pages.personnalisation.add_new_product'))}")
if "pages.personnalisation.create_product.fait" not in st.session_state and get_champ_statut("pages.personnalisation.create_product.fait") == "oui":
index = 0
while True:
new_prod = get_champ_statut(f"pages.personnalisation.create_product.{index}.nom")
if new_prod == "":
break # Fin de la liste
G.add_node(new_prod, niveau="0", personnalisation="oui", label=new_prod)
sel_new_op = get_champ_statut(f"pages.personnalisation.create_product.{index}.edge")
if sel_new_op:
G.add_edge(new_prod, sel_new_op)
i = 0
while True:
comp = get_champ_statut(f"pages.personnalisation.create_product.{index}.composants.{i}")
if comp == "":
break
G.add_edge(new_prod, comp)
i += 1
index += 1
new_prod = st.text_input(str(_("pages.personnalisation.new_product_name")), key="new_prod")
if new_prod:
ops_dispo = sorted([
n for n, d in G.nodes(data=True)
if d.get("niveau") == "10"
and any(G.has_edge(p, n) and G.nodes[p].get("niveau") == "0" for p in G.predecessors(n))
])
sel_new_op = st.selectbox(str(_("pages.personnalisation.assembly_operation")), [str(_("pages.personnalisation.none"))] + ops_dispo, index=0)
niveau1 = sorted([n for n, d in G.nodes(data=True) if d.get("niveau") == "1"])
sel_comps = st.multiselect(str(_("pages.personnalisation.components_to_link")), options=niveau1)
if st.button(str(_("pages.personnalisation.create_product"))):
G.add_node(new_prod, niveau="0", personnalisation="oui", label=new_prod)
maj_champ_statut("pages.personnalisation.create_product.fait", "oui")
st.session_state["pages.personnalisation.create_product.fait"] = "oui"
i = 0
while get_champ_statut(f"pages.personnalisation.create_product.{i}.nom") != "":
i += 1
maj_champ_statut(f"pages.personnalisation.create_product.{i}.nom", new_prod)
if sel_new_op != str(_("pages.personnalisation.none")):
G.add_edge(new_prod, sel_new_op)
maj_champ_statut(f"pages.personnalisation.create_product.{i}.edge", sel_new_op)
for j, comp in enumerate(sel_comps):
G.add_edge(new_prod, comp)
maj_champ_statut(f"pages.personnalisation.create_product.{i}.composants.{j}", comp)
st.success(f"{new_prod} {str(_('pages.personnalisation.added'))}")
return G