Add sort to playlist/tracks association table
This commit is contained in:
parent
af895ef577
commit
adc4cec094
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,3 +7,4 @@ venv/
|
|||||||
Session.vim
|
Session.vim
|
||||||
*.flac
|
*.flac
|
||||||
*.mp3
|
*.mp3
|
||||||
|
StudioPlaylist.png
|
||||||
|
|||||||
70
app/model.py
70
app/model.py
@ -11,8 +11,7 @@ from sqlalchemy import (
|
|||||||
Float,
|
Float,
|
||||||
ForeignKey,
|
ForeignKey,
|
||||||
Integer,
|
Integer,
|
||||||
String,
|
String
|
||||||
Table
|
|
||||||
)
|
)
|
||||||
from sqlalchemy.orm.exc import NoResultFound
|
from sqlalchemy.orm.exc import NoResultFound
|
||||||
from sqlalchemy.orm import relationship, sessionmaker
|
from sqlalchemy.orm import relationship, sessionmaker
|
||||||
@ -69,45 +68,65 @@ class Settings(Base):
|
|||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
playlist_tracks = Table(
|
class PlaylistTracks(Base):
|
||||||
'playlistracks',
|
__tablename__ = 'playlisttracks'
|
||||||
Base.metadata,
|
Base.metadata,
|
||||||
Column('playlist_id', Integer, ForeignKey('playlists.id')),
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
Column('track_id', Integer, ForeignKey('tracks.id'))
|
playlist_id = Column(Integer, ForeignKey('playlists.id'), primary_key=True)
|
||||||
)
|
track_id = Column(Integer, ForeignKey('tracks.id'), primary_key=True)
|
||||||
|
sort = Column(Integer, nullable=False)
|
||||||
|
tracks = relationship("Tracks", back_populates="playlists")
|
||||||
|
playlists = relationship("Playlists", back_populates="tracks")
|
||||||
|
|
||||||
|
|
||||||
class Playlists(Base):
|
class Playlists(Base):
|
||||||
"""
|
"""
|
||||||
Usage:
|
Usage:
|
||||||
|
|
||||||
In [3]: pl = session.query(Playlists).filter(Playlists.id == 1).one()
|
pl = session.query(Playlists).filter(Playlists.id == 1).one()
|
||||||
|
|
||||||
In [4]: pl
|
pl
|
||||||
Out[4]: <Playlist(id=1, name=Default>
|
<Playlist(id=1, name=Default>
|
||||||
|
|
||||||
In [5]: tr = session.query(Tracks).filter(Tracks.id == 3837).one()
|
pl.tracks
|
||||||
|
[<__main__.PlaylistTracks at 0x7fcd20181c18>,
|
||||||
|
<__main__.PlaylistTracks at 0x7fcd20181c88>,
|
||||||
|
<__main__.PlaylistTracks at 0x7fcd20181be0>,
|
||||||
|
<__main__.PlaylistTracks at 0x7fcd20181c50>]
|
||||||
|
|
||||||
In [6]: tr
|
[a.tracks for a in pl.tracks]
|
||||||
Out[6]: <Track(id=3837, title=Babe, artist=Various, path=/home/[...]
|
[<Track(id=3992, title=Yesterday Man, artist=Various, path=/h[...]
|
||||||
|
<Track(id=2238, title=These Boots Are Made For Walkin', arti[...]
|
||||||
|
<Track(id=3837, title=Babe, artist=Various, path=/home/kae/m[...]
|
||||||
|
<Track(id=2332, title=Such Great Heights - Remastered, artis[...]]
|
||||||
|
|
||||||
In [7]: pl.tracks.append(tr)
|
glue = PlaylistTracks(sort=5)
|
||||||
...: session.commit()
|
|
||||||
|
|
||||||
In [8]: tr.playlists
|
tr = session.query(Tracks).filter(Tracks.id == 676).one()
|
||||||
Out[8]: [<Playlist(id=1, name=Default>]
|
|
||||||
|
|
||||||
In [9]: pl.tracks
|
tr
|
||||||
Out[9]: [<Track(id=3837, title=Babe, artist=Various, path=/home/[...]
|
<Track(id=676, title=Seven Nation Army, artist=White Stripes, path=/home/kae/music/White Stripes/Elephant/01. Seven Nation Army.flac>
|
||||||
|
|
||||||
|
glue.track_id = tr.id
|
||||||
|
|
||||||
|
pl.tracks.append(glue)
|
||||||
|
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
[a.tracks for a in pl.tracks]
|
||||||
|
[<Track(id=3992, title=Yesterday Man, artist=Various, path=/h[...]
|
||||||
|
<Track(id=2238, title=These Boots Are Made For Walkin', arti[...]
|
||||||
|
<Track(id=3837, title=Babe, artist=Various, path=/home/kae/m[...]
|
||||||
|
<Track(id=2332, title=Such Great Heights - Remastered, artis[...]
|
||||||
|
<Track(id=676, title=Seven Nation Army, artist=White Stripes[...]]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__tablename__ = "playlists"
|
__tablename__ = "playlists"
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True, autoincrement=True)
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
name = Column(String(32), nullable=False, unique=True)
|
name = Column(String(32), nullable=False, unique=True)
|
||||||
tracks = relationship(
|
tracks = relationship("PlaylistTracks",
|
||||||
"Tracks",
|
order_by="PlaylistTracks.sort",
|
||||||
secondary=playlist_tracks,
|
|
||||||
back_populates="playlists")
|
back_populates="playlists")
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
@ -123,7 +142,7 @@ class Playlists(Base):
|
|||||||
self.tracks.append(track)
|
self.tracks.append(track)
|
||||||
|
|
||||||
def get_tracks(self):
|
def get_tracks(self):
|
||||||
return self.tracks
|
return [a.tracks for a in self.tracks]
|
||||||
|
|
||||||
|
|
||||||
class Tracks(Base):
|
class Tracks(Base):
|
||||||
@ -139,10 +158,7 @@ class Tracks(Base):
|
|||||||
path = Column(String(2048), index=False, nullable=False)
|
path = Column(String(2048), index=False, nullable=False)
|
||||||
mtime = Column(Float, index=True)
|
mtime = Column(Float, index=True)
|
||||||
lastplayed = Column(DateTime, index=True, default=None)
|
lastplayed = Column(DateTime, index=True, default=None)
|
||||||
playlists = relationship(
|
playlists = relationship("PlaylistTracks", back_populates="tracks")
|
||||||
"Playlists",
|
|
||||||
secondary=playlist_tracks,
|
|
||||||
back_populates="tracks")
|
|
||||||
playdates_id = Column(Integer, ForeignKey('playdates.id'))
|
playdates_id = Column(Integer, ForeignKey('playdates.id'))
|
||||||
playdates = relationship("Playdates", back_populates="tracks")
|
playdates = relationship("Playdates", back_populates="tracks")
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,34 @@
|
|||||||
|
"""Add id to playlist association table
|
||||||
|
|
||||||
|
Revision ID: 9bf80ba3635f
|
||||||
|
Revises: f071129cbd93
|
||||||
|
Create Date: 2021-03-28 12:16:14.631579
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '9bf80ba3635f'
|
||||||
|
down_revision = 'f071129cbd93'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
conn = op.get_bind()
|
||||||
|
conn.execute(
|
||||||
|
"ALTER TABLE playlistracks ADD id INT PRIMARY KEY AUTO_INCREMENT FIRST"
|
||||||
|
)
|
||||||
|
conn.execute("RENAME TABLE playlistracks TO playlisttracks")
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
conn = op.get_bind()
|
||||||
|
conn.execute("ALTER TABLE playlistracks DROP id")
|
||||||
|
conn.execute("RENAME TABLE playlisttracks TO playlistracks")
|
||||||
|
# ### end Alembic commands ###
|
||||||
@ -0,0 +1,28 @@
|
|||||||
|
"""Add sort to playlist association table
|
||||||
|
|
||||||
|
Revision ID: f071129cbd93
|
||||||
|
Revises: f07b96a5e60f
|
||||||
|
Create Date: 2021-03-28 11:19:31.944110
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'f071129cbd93'
|
||||||
|
down_revision = 'f07b96a5e60f'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.add_column('playlistracks', sa.Column('sort', sa.Integer(), nullable=False))
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.drop_column('playlistracks', 'sort')
|
||||||
|
# ### end Alembic commands ###
|
||||||
Loading…
Reference in New Issue
Block a user