import datetime import json from typing import TypedDict from folkugat_web.model import search as search_model from folkugat_web.model import temes as model TemaRowTuple = tuple[int, str, str, str, str, int] class TemaRowDict(TypedDict): id: int | None title: str alternatives: str modification_date: str creation_date: str hidden: int def tema_to_row(tema: model.Tema) -> TemaRowDict: return { 'id': tema.id, 'title': tema.title, 'alternatives': json.dumps(tema.alternatives), '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: str) -> search_model.NGrams: cell: dict[str, list[str]] = json.loads(cell_str) return {int(n): ngrams_ for n, ngrams_ in cell.items()} def row_to_tema(row: TemaRowTuple) -> model.Tema: return model.Tema( id=row[0], title=row[1], alternatives=json.loads(row[2]), modification_date=datetime.datetime.fromisoformat(row[3]), creation_date=datetime.datetime.fromisoformat(row[4]), hidden=bool(row[5]), )