Files
DanceVideos/app/playlists/playlist_page.py
2025-10-13 17:31:57 +02:00

94 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import streamlit as st
from playlists import playlist_db
from playlists.playlist_model import Playlist, RuleSet
from playlists.playlist_controller import playlist_manual_editor, playlist_dynamic_editor
from datetime import datetime
from cache.video_summary import rebuild_video_summary
def main():
st.title("🎵 Gestion des Playlists")
if st.button("🔁 Recalculer le cache vidéo"):
rebuild_video_summary()
st.success("Cache mis à jour !")
# --- Barre latérale : recherche & filtres ---
st.sidebar.header("🔎 Recherche de playlists")
search_term = st.sidebar.text_input("Filtrer par nom ou description")
date_filter = st.sidebar.date_input("Créées après", value=None)
playlists = playlist_db.load_all_playlists()
# Appliquer les filtres
filtered = []
for p in playlists:
if search_term.lower() not in p.name.lower() and search_term.lower() not in (p.description or "").lower():
continue
if date_filter and datetime.fromisoformat(p.created_at) < datetime.combine(date_filter, datetime.min.time()):
continue
filtered.append(p)
# --- Sélection ou création ---
names = ["( Nouvelle playlist)"] + [p.name for p in filtered]
selected_name = st.selectbox("Sélectionnez une playlist", names, key="playlist_select")
# --- Création ---
if selected_name == "( Nouvelle playlist)":
st.subheader("Créer une nouvelle playlist")
name = st.text_input("Nom")
desc = st.text_area("Description")
type_choice = st.radio("Type", ["manual", "dynamic"])
if st.button("Créer"):
if not name.strip():
st.error("Le nom ne peut pas être vide.")
else:
pl = Playlist(name=name.strip(), description=desc, type=type_choice, rules=RuleSet())
pl.save()
st.session_state["playlist_select"] = pl.name # ✅ sélectionne automatiquement
if hasattr(st, "rerun"):
st.rerun()
else:
st.experimental_rerun()
return
# --- Mode édition ---
current = next((p for p in playlists if p.name == selected_name), None)
if not current:
st.warning("Aucune playlist sélectionnée.")
return
# --- Barre dactions ---
st.subheader(f"🎞️ Playlist : {current.name}")
new_name = st.text_input("Renommer", value=current.name)
new_desc = st.text_area("Description", value=current.description or "")
if st.button("💾 Sauvegarder les métadonnées"):
current.name = new_name
current.description = new_desc
current.save()
st.success("Mise à jour enregistrée ✅")
if hasattr(st, "rerun"):
st.rerun()
else:
st.experimental_rerun()
col1, col2, col3 = st.columns([1, 1, 2])
with col1:
if st.button("🗑️ Supprimer"):
playlist_db.delete_playlist(current.id)
st.success("Playlist supprimée ✅")
if hasattr(st, "rerun"):
st.rerun()
else:
st.experimental_rerun()
with col2:
if st.button("⏪ Retour à la liste"):
st.session_state.pop("playlist_select", None)
st.rerun()
st.divider()
# --- Éditeur selon le type ---
if current.type == "manual":
playlist_manual_editor(current)
else:
playlist_dynamic_editor(current)