Manglede tabeller

This commit is contained in:
2026-04-13 15:37:17 +02:00
parent b066b6d92c
commit 69d1d484a2
2 changed files with 65 additions and 15 deletions

View File

@@ -52,6 +52,8 @@ class Song(Base):
owner: Mapped["User"] = relationship("User", back_populates="songs")
project_songs: Mapped[list["ProjectSong"]] = relationship("ProjectSong", back_populates="song")
song_dances: Mapped[list["SongDance"]] = relationship("SongDance", back_populates="song", cascade="all, delete-orphan")
song_alt_dances: Mapped[list["SongAltDance"]] = relationship("SongAltDance", back_populates="song", cascade="all, delete-orphan")
# ── Dans-entitet ──────────────────────────────────────────────────────────────
@@ -149,6 +151,36 @@ class PlaylistShare(Base):
shared_with: Mapped["User|None"] = relationship("User", foreign_keys=[shared_with_id], back_populates="playlist_shares")
# ── Sang-dans tags ────────────────────────────────────────────────────────────
class SongDance(Base):
"""Dans-tags på en sang (brugerens egne tags)."""
__tablename__ = "song_dances"
__table_args__ = (UniqueConstraint("song_id", "dance_id", name="uq_song_dance"),)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
song_id: Mapped[str] = mapped_column(String(36), ForeignKey("songs.id"), nullable=False)
dance_id: Mapped[int] = mapped_column(Integer, ForeignKey("dances.id"), nullable=False)
dance_order: Mapped[int] = mapped_column(Integer, default=1)
song: Mapped["Song"] = relationship("Song", back_populates="song_dances")
dance: Mapped["Dance"] = relationship("Dance")
class SongAltDance(Base):
"""Alternativ-dans tags på en sang."""
__tablename__ = "song_alt_dances"
__table_args__ = (UniqueConstraint("song_id", "dance_id", name="uq_song_alt_dance"),)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
song_id: Mapped[str] = mapped_column(String(36), ForeignKey("songs.id"), nullable=False)
dance_id: Mapped[int] = mapped_column(Integer, ForeignKey("dances.id"), nullable=False)
note: Mapped[str] = mapped_column(String(255), default="")
song: Mapped["Song"] = relationship("Song", back_populates="song_alt_dances")
dance: Mapped["Dance"] = relationship("Dance")
# ── Community dans-tags ───────────────────────────────────────────────────────
class CommunityDance(Base):

View File

@@ -140,27 +140,43 @@ def push(
db.flush()
dance_id_map[key] = dance.id
# ── Community dans-tags ────────────────────────────────────────────────────
# ── Sang-dans tags (brugerens egne) ───────────────────────────────────────
from app.models import SongDance, SongAltDance
for sd in payload.song_dances:
song_id = song_id_map.get(sd.song_local_id)
if not song_id:
continue
song = db.query(Song).filter_by(id=song_id).first()
level_id = level_map.get(sd.level_name.lower()) if sd.level_name else None
key = f"{sd.dance_name.lower()}|{level_id}"
dance_id = dance_id_map.get(key)
if not dance_id:
continue
# Indsend som community dans-tag
existing = db.query(CommunityDance).filter_by(
song_title=song.title, song_artist=song.artist, dance_id=dance_id
existing = db.query(SongDance).filter_by(
song_id=song_id, dance_id=dance_id
).first()
if not existing:
cd = CommunityDance(
song_title=song.title, song_artist=song.artist,
dance_id=dance_id, submitted_by=me.id,
)
db.add(cd)
db.add(SongDance(
song_id=song_id, dance_id=dance_id,
dance_order=sd.dance_order,
))
for sa in payload.song_alts:
song_id = song_id_map.get(sa.song_local_id)
if not song_id:
continue
level_id = level_map.get(sa.level_name.lower()) if sa.level_name else None
key = f"{sa.dance_name.lower()}|{level_id}"
dance_id = dance_id_map.get(key)
if not dance_id:
continue
existing = db.query(SongAltDance).filter_by(
song_id=song_id, dance_id=dance_id
).first()
if not existing:
db.add(SongAltDance(
song_id=song_id, dance_id=dance_id,
note=sa.note,
))
# ── Playlister ────────────────────────────────────────────────────────────
playlist_id_map = {}
@@ -301,18 +317,20 @@ def pull(
"songs": sorted(songs_out, key=lambda x: x["position"]),
})
# Brugerens egne dans-tags (via community dances submitted_by me)
# Brugerens egne dans-tags
from app.models import SongDance, SongAltDance
song_tags = []
for cd in db.query(CommunityDance).filter_by(submitted_by=me.id).all():
dance = db.query(Dance).filter_by(id=cd.dance_id).first()
for sd in db.query(SongDance).join(Song).filter(Song.owner_id == me.id).all():
dance = db.query(Dance).filter_by(id=sd.dance_id).first()
if not dance:
continue
level = db.query(DanceLevel).filter_by(id=dance.level_id).first() if dance.level_id else None
song_tags.append({
"song_title": cd.song_title,
"song_artist": cd.song_artist,
"song_title": sd.song.title,
"song_artist": sd.song.artist,
"dance_name": dance.name,
"level_name": level.name if level else "",
"dance_order": sd.dance_order,
})
return {