Files
folkugat-web/folkugat_web/model/temes.py
2025-03-23 00:19:33 +01:00

145 lines
3.4 KiB
Python

import dataclasses
import datetime
import enum
from typing import Self
from folkugat_web.model.search import NGrams
from folkugat_web.model.sessions import Session
from folkugat_web.services import ngrams
from ._base import IndexedList, WithId
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: LinkType | None
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) -> Self:
return cls(
id=d["id"],
title=d["title"],
content=d["content"],
)
@dataclasses.dataclass
class Stats:
times_played: int
sessions_played: list[Session]
@dataclasses.dataclass
class Tema:
id: int | None = 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)
# Stats
stats: Stats | None = None
def compute_ngrams(self):
self.ngrams = ngrams.get_text_ngrams(self.title, *self.alternatives)
def with_ngrams(self):
self.compute_ngrams()
return self
@staticmethod
def _is_score(link: Link) -> bool:
if link.content_type is not ContentType.PARTITURA:
return False
if link.link_type is LinkType.PDF:
return link.url.startswith("/")
return link.link_type is LinkType.IMAGE
def score(self) -> Link | None:
return next(filter(self._is_score, self.links), None)