Move SQLAlchemy statements to models.py
This commit is contained in:
parent
813b325029
commit
9554336860
@ -11,6 +11,7 @@ from typing import List, Optional, Sequence
|
||||
from sqlalchemy.ext.associationproxy import association_proxy
|
||||
|
||||
from sqlalchemy import (
|
||||
bindparam,
|
||||
Boolean,
|
||||
DateTime,
|
||||
delete,
|
||||
@ -49,9 +50,9 @@ class Carts(Base):
|
||||
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
||||
cart_number: Mapped[int] = mapped_column(unique=True)
|
||||
name: Mapped[str] = mapped_column(String(256), index=True)
|
||||
duration: Mapped[int] = mapped_column(index=True)
|
||||
path: Mapped[str] = mapped_column(String(2048), index=False)
|
||||
enabled: Mapped[bool] = mapped_column(default=False)
|
||||
duration: Mapped[Optional[int]] = mapped_column(index=True)
|
||||
path: Mapped[Optional[str]] = mapped_column(String(2048), index=False)
|
||||
enabled: Mapped[Optional[bool]] = mapped_column(default=False)
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return (
|
||||
@ -63,7 +64,7 @@ class Carts(Base):
|
||||
self,
|
||||
session: scoped_session,
|
||||
cart_number: int,
|
||||
name: Optional[str] = None,
|
||||
name: str,
|
||||
duration: Optional[int] = None,
|
||||
path: Optional[str] = None,
|
||||
enabled: bool = True,
|
||||
@ -635,6 +636,26 @@ class PlaylistRows(Base):
|
||||
.values(plr_rownum=PlaylistRows.plr_rownum + move_by)
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def update_plr_rownumbers(
|
||||
session: scoped_session, playlist_id: int, sqla_map: List[dict[str, int]]
|
||||
) -> None:
|
||||
"""
|
||||
Take a {plrid: plr_rownum} dictionary and update the row numbers accordingly
|
||||
"""
|
||||
|
||||
# Update database. Ref:
|
||||
# https://docs.sqlalchemy.org/en/20/tutorial/data_update.html#the-update-sql-expression-construct
|
||||
stmt = (
|
||||
update(PlaylistRows)
|
||||
.where(
|
||||
PlaylistRows.playlist_id == playlist_id,
|
||||
PlaylistRows.id == bindparam("plrid"),
|
||||
)
|
||||
.values(plr_rownum=bindparam("plr_rownum"))
|
||||
)
|
||||
session.connection().execute(stmt, sqla_map)
|
||||
|
||||
|
||||
class Settings(Base):
|
||||
"""Manage settings"""
|
||||
|
||||
@ -15,7 +15,6 @@ from datetime import datetime, timedelta
|
||||
from pygame import mixer
|
||||
from time import sleep
|
||||
from typing import (
|
||||
Callable,
|
||||
cast,
|
||||
List,
|
||||
Optional,
|
||||
|
||||
@ -438,20 +438,8 @@ class PlaylistModel(QAbstractTableModel):
|
||||
plrid = self.playlist_rows[oldrow].plrid
|
||||
sqla_map.append({"plrid": plrid, "plr_rownum": newrow})
|
||||
|
||||
# Update database. Ref:
|
||||
# https://docs.sqlalchemy.org/en/20/tutorial/data_update.html#the-update-sql-expression-construct
|
||||
stmt = (
|
||||
update(PlaylistRows)
|
||||
.where(
|
||||
PlaylistRows.playlist_id == self.playlist_id,
|
||||
PlaylistRows.id == bindparam("plrid"),
|
||||
)
|
||||
.values(plr_rownum=bindparam("plr_rownum"))
|
||||
)
|
||||
|
||||
with Session() as session:
|
||||
session.connection().execute(stmt, sqla_map)
|
||||
|
||||
PlaylistRows.update_plr_rownumbers(session, self.playlist_id, sqla_map)
|
||||
# Update playlist_rows
|
||||
self.refresh_data(session)
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user