Migrated links, lyrics and properties

This commit is contained in:
marc
2025-03-23 21:46:04 +01:00
parent c097811e40
commit d596861a2e
47 changed files with 1403 additions and 844 deletions

View File

@@ -1,3 +0,0 @@
from ._base import IndexedList
__all__ = ["IndexedList"]

View File

@@ -1,39 +0,0 @@
import dataclasses
from typing import TypeVar
from typing_extensions import override
@dataclasses.dataclass
class WithId:
id: int | None
T = TypeVar("T", bound=WithId)
class IndexedList(list[T]):
@override
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)

View File

@@ -1,14 +1,11 @@
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"
@@ -26,31 +23,14 @@ class LinkType(enum.Enum):
@dataclasses.dataclass
class Link(WithId):
class Link:
id: int | None
tema_id: int
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"
@@ -60,46 +40,20 @@ class PropertyField(enum.Enum):
@dataclasses.dataclass
class Property(WithId):
class Property:
id: int | None
tema_id: int
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):
class Lyrics:
id: int | None
tema_id: int
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:
@@ -112,12 +66,11 @@ 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)
properties: list[Property] = dataclasses.field(default_factory=list)
links: list[Link] = dataclasses.field(default_factory=list)
lyrics: list[Lyrics] = dataclasses.field(default_factory=list)
# 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)
@@ -125,12 +78,8 @@ class Tema:
# 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
def ngrams(self) -> NGrams:
return ngrams.get_text_ngrams(self.title, *self.alternatives)
@staticmethod
def _is_score(link: Link) -> bool: