From 75e78764c6b9c8e3167cc2eef5e55ef47aa87834 Mon Sep 17 00:00:00 2001 From: Nora-Olivia-Ammann <103038637+Nora-Olivia-Ammann@users.noreply.github.com> Date: Fri, 15 Nov 2024 09:47:44 +0100 Subject: [PATCH] create date serialisation --- .../models/serialise/serialise_value.py | 28 +++++++++++++++++++ src/dsp_tools/utils/date_util.py | 12 ++++++++ 2 files changed, 40 insertions(+) diff --git a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py index 77309b1e39..66f1244ac1 100644 --- a/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py +++ b/src/dsp_tools/commands/xmlupload/models/serialise/serialise_value.py @@ -6,6 +6,11 @@ from dataclasses import dataclass from typing import Any +from dsp_tools.utils.date_util import Date +from dsp_tools.utils.date_util import DayMonthYearEra +from dsp_tools.utils.date_util import SingleDate +from dsp_tools.utils.date_util import StartEnd + @dataclass(frozen=True) class SerialiseProperty: @@ -48,6 +53,29 @@ def serialise(self) -> dict[str, Any]: return serialised +class SerialiseDateValue(SerialiseValue): + """A DateValue to be serialised.""" + + value: Date + + def serialise(self) -> dict[str, Any]: + serialised = self._get_one_date_dict(self.value.start, StartEnd.START) + + + def _get_one_date_dict(self, date: SingleDate, start_end: StartEnd) -> dict[str, str]: + def get_prop(precision: DayMonthYearEra) -> str: + return f"knora-api:dateValueHas{start_end!s}{precision!s}" + + date_dict = {get_prop(DayMonthYearEra.YEAR): date.year} if date.year else {} + if date.month: + date_dict[get_prop(DayMonthYearEra.MONTH)] = date.month + if date.day: + date_dict[get_prop(DayMonthYearEra.DAY)] = date.day + if date.era: + date_dict[get_prop(DayMonthYearEra.ERA)] = date.era + return date_dict + + class SerialiseDecimal(SerialiseValue): """A DecimalValue to be serialised.""" diff --git a/src/dsp_tools/utils/date_util.py b/src/dsp_tools/utils/date_util.py index a90bbda4a2..8c54806875 100644 --- a/src/dsp_tools/utils/date_util.py +++ b/src/dsp_tools/utils/date_util.py @@ -70,6 +70,18 @@ def from_string(s: str) -> Era: raise BaseError(f"Invalid era type: {s}") +class DayMonthYearEra(Enum): + DAY = "Day" + MONTH = "Month" + YEAR = "Year" + ERA = "Era" + + +class StartEnd(Enum): + START = "Start" + END = "End" + + @dataclass(frozen=True) class SingleDate: """Information about a single date."""