Added indexed lists and link edition in tunes
This commit is contained in:
@@ -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