Added indexed lists and link edition in tunes
This commit is contained in:
@@ -0,0 +1 @@
|
||||
from ._base import IndexedList
|
||||
|
||||
36
folkugat_web/model/_base.py
Normal file
36
folkugat_web/model/_base.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import dataclasses
|
||||
from typing import Optional, TypeVar
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class WithId:
|
||||
id: Optional[int]
|
||||
|
||||
|
||||
T = TypeVar("T", bound=WithId)
|
||||
|
||||
|
||||
class IndexedList(list[T]):
|
||||
def append(self, _item: T) -> None:
|
||||
if _item.id is None:
|
||||
_item.id = max((i.id or 0 for i in self), default=0) + 1
|
||||
return super().append(_item)
|
||||
|
||||
def find_idx(self, _id: int) -> int:
|
||||
try:
|
||||
i, _ = next(filter(lambda it: it[1].id == _id, enumerate(self)))
|
||||
except StopIteration:
|
||||
raise ValueError(f"Could not find item with id {_id}")
|
||||
return i
|
||||
|
||||
def get(self, _id: int) -> T:
|
||||
i = self.find_idx(_id)
|
||||
return self.__getitem__(i)
|
||||
|
||||
def delete(self, _id: int) -> None:
|
||||
i = self.find_idx(_id)
|
||||
return super().__delitem__(i)
|
||||
|
||||
def replace(self, _id: int, _obj: T) -> None:
|
||||
i = self.find_idx(_id)
|
||||
super().__setitem__(i, _obj)
|
||||
@@ -5,16 +5,18 @@ from typing import Optional
|
||||
|
||||
from folkugat_web.services import ngrams
|
||||
|
||||
from ._base import IndexedList, WithId
|
||||
|
||||
NGrams = dict[int, list[str]]
|
||||
|
||||
|
||||
class ContentType(enum.Enum):
|
||||
PARTITURA = "partitura"
|
||||
AUDIO = "àudio"
|
||||
OTHER = "enllaç"
|
||||
|
||||
|
||||
class LinkType(enum.Enum):
|
||||
SCORE = "score"
|
||||
AUDIO = "audio"
|
||||
OTHER = "other"
|
||||
|
||||
|
||||
class LinkSubtype(enum.Enum):
|
||||
# Score
|
||||
PDF = "pdf"
|
||||
IMAGE = "image"
|
||||
@@ -24,16 +26,17 @@ class LinkSubtype(enum.Enum):
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Link:
|
||||
type: LinkType
|
||||
subtype: Optional[LinkSubtype]
|
||||
class Link(WithId):
|
||||
content_type: ContentType
|
||||
link_type: Optional[LinkType]
|
||||
url: str
|
||||
title: str = ""
|
||||
|
||||
def to_dict(self):
|
||||
return dict(
|
||||
type=self.type.value,
|
||||
subtype=self.subtype.value if self.subtype else None,
|
||||
id=self.id,
|
||||
content_type=self.content_type.value,
|
||||
link_type=self.link_type.value if self.link_type else None,
|
||||
url=self.url,
|
||||
title=self.title,
|
||||
)
|
||||
@@ -41,8 +44,9 @@ class Link:
|
||||
@classmethod
|
||||
def from_dict(cls, d):
|
||||
return cls(
|
||||
type=LinkType(d["type"]),
|
||||
subtype=LinkSubtype(d["subtype"]) if d["subtype"] else None,
|
||||
id=d["id"],
|
||||
content_type=ContentType(d["content_type"]),
|
||||
link_type=LinkType(d["link_type"]) if d["link_type"] else None,
|
||||
url=d["url"],
|
||||
title=d["title"],
|
||||
)
|
||||
@@ -56,12 +60,13 @@ class PropertyField(enum.Enum):
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Property:
|
||||
class Property(WithId):
|
||||
field: PropertyField
|
||||
value: str
|
||||
|
||||
def to_dict(self):
|
||||
return dict(
|
||||
id=self.id,
|
||||
field=self.field.value,
|
||||
value=self.value,
|
||||
)
|
||||
@@ -69,18 +74,20 @@ class Property:
|
||||
@classmethod
|
||||
def from_dict(cls, d):
|
||||
return cls(
|
||||
id=d["id"],
|
||||
field=PropertyField(d["field"]),
|
||||
value=d["value"],
|
||||
)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
class Lyrics:
|
||||
class Lyrics(WithId):
|
||||
title: str
|
||||
content: str
|
||||
|
||||
def to_dict(self):
|
||||
return dict(
|
||||
id=self.id,
|
||||
title=self.title,
|
||||
content=self.content,
|
||||
)
|
||||
@@ -88,6 +95,7 @@ class Lyrics:
|
||||
@classmethod
|
||||
def from_dict(cls, d):
|
||||
return cls(
|
||||
id=d["id"],
|
||||
title=d["title"],
|
||||
content=d["content"],
|
||||
)
|
||||
@@ -98,9 +106,9 @@ class Tema:
|
||||
id: Optional[int] = None
|
||||
# Info
|
||||
title: str = ""
|
||||
properties: list[Property] = dataclasses.field(default_factory=list)
|
||||
links: list[Link] = dataclasses.field(default_factory=list)
|
||||
lyrics: list[Lyrics] = dataclasses.field(default_factory=list)
|
||||
properties: IndexedList[Property] = dataclasses.field(default_factory=IndexedList)
|
||||
links: IndexedList[Link] = dataclasses.field(default_factory=IndexedList)
|
||||
lyrics: IndexedList[Lyrics] = dataclasses.field(default_factory=IndexedList)
|
||||
# Search related
|
||||
alternatives: list[str] = dataclasses.field(default_factory=list)
|
||||
ngrams: NGrams = dataclasses.field(default_factory=dict)
|
||||
@@ -115,9 +123,3 @@ class Tema:
|
||||
def with_ngrams(self):
|
||||
self.compute_ngrams()
|
||||
return self
|
||||
|
||||
def scores(self):
|
||||
return [link for link in self.links if link.type is LinkType.SCORE]
|
||||
|
||||
def audios(self):
|
||||
return [link for link in self.links if link.type is LinkType.AUDIO]
|
||||
|
||||
Reference in New Issue
Block a user