WIP: Move rows within and between playlists working
This commit is contained in:
parent
3cd764c893
commit
52d2269ece
@ -316,43 +316,55 @@ def move_rows_to_playlist(
|
|||||||
Move rows between playlists.
|
Move rows between playlists.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
with db.Session() as session:
|
log.debug(
|
||||||
# Prepare desination playlist
|
f"move_rows_to_playlist({from_rows=}, {from_playlist_id=}, {to_row=}, {to_playlist_id=})"
|
||||||
# Find last used row
|
|
||||||
last_row = session.execute(
|
|
||||||
select(func.max(PlaylistRows.row_number)).where(
|
|
||||||
PlaylistRows.playlist_id == to_playlist_id
|
|
||||||
)
|
)
|
||||||
).scalar_one()
|
|
||||||
if last_row is None:
|
with db.Session() as session:
|
||||||
last_row = -1
|
# Sanity check row numbers
|
||||||
# Make room in destination
|
_check_playlist_integrity(session, from_playlist_id, fix=False)
|
||||||
if to_row <= last_row:
|
_check_playlist_integrity(session, to_playlist_id, fix=False)
|
||||||
_move_rows(session, to_playlist_id, to_row, len(from_rows))
|
|
||||||
# Move rows
|
# Check there are no playlist rows with playlist_id == PENDING_MOVE
|
||||||
row_offset = to_row - min(from_rows)
|
pending_move_rows = get_playlist_rows(Config.PLAYLIST_PENDING_MOVE)
|
||||||
stmt = (
|
if pending_move_rows:
|
||||||
|
raise ApplicationError(f"move_rows_to_playlist: {pending_move_rows=}")
|
||||||
|
|
||||||
|
# Put rows to be moved into PENDING_MOVE playlist
|
||||||
|
session.execute(
|
||||||
update(PlaylistRows)
|
update(PlaylistRows)
|
||||||
.where(
|
.where(
|
||||||
PlaylistRows.playlist_id == from_playlist_id,
|
PlaylistRows.playlist_id == from_playlist_id,
|
||||||
PlaylistRows.row_number.in_(from_rows),
|
PlaylistRows.row_number.in_(from_rows),
|
||||||
)
|
)
|
||||||
.values(
|
.values(playlist_id=Config.PLAYLIST_PENDING_MOVE)
|
||||||
playlist_id=to_playlist_id,
|
|
||||||
row_number=PlaylistRows.row_number + row_offset,
|
|
||||||
)
|
)
|
||||||
)
|
|
||||||
session.execute(stmt)
|
|
||||||
# Remove gaps in source
|
|
||||||
_move_rows(
|
|
||||||
session=session,
|
|
||||||
playlist_id=from_playlist_id,
|
|
||||||
starting_row=max(from_rows) + 1,
|
|
||||||
move_by=(len(from_rows) * -1),
|
|
||||||
)
|
|
||||||
# Commit changes
|
|
||||||
session.commit()
|
session.commit()
|
||||||
# Sanity check
|
|
||||||
|
# Resequence remaining row numbers
|
||||||
|
_check_playlist_integrity(session, from_playlist_id, fix=True)
|
||||||
|
|
||||||
|
# Make space for moved rows.
|
||||||
|
_move_rows(session, to_playlist_id, to_row, len(from_rows))
|
||||||
|
|
||||||
|
# Move the PENDING_MOVE rows back and fixup row numbers
|
||||||
|
update_list: list[dict[str, int]] = []
|
||||||
|
next_row = to_row
|
||||||
|
# PLAYLIST_PENDING_MOVE may have gaps so don't check it
|
||||||
|
for row_to_move in get_playlist_rows(
|
||||||
|
Config.PLAYLIST_PENDING_MOVE, check_playlist_itegrity=False
|
||||||
|
):
|
||||||
|
update_list.append(
|
||||||
|
{"id": row_to_move.playlistrow_id, "row_number": next_row}
|
||||||
|
)
|
||||||
|
update_list.append(
|
||||||
|
{"id": row_to_move.playlistrow_id, "playlist_id": to_playlist_id}
|
||||||
|
)
|
||||||
|
next_row += 1
|
||||||
|
session.execute(update(PlaylistRows), update_list)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
# Sanity check row numbers
|
||||||
_check_playlist_integrity(session, from_playlist_id, fix=False)
|
_check_playlist_integrity(session, from_playlist_id, fix=False)
|
||||||
_check_playlist_integrity(session, to_playlist_id, fix=False)
|
_check_playlist_integrity(session, to_playlist_id, fix=False)
|
||||||
|
|
||||||
@ -416,9 +428,16 @@ def move_rows_within_playlist(
|
|||||||
# Move the PENDING_MOVE rows back and fixup row numbers
|
# Move the PENDING_MOVE rows back and fixup row numbers
|
||||||
update_list: list[dict[str, int]] = []
|
update_list: list[dict[str, int]] = []
|
||||||
next_row = space_row
|
next_row = space_row
|
||||||
for row_to_move in get_playlist_rows(Config.PLAYLIST_PENDING_MOVE):
|
# PLAYLIST_PENDING_MOVE may have gaps so don't check it
|
||||||
update_list.append({"id": row_to_move.playlistrow_id, "row_number": next_row})
|
for row_to_move in get_playlist_rows(
|
||||||
update_list.append({"id": row_to_move.playlistrow_id, "playlist_id": playlist_id})
|
Config.PLAYLIST_PENDING_MOVE, check_playlist_itegrity=False
|
||||||
|
):
|
||||||
|
update_list.append(
|
||||||
|
{"id": row_to_move.playlistrow_id, "row_number": next_row}
|
||||||
|
)
|
||||||
|
update_list.append(
|
||||||
|
{"id": row_to_move.playlistrow_id, "playlist_id": playlist_id}
|
||||||
|
)
|
||||||
next_row += 1
|
next_row += 1
|
||||||
session.execute(update(PlaylistRows), update_list)
|
session.execute(update(PlaylistRows), update_list)
|
||||||
session.commit()
|
session.commit()
|
||||||
@ -554,7 +573,9 @@ def get_playlist_row(playlistrow_id: int) -> PlaylistRowDTO | None:
|
|||||||
return dto
|
return dto
|
||||||
|
|
||||||
|
|
||||||
def get_playlist_rows(playlist_id: int) -> list[PlaylistRowDTO]:
|
def get_playlist_rows(
|
||||||
|
playlist_id: int, check_playlist_itegrity=True
|
||||||
|
) -> list[PlaylistRowDTO]:
|
||||||
# Alias PlaydatesTable for subquery
|
# Alias PlaydatesTable for subquery
|
||||||
LatestPlaydate = aliased(Playdates)
|
LatestPlaydate = aliased(Playdates)
|
||||||
|
|
||||||
@ -597,7 +618,10 @@ def get_playlist_rows(playlist_id: int) -> list[PlaylistRowDTO]:
|
|||||||
results = session.execute(stmt).all()
|
results = session.execute(stmt).all()
|
||||||
# Sanity check
|
# Sanity check
|
||||||
# TODO: would be good to be confident at removing this
|
# TODO: would be good to be confident at removing this
|
||||||
_check_playlist_integrity(session=session, playlist_rows=results, fix=False)
|
if check_playlist_itegrity:
|
||||||
|
_check_playlist_integrity(
|
||||||
|
session=session, playlist_id=playlist_id, fix=False
|
||||||
|
)
|
||||||
|
|
||||||
dto_list = []
|
dto_list = []
|
||||||
for row in results:
|
for row in results:
|
||||||
|
|||||||
@ -241,3 +241,34 @@ class MyTestCase(unittest.TestCase):
|
|||||||
for row in repository.get_playlist_rows(playlist.playlist_id):
|
for row in repository.get_playlist_rows(playlist.playlist_id):
|
||||||
new_order.append(int(row.note))
|
new_order.append(int(row.note))
|
||||||
assert new_order == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
assert new_order == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
|
|
||||||
|
def test_move_rows_to_playlist(self):
|
||||||
|
number_of_rows = 11
|
||||||
|
rows_to_move = [2, 4, 6]
|
||||||
|
to_row = 5
|
||||||
|
|
||||||
|
(playlist_src, model_src) = self.create_rows("src playlist", number_of_rows)
|
||||||
|
(playlist_dst, model_dst) = self.create_rows("dst playlist", number_of_rows)
|
||||||
|
|
||||||
|
repository.move_rows_to_playlist(
|
||||||
|
rows_to_move, playlist_src.playlist_id, to_row, playlist_dst.playlist_id
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check we have all rows and plr_rownums are correct
|
||||||
|
new_order_src = []
|
||||||
|
for row in repository.get_playlist_rows(playlist_src.playlist_id):
|
||||||
|
new_order_src.append(int(row.note))
|
||||||
|
assert new_order_src == [0, 1, 3, 5, 7, 8, 9, 10]
|
||||||
|
new_order_dst = []
|
||||||
|
for row in repository.get_playlist_rows(playlist_dst.playlist_id):
|
||||||
|
new_order_dst.append(int(row.note))
|
||||||
|
assert new_order_dst == [0, 1, 2, 3, 4, 2, 4, 6, 5, 6, 7, 8, 9, 10]
|
||||||
|
|
||||||
|
def test_remove_rows(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_get_playlist_by_id(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def test_settings(self):
|
||||||
|
pass
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user