Files
folkugat-web/folkugat_web/dal/sql/temes/conversion.py
2025-03-22 23:06:34 +01:00

57 lines
1.8 KiB
Python

import datetime
import json
from typing import TypedDict
from folkugat_web.model import IndexedList
from folkugat_web.model import search as search_model
from folkugat_web.model import temes as model
TemaRowTuple = tuple[int, str, str, str, str, str, str, str, str, int]
class TemaRowDict(TypedDict):
id: int | None
title: str
properties: str
links: str
lyrics: str
alternatives: str
ngrams: str
modification_date: str
creation_date: str
hidden: int
def tema_to_row(tema: model.Tema) -> TemaRowDict:
return {
'id': tema.id,
'title': tema.title,
'properties': json.dumps(list(map(lambda p: p.to_dict(), tema.properties))),
'links': json.dumps(list(map(lambda l: l.to_dict(), tema.links))),
'lyrics': json.dumps(list(map(lambda l: l.to_dict(), tema.lyrics))),
'alternatives': json.dumps(tema.alternatives),
'ngrams': json.dumps(tema.ngrams),
'modification_date': tema.modification_date.isoformat(),
'creation_date': tema.creation_date.isoformat(),
'hidden': 1 if tema.hidden else 0,
}
def cell_to_ngrams(cell: str) -> search_model.NGrams:
return {int(n): ngrams_ for n, ngrams_ in json.loads(cell).items()}
def row_to_tema(row: TemaRowTuple) -> model.Tema:
return model.Tema(
id=row[0],
title=row[1],
properties=IndexedList(map(model.Property.from_dict, json.loads(row[2]))),
links=IndexedList(map(model.Link.from_dict, json.loads(row[3]))),
lyrics=IndexedList(map(model.Lyrics.from_dict, json.loads(row[4]))),
alternatives=json.loads(row[5]),
ngrams=cell_to_ngrams(row[6]),
modification_date=datetime.datetime.fromisoformat(row[7]),
creation_date=datetime.datetime.fromisoformat(row[8]),
hidden=bool(row[9]),
)