Added indexed lists and link edition in tunes
This commit is contained in:
32
folkugat_web/services/temes/links.py
Normal file
32
folkugat_web/services/temes/links.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import re
|
||||
from typing import Optional
|
||||
|
||||
from folkugat_web.model import temes as model
|
||||
|
||||
IMAGE_FORMATS_RE = "|".join([
|
||||
'jpg', 'jpeg', 'png'
|
||||
])
|
||||
|
||||
LINK_RES = {
|
||||
model.LinkType.IMAGE: [
|
||||
re.compile(rf"^.*\.({IMAGE_FORMATS_RE})$")
|
||||
],
|
||||
model.LinkType.PDF: [
|
||||
re.compile(r"^.*\.pdf$")
|
||||
],
|
||||
model.LinkType.SPOTIFY: [
|
||||
re.compile(r"^.*spotify\.com.*$")
|
||||
],
|
||||
model.LinkType.YOUTUBE: [
|
||||
re.compile(r"^.*youtube\.com.*$"),
|
||||
re.compile(r"^.*youtu\.be.*$"),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def guess_link_type(url: str) -> Optional[model.LinkType]:
|
||||
for link_type, regexes in LINK_RES.items():
|
||||
for regex in regexes:
|
||||
if regex.match(url):
|
||||
return link_type
|
||||
return None
|
||||
@@ -20,29 +20,25 @@ def update_title(tema_id: int, title: str) -> model.Tema:
|
||||
return new_tema
|
||||
|
||||
|
||||
def update_lyric(tema_id: int, lyric_idx: int, lyric: model.Lyrics) -> model.Tema:
|
||||
def update_lyric(tema_id: int, lyric_id: int, lyric: model.Lyrics) -> model.Tema:
|
||||
with get_connection() as con:
|
||||
tema = temes_q.get_tema_by_id(tema_id=tema_id, con=con)
|
||||
if tema is None:
|
||||
raise ValueError(f"No tune found with tema_id = {tema_id}!")
|
||||
if lyric_idx > len(tema.lyrics):
|
||||
raise ValueError(f'Lyric index out of bounds')
|
||||
|
||||
tema.lyrics[lyric_idx] = lyric
|
||||
tema.lyrics.replace(lyric_id, lyric)
|
||||
temes_w.update_tema(tema=tema, con=con)
|
||||
|
||||
return tema
|
||||
|
||||
|
||||
def delete_lyric(tema_id: int, lyric_idx: int) -> model.Tema:
|
||||
def delete_lyric(tema_id: int, lyric_id: int) -> model.Tema:
|
||||
with get_connection() as con:
|
||||
tema = temes_q.get_tema_by_id(tema_id=tema_id, con=con)
|
||||
if tema is None:
|
||||
raise ValueError(f"No tune found with tema_id = {tema_id}!")
|
||||
if lyric_idx > len(tema.lyrics):
|
||||
raise ValueError(f'Lyric index out of bounds')
|
||||
|
||||
del tema.lyrics[lyric_idx]
|
||||
tema.lyrics.delete(lyric_id)
|
||||
temes_w.update_tema(tema=tema, con=con)
|
||||
|
||||
return tema
|
||||
@@ -54,9 +50,51 @@ def add_lyric(tema_id: int) -> model.Tema:
|
||||
if tema is None:
|
||||
raise ValueError(f"No tune found with tema_id = {tema_id}!")
|
||||
tema.lyrics.append(model.Lyrics(
|
||||
id=None,
|
||||
title=tema.title,
|
||||
content="",
|
||||
))
|
||||
temes_w.update_tema(tema=tema, con=con)
|
||||
|
||||
return tema
|
||||
|
||||
|
||||
def update_link(tema_id: int, link_id: int, link: model.Link) -> model.Tema:
|
||||
with get_connection() as con:
|
||||
tema = temes_q.get_tema_by_id(tema_id=tema_id, con=con)
|
||||
if tema is None:
|
||||
raise ValueError(f"No tune found with tema_id = {tema_id}!")
|
||||
|
||||
tema.links.replace(link_id, link)
|
||||
temes_w.update_tema(tema=tema, con=con)
|
||||
|
||||
return tema
|
||||
|
||||
|
||||
def delete_link(tema_id: int, link_id: int) -> model.Tema:
|
||||
with get_connection() as con:
|
||||
tema = temes_q.get_tema_by_id(tema_id=tema_id, con=con)
|
||||
if tema is None:
|
||||
raise ValueError(f"No tune found with tema_id = {tema_id}!")
|
||||
|
||||
tema.links.delete(link_id)
|
||||
temes_w.update_tema(tema=tema, con=con)
|
||||
|
||||
return tema
|
||||
|
||||
|
||||
def add_link(tema_id: int) -> model.Tema:
|
||||
with get_connection() as con:
|
||||
tema = temes_q.get_tema_by_id(tema_id=tema_id, con=con)
|
||||
if tema is None:
|
||||
raise ValueError(f"No tune found with tema_id = {tema_id}!")
|
||||
tema.links.append(model.Link(
|
||||
id=None,
|
||||
title="",
|
||||
url="",
|
||||
content_type=model.ContentType.OTHER,
|
||||
link_type=None,
|
||||
))
|
||||
temes_w.update_tema(tema=tema, con=con)
|
||||
|
||||
return tema
|
||||
|
||||
Reference in New Issue
Block a user