import streamlit as st from utils.translations import _ def get_produits_personnalises(G): """Récupère la liste des produits personnalisés du niveau 0.""" return sorted([n for n, d in G.nodes(data=True) if d.get("niveau") == "0" and d.get("personnalisation") == "oui"]) def supprimer_produit(G, prod): """Supprime un produit du graphe.""" G.remove_node(prod) st.success(f"{prod} {str(_('pages.personnalisation.deleted'))}") st.session_state.pop("prod_sel", None) return G def get_operations_disponibles(G): """Récupère la liste des opérations d'assemblage disponibles.""" return 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)) ]) def get_operations_actuelles(G, prod): """Récupère les opérations actuellement liées au produit.""" return [succ for succ in G.successors(prod) if G.nodes[succ].get("niveau") == "10"] def get_composants_niveau1(G): """Récupère la liste des composants de niveau 1.""" return sorted([n for n, d in G.nodes(data=True) if d.get("niveau") == "1"]) def get_composants_lies(G, prod): """Récupère les composants actuellement liés au produit.""" return [succ for succ in G.successors(prod) if G.nodes[succ].get("niveau") == "1"] def mettre_a_jour_operations(G, prod, curr_ops, sel_op): """Met à jour les opérations liées au produit.""" none_option = str(_("pages.personnalisation.none", "-- Aucune --")) for op in curr_ops: if sel_op == none_option or op != sel_op: G.remove_edge(prod, op) if sel_op != none_option and (not curr_ops or sel_op not in curr_ops): G.add_edge(prod, sel_op) return G def mettre_a_jour_composants(G, prod, linked, nouveaux): """Met à jour les composants liés au produit.""" for comp in set(linked) - set(nouveaux): G.remove_edge(prod, comp) for comp in set(nouveaux) - set(linked): G.add_edge(prod, comp) return G def modifier_produit(G): st.markdown(f"## {str(_('pages.personnalisation.modify_product'))}") # Sélection du produit à modifier produits0 = get_produits_personnalises(G) sel_display = st.multiselect(str(_("pages.personnalisation.products_to_modify")), options=produits0) if not sel_display: return G # Obtention du produit sélectionné prod = sel_display[0] # Suppression du produit si demandé if st.button(f"{str(_('pages.personnalisation.delete'))} {prod}"): return supprimer_produit(G, prod) # Gestion des opérations d'assemblage ops_dispo = get_operations_disponibles(G) curr_ops = get_operations_actuelles(G, prod) default_idx = ops_dispo.index(curr_ops[0]) + 1 if curr_ops and curr_ops[0] in ops_dispo else 0 sel_op = st.selectbox(str(_("pages.personnalisation.linked_assembly_operation")), [str(_("pages.personnalisation.none"))] + ops_dispo, index=default_idx) # Gestion des composants niveau1 = get_composants_niveau1(G) linked = get_composants_lies(G, prod) nouveaux = st.multiselect(f"{str(_('pages.personnalisation.components_linked_to'))} {prod}", options=niveau1, default=linked) # Mise à jour des liens si demandé if st.button(f"{str(_('pages.personnalisation.update'))} {prod}"): G = mettre_a_jour_operations(G, prod, curr_ops, sel_op) G = mettre_a_jour_composants(G, prod, linked, nouveaux) st.success(f"{prod} {str(_('pages.personnalisation.updated'))}") return G