From 64799ccc6159951b7ed9c1da89535f5ad288c04c Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Wed, 6 Jul 2022 21:40:35 +0100 Subject: [PATCH] Scheme fixed for v2.4 (nee v3) --- app/models.py | 171 +++++++++++++++++++++++++------------------------- 1 file changed, 86 insertions(+), 85 deletions(-) diff --git a/app/models.py b/app/models.py index c5dbd37..7e627f6 100644 --- a/app/models.py +++ b/app/models.py @@ -9,7 +9,7 @@ from datetime import datetime from typing import List, Optional # # from pydub import AudioSegment -# from sqlalchemy.ext.associationproxy import association_proxy +from sqlalchemy.ext.associationproxy import association_proxy # from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta from sqlalchemy import ( Boolean, @@ -20,7 +20,7 @@ from sqlalchemy import ( # func, Integer, String, - # UniqueConstraint, + UniqueConstraint, select, ) # from sqlalchemy.exc import IntegrityError @@ -52,13 +52,19 @@ Base = declarative_base() class NoteColours(Base): __tablename__ = 'notecolours' - id: int = Column(Integer, primary_key=True, autoincrement=True) - substring: str = Column(String(256), index=False) - colour: str = Column(String(21), index=False) - enabled: bool = Column(Boolean, default=True, index=True) - is_regex: bool = Column(Boolean, default=False, index=False) - is_casesensitive: bool = Column(Boolean, default=False, index=False) - order: int = Column(Integer, index=True) + id = Column(Integer, primary_key=True, autoincrement=True) + substring = Column(String(256), index=False) + colour = Column(String(21), index=False) + enabled = Column(Boolean, default=True, index=True) + is_regex = Column(Boolean, default=False, index=False) + is_casesensitive = Column(Boolean, default=False, index=False) + order = Column(Integer, index=True) + + def __repr__(self) -> str: + return ( + f"" + ) # # def __init__( # self, session: Session, substring: str, colour: str, @@ -74,12 +80,6 @@ class NoteColours(Base): # session.add(self) # session.flush() # -# def __repr__(self) -> str: -# return ( -# f"" -# ) -# # @classmethod # def get_all(cls, session: Session) -> Optional[List["NoteColours"]]: # """Return all records""" @@ -122,17 +122,17 @@ class NoteColours(Base): # return rec.colour # # return None - - -class Notes(Base): - __tablename__ = 'notes' - - id: int = Column(Integer, primary_key=True, autoincrement=True) - playlist_id: int = Column(Integer, ForeignKey('playlists.id')) - playlist: RelationshipProperty = relationship( - "Playlists", back_populates="notes", lazy="joined") - row: int = Column(Integer, nullable=False) - note: str = Column(String(256), index=False) +# +# +#class Notes(Base): +# __tablename__ = 'notes' +# +# id: int = Column(Integer, primary_key=True, autoincrement=True) +# playlist_id: int = Column(Integer, ForeignKey('playlists.id')) +# playlist: RelationshipProperty = relationship( +# "Playlists", back_populates="notes", lazy="joined") +# row: int = Column(Integer, nullable=False) +# note: str = Column(String(256), index=False) # # def __init__(self, session: Session, playlist_id: int, # row: int, text: str) -> None: @@ -213,10 +213,15 @@ class Playdates(Base): __tablename__ = 'playdates' id: int = Column(Integer, primary_key=True, autoincrement=True) - lastplayed: datetime = Column(DateTime, index=True, default=None) - track_id: int = Column(Integer, ForeignKey('tracks.id')) - track: RelationshipProperty = relationship( - "Tracks", back_populates="playdates", lazy="joined") + lastplayed = Column(DateTime, index=True, default=None) + track_id = Column(Integer, ForeignKey('tracks.id')) + track = relationship("Tracks", back_populates="playdates") + + def __repr__(self) -> str: + return ( + f"" + ) # # def __init__(self, session: Session, track_id: int) -> None: # """Record that track was played""" @@ -268,23 +273,20 @@ class Playlists(Base): id: int = Column(Integer, primary_key=True, autoincrement=True) name: str = Column(String(32), nullable=False, unique=True) - last_used: datetime = Column(DateTime, default=None, nullable=True) + last_used = Column(DateTime, default=None, nullable=True) loaded: bool = Column(Boolean, default=True, nullable=False) -# notes = relationship( -# "Notes", order_by="Notes.row", -# back_populates="playlist", lazy="select" -# ) -# -# tracks = association_proxy('playlist_tracks', 'tracks') -# row = association_proxy('playlist_tracks', 'row') + rows = relationship("PlaylistRows", back_populates="playlist", + cascade="all, delete-orphan") + + def __repr__(self) -> str: + return f"" + + # # def __init__(self, session: Session, name: str) -> None: # self.name = name # session.add(self) # session.flush() - - def __repr__(self) -> str: - return f"" # # def add_track( # self, session: Session, track_id: int, @@ -409,30 +411,32 @@ class Playlists(Base): # cascade="all, delete-orphan" # ) # ) + + class PlaylistRows(Base): __tablename__ = 'playlist_rows' - id: int = Column(Integer, primary_key=True, autoincrement=True) - playlist_id: int = Column(Integer, ForeignKey('playlists.id'), - primary_key=True) - row: int = Column(Integer, nullable=False) - text: str = Column(String(2048), index=False) - track_id: int = Column(Integer, ForeignKey('tracks.id'), primary_key=True) - tracks: RelationshipProperty = relationship("Tracks") - playlist: RelationshipProperty = relationship( - Playlists, - backref=backref( - "playlist_tracks", - collection_class=attribute_mapped_collection("row"), - lazy="joined", - cascade="all, delete-orphan" + id = Column(Integer, primary_key=True, autoincrement=True) + row_number = Column(Integer, nullable=False) + note = Column(String(2048), index=False) + playlist_id = Column(Integer, ForeignKey('playlists.id'), nullable=False) + playlist = relationship(Playlists, back_populates="rows") + track_id = Column(Integer, ForeignKey('tracks.id'), nullable=True) + track = relationship("Tracks", back_populates="playlistrows") + + # Ensure row numbers are unique within each playlist + __table_args__ = (UniqueConstraint + ('row_number', 'playlist_id', name="uniquerow"), + ) + + def __repr__(self) -> str: + return ( + f"" ) - ) -# # Ensure row numbers are unique within each playlist -# __table_args__ = (UniqueConstraint -# ('row', 'playlist_id', name="uniquerow"), -# ) -# + + # def __init__( # self, session: Session, playlist_id: int, track_id: int, # row: int) -> None: @@ -478,9 +482,9 @@ class Settings(Base): id: int = Column(Integer, primary_key=True, autoincrement=True) name: str = Column(String(64), nullable=False, unique=True) - f_datetime: datetime = Column(DateTime, default=None, nullable=True) + f_datetime = Column(DateTime, default=None, nullable=True) f_int: int = Column(Integer, default=None, nullable=True) - f_string: str = Column(String(128), default=None, nullable=True) + f_string = Column(String(128), default=None, nullable=True) def __repr__(self) -> str: value = self.f_datetime or self.f_int or self.f_string @@ -517,22 +521,25 @@ class Tracks(Base): __tablename__ = 'tracks' id: int = Column(Integer, primary_key=True, autoincrement=True) - title: str = Column(String(256), index=True) - artist: str = Column(String(256), index=True) - duration: int = Column(Integer, index=True) - start_gap: int = Column(Integer, index=False) - fade_at: int = Column(Integer, index=False) - silence_at: int = Column(Integer, index=False) - path: str = Column(String(2048), index=False, nullable=False) - mtime: float = Column(Float, index=True) - lastplayed: datetime = Column(DateTime, index=True, default=None) - playlists: RelationshipProperty = relationship("PlaylistRows", - back_populates="tracks", - lazy="joined") - playdates: RelationshipProperty = relationship("Playdates", - back_populates="track" - "", - lazy="joined") + title = Column(String(256), index=True) + artist = Column(String(256), index=True) + duration = Column(Integer, index=True) + start_gap = Column(Integer, index=False) + fade_at = Column(Integer, index=False) + silence_at = Column(Integer, index=False) + path = Column(String(2048), index=False, nullable=False) + mtime = Column(Float, index=True) + lastplayed = Column(DateTime, index=True, default=None) + playlistrows = relationship("PlaylistRows", back_populates="track") + playlists = association_proxy("playlistrows", "playlist") + playdates = relationship("Playdates", back_populates="track") + + def __repr__(self) -> str: + return ( + f"" + ) +# # # def __init__( # self, @@ -560,12 +567,6 @@ class Tracks(Base): # session.add(self) # session.flush() # -# def __repr__(self) -> str: -# return ( -# f"" -# ) -# # @staticmethod # def get_all_paths(session) -> List[str]: # """Return a list of paths of all tracks"""