40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
import datetime
|
|
import json
|
|
|
|
from folkugat_web.model import IndexedList
|
|
from folkugat_web.model import temes as model
|
|
|
|
|
|
def tema_to_row(tema: model.Tema) -> dict:
|
|
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) -> model.NGrams:
|
|
return {int(n): ngrams_ for n, ngrams_ in json.loads(cell).items()}
|
|
|
|
|
|
def row_to_tema(row: tuple) -> 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]),
|
|
)
|