Source code for codegrade.models.entry_window

"""The module that defines the ``EntryWindow`` model.

SPDX-License-Identifier: AGPL-3.0-only OR BSD-3-Clause-Clear
"""

from __future__ import annotations

import datetime
import typing as t
from dataclasses import dataclass, field

import cg_request_args as rqa

from ..utils import to_dict


[docs] @dataclass(kw_only=True) class EntryWindow: """The JSON representation of an EntryWindow.""" #: When the user may start entering. starts_at: datetime.datetime #: When the user may no longer enter. ends_at: datetime.datetime raw_data: t.Optional[t.Dict[str, t.Any]] = field(init=False, repr=False) data_parser: t.ClassVar[t.Any] = rqa.Lazy( lambda: rqa.FixedMapping( rqa.RequiredArgument( "starts_at", rqa.RichValue.DateTime, doc="When the user may start entering.", ), rqa.RequiredArgument( "ends_at", rqa.RichValue.DateTime, doc="When the user may no longer enter.", ), ) ) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = { "starts_at": to_dict(self.starts_at), "ends_at": to_dict(self.ends_at), } return res @classmethod def from_dict( cls: t.Type[EntryWindow], d: t.Dict[str, t.Any] ) -> EntryWindow: parsed = cls.data_parser.try_parse(d) res = cls( starts_at=parsed.starts_at, ends_at=parsed.ends_at, ) res.raw_data = d return res