Use basedpyright refactor
This commit is contained in:
@@ -1,7 +1,18 @@
|
||||
from typing import TypedDict
|
||||
|
||||
from folkugat_web.model import playlists as model
|
||||
|
||||
PlaylistRowTuple = tuple[int, int, int, int | None]
|
||||
|
||||
def playlist_entry_to_row(tema_in_set: model.PlaylistEntry) -> dict:
|
||||
|
||||
class PlaylistRowDict(TypedDict):
|
||||
id: int | None
|
||||
session_id: int
|
||||
set_id: int
|
||||
tema_id: int | None
|
||||
|
||||
|
||||
def playlist_entry_to_row(tema_in_set: model.PlaylistEntry) -> PlaylistRowDict:
|
||||
return {
|
||||
'id': tema_in_set.id,
|
||||
'session_id': tema_in_set.session_id,
|
||||
@@ -10,7 +21,7 @@ def playlist_entry_to_row(tema_in_set: model.PlaylistEntry) -> dict:
|
||||
}
|
||||
|
||||
|
||||
def row_to_playlist_entry(row: tuple) -> model.PlaylistEntry:
|
||||
def row_to_playlist_entry(row: PlaylistRowTuple) -> model.PlaylistEntry:
|
||||
return model.PlaylistEntry(
|
||||
id=row[0],
|
||||
session_id=row[1],
|
||||
@@ -1,9 +1,7 @@
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
|
||||
|
||||
def create_db(con: Optional[Connection] = None):
|
||||
def create_db(con: Connection | None = None):
|
||||
with get_connection(con) as con:
|
||||
create_playlists_table(con)
|
||||
|
||||
@@ -11,7 +9,7 @@ def create_db(con: Optional[Connection] = None):
|
||||
def drop_playlists_table(con: Connection):
|
||||
query = "DROP TABLE IF EXISTS playlists"
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
_ = cur.execute(query)
|
||||
|
||||
|
||||
def create_playlists_table(con: Connection):
|
||||
@@ -24,4 +22,4 @@ def create_playlists_table(con: Connection):
|
||||
)
|
||||
"""
|
||||
cur = con.cursor()
|
||||
cur.execute(query)
|
||||
_ = cur.execute(query)
|
||||
|
||||
@@ -1,19 +1,27 @@
|
||||
from collections.abc import Iterator
|
||||
from typing import Optional
|
||||
from typing import TypedDict
|
||||
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
from folkugat_web.dal.sql.sessions import conversion as sessions_conversion
|
||||
from folkugat_web.model import playlists as model
|
||||
from folkugat_web.model.sessions import Session
|
||||
|
||||
from ._conversion import row_to_playlist_entry
|
||||
from . import conversion
|
||||
|
||||
|
||||
class QueryData(TypedDict, total=False):
|
||||
id: int
|
||||
set_id: int
|
||||
session_id: int
|
||||
|
||||
|
||||
def _filter_clause(
|
||||
entry_id: Optional[int] = None,
|
||||
set_id: Optional[int] = None,
|
||||
session_id: Optional[int] = None,
|
||||
) -> tuple[str, dict]:
|
||||
filter_clauses = []
|
||||
query_data = {}
|
||||
entry_id: int | None = None,
|
||||
set_id: int | None = None,
|
||||
session_id: int | None = None,
|
||||
) -> tuple[str, QueryData]:
|
||||
filter_clauses: list[str] = []
|
||||
query_data: QueryData = {}
|
||||
|
||||
if entry_id is not None:
|
||||
filter_clauses.append("id = :id")
|
||||
@@ -29,10 +37,10 @@ def _filter_clause(
|
||||
|
||||
|
||||
def get_playlist_entries(
|
||||
entry_id: Optional[int] = None,
|
||||
set_id: Optional[int] = None,
|
||||
session_id: Optional[int] = None,
|
||||
con: Optional[Connection] = None,
|
||||
entry_id: int | None = None,
|
||||
set_id: int | None = None,
|
||||
session_id: int | None = None,
|
||||
con: Connection | None = None,
|
||||
) -> Iterator[model.PlaylistEntry]:
|
||||
filter_clause, data = _filter_clause(entry_id=entry_id, set_id=set_id, session_id=session_id)
|
||||
query = f"""
|
||||
@@ -43,5 +51,18 @@ def get_playlist_entries(
|
||||
"""
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
return map(row_to_playlist_entry, cur.fetchall())
|
||||
_ = cur.execute(query, data)
|
||||
return map(conversion.row_to_playlist_entry, cur.fetchall())
|
||||
|
||||
|
||||
def get_tune_sessions(tema_ids: list[int], con: Connection | None = None) -> Iterator[Session]:
|
||||
query = """
|
||||
SELECT p.tema_id, s.id, s.date, s.start_time, s.end_time, s.venue_name, s.venue_url, s.is_live
|
||||
FROM playlists p JOIN sessions s ON p.session_id = s.id
|
||||
WHERE p.tema_id IN :tema_ids
|
||||
"""
|
||||
data = dict(tema_ids=tuple(tema_ids))
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
_ = cur.execute(query, data)
|
||||
return map(sessions_conversion.row_to_session, cur.fetchall())
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web.dal.sql import Connection, get_connection
|
||||
from folkugat_web.model import playlists as model
|
||||
|
||||
from ._conversion import playlist_entry_to_row, row_to_playlist_entry
|
||||
from . import conversion
|
||||
|
||||
|
||||
def insert_playlist_entry(pl_entry: model.PlaylistEntry, con: Optional[Connection] = None) -> model.PlaylistEntry:
|
||||
def insert_playlist_entry(pl_entry: model.PlaylistEntry, con: Connection | None = None) -> model.PlaylistEntry:
|
||||
query = """
|
||||
INSERT INTO playlists
|
||||
(id, session_id, set_id, tema_id)
|
||||
@@ -14,15 +12,15 @@ def insert_playlist_entry(pl_entry: model.PlaylistEntry, con: Optional[Connectio
|
||||
(:id, :session_id, :set_id, :tema_id)
|
||||
RETURNING *
|
||||
"""
|
||||
data = playlist_entry_to_row(pl_entry)
|
||||
data = conversion.playlist_entry_to_row(pl_entry)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
row = cur.fetchone()
|
||||
return row_to_playlist_entry(row)
|
||||
_ = cur.execute(query, data)
|
||||
row: conversion.PlaylistRowTuple = cur.fetchone()
|
||||
return conversion.row_to_playlist_entry(row)
|
||||
|
||||
|
||||
def update_playlist_entry(entry: model.PlaylistEntry, con: Optional[Connection] = None):
|
||||
def update_playlist_entry(entry: model.PlaylistEntry, con: Connection | None = None):
|
||||
query = """
|
||||
UPDATE playlists
|
||||
SET
|
||||
@@ -30,14 +28,14 @@ def update_playlist_entry(entry: model.PlaylistEntry, con: Optional[Connection]
|
||||
WHERE
|
||||
id = :id
|
||||
"""
|
||||
data = playlist_entry_to_row(entry)
|
||||
data = conversion.playlist_entry_to_row(entry)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
_ = cur.execute(query, data)
|
||||
return
|
||||
|
||||
|
||||
def delete_playlist_entry(entry_id: int, con: Optional[Connection] = None):
|
||||
def delete_playlist_entry(entry_id: int, con: Connection | None = None):
|
||||
query = """
|
||||
DELETE FROM playlists
|
||||
WHERE id = :id
|
||||
@@ -45,11 +43,11 @@ def delete_playlist_entry(entry_id: int, con: Optional[Connection] = None):
|
||||
data = dict(id=entry_id)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
_ = cur.execute(query, data)
|
||||
return
|
||||
|
||||
|
||||
def delete_playlist_set(session_id: int, set_id: int, con: Optional[Connection] = None):
|
||||
def delete_playlist_set(session_id: int, set_id: int, con: Connection | None = None):
|
||||
query = """
|
||||
DELETE FROM playlists
|
||||
WHERE session_id = :session_id AND set_id = :set_id
|
||||
@@ -57,5 +55,5 @@ def delete_playlist_set(session_id: int, set_id: int, con: Optional[Connection]
|
||||
data = dict(session_id=session_id, set_id=set_id)
|
||||
with get_connection(con) as con:
|
||||
cur = con.cursor()
|
||||
cur.execute(query, data)
|
||||
_ = cur.execute(query, data)
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user