126 lines
2.9 KiB
Python
126 lines
2.9 KiB
Python
import dataclasses
|
|
import datetime
|
|
import enum
|
|
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
|
|
PDF = "pdf"
|
|
IMAGE = "image"
|
|
# Audio
|
|
SPOTIFY = "spotify"
|
|
YOUTUBE = "youtube"
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Link(WithId):
|
|
content_type: ContentType
|
|
link_type: Optional[LinkType]
|
|
url: str
|
|
title: str = ""
|
|
|
|
def to_dict(self):
|
|
return dict(
|
|
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,
|
|
)
|
|
|
|
@classmethod
|
|
def from_dict(cls, d):
|
|
return cls(
|
|
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"],
|
|
)
|
|
|
|
|
|
class PropertyField(enum.Enum):
|
|
AUTOR = "autor"
|
|
TIPUS = "tipus"
|
|
COMPAS = "compàs"
|
|
ORIGEN = "orígen"
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Property(WithId):
|
|
field: PropertyField
|
|
value: str
|
|
|
|
def to_dict(self):
|
|
return dict(
|
|
id=self.id,
|
|
field=self.field.value,
|
|
value=self.value,
|
|
)
|
|
|
|
@classmethod
|
|
def from_dict(cls, d):
|
|
return cls(
|
|
id=d["id"],
|
|
field=PropertyField(d["field"]),
|
|
value=d["value"],
|
|
)
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Lyrics(WithId):
|
|
title: str
|
|
content: str
|
|
|
|
def to_dict(self):
|
|
return dict(
|
|
id=self.id,
|
|
title=self.title,
|
|
content=self.content,
|
|
)
|
|
|
|
@classmethod
|
|
def from_dict(cls, d):
|
|
return cls(
|
|
id=d["id"],
|
|
title=d["title"],
|
|
content=d["content"],
|
|
)
|
|
|
|
|
|
@dataclasses.dataclass
|
|
class Tema:
|
|
id: Optional[int] = None
|
|
# Info
|
|
title: str = ""
|
|
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)
|
|
hidden: bool = True
|
|
# Other info
|
|
modification_date: datetime.datetime = dataclasses.field(default_factory=datetime.datetime.now)
|
|
creation_date: datetime.datetime = dataclasses.field(default_factory=datetime.datetime.now)
|
|
|
|
def compute_ngrams(self):
|
|
self.ngrams = ngrams.get_text_ngrams(self.title, *self.alternatives)
|
|
|
|
def with_ngrams(self):
|
|
self.compute_ngrams()
|
|
return self
|