Escollir partitura del tema en un set

This commit is contained in:
marc
2026-02-16 23:03:01 +01:00
parent 443c1b1391
commit 89f72dbb33
13 changed files with 1385 additions and 22 deletions

View File

@@ -26,15 +26,34 @@ def unknown_tune() -> lilypond_model.LilypondTune:
)
def tune_from_tema(tema: model.Tema | None, score_source: str | None = None) -> lilypond_model.LilypondTune:
def tune_from_tema(
tema: model.Tema | None,
score_source: str | None = None,
score_id: int | None = None,
) -> lilypond_model.LilypondTune:
"""
The given `tema` is assumed to have properties, lyrics and scores (if source is None)
"""
if tema is None:
return unknown_tune()
# Determine the score source to use
effective_source = score_source
if effective_source is None and tema.scores:
if score_id is not None:
# Find the specific score by ID
for score in tema.scores:
if score.id == score_id:
effective_source = score.source
break
if effective_source is None:
# Fallback to main_score or first score
main = tema.main_score()
effective_source = main.source if main else tema.scores[0].source
return lilypond_model.LilypondTune(
header=lilypond_model.HeaderData.from_tema(tema=tema),
score_source=score_source or (tema.scores[0].source if tema.scores else None),
score_source=effective_source,
lyrics=lilypond_model.LyricsText.from_lyrics(lyrics=tema.lyrics[0]) if tema.lyrics else None,
)
@@ -84,15 +103,23 @@ def set_from_set(set_entry: playlists_model.Set) -> lilypond_model.LilypondSet:
"""
The tune_set is assumed to be enriched with tunes
"""
tema_ids = [tema_in_set.tema_id for tema_in_set in set_entry.temes]
temes_by_id = {
tema_in_set.tema_id: tema_in_set.tema
for tema_in_set in set_entry.temes
if tema_in_set.id is not None and tema_in_set.tema
}
temes = [temes_by_id[tema_id] if tema_id is not None else None for tema_id in tema_ids]
temes = [temes_by_id.get(t.tema_id) for t in set_entry.temes]
set_title = build_set_title(temes=temes)
tunes = tunes_from_temes(temes)
# Build tunes with score_id consideration
tunes = [
tune_from_tema(
tema=temes_by_id.get(tema_in_set.tema_id),
score_id=tema_in_set.score_id,
)
for tema_in_set in set_entry.temes
]
return lilypond_model.LilypondSet(
title=set_title,
tunes=tunes

View File

@@ -95,9 +95,11 @@ def delete_tema(entry_id: int, con: Connection | None = None):
def set_tema(playlist_id: int, set_id: int, entry_id: int, tema_id: int | None,
con: Connection | None = None):
score_id: int | None = None, con: Connection | None = None):
with get_connection(con) as con:
new_entry = playlists.PlaylistEntry(id=entry_id, playlist_id=playlist_id, set_id=set_id, tema_id=tema_id)
new_entry = playlists.PlaylistEntry(
id=entry_id, playlist_id=playlist_id, set_id=set_id, tema_id=tema_id, score_id=score_id
)
write.update_playlist_entry(entry=new_entry, con=con)
@@ -111,11 +113,13 @@ async def get_or_create_set_score(tune_set: playlists.Set) -> playlists.SetScore
if not tune_set.temes:
return None
if len(tune_set.temes) == 1:
if (tema := tune_set.temes[0].tema) is None or (main_score := tema.main_score()) is None:
tema_in_set = tune_set.temes[0]
effective_score = tema_in_set.get_effective_score()
if effective_score is None:
return None
return playlists.SetScore(
pdf_url=main_score.pdf_url,
img_url=main_score.img_url,
pdf_url=effective_score.pdf_url,
img_url=effective_score.img_url,
)
lilypond_set = lilypond_build.set_from_set(set_entry=tune_set)