Source code for codegrade.models.restriction_exam_environment

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

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

from __future__ import annotations

import typing as t
from dataclasses import dataclass, field

import cg_request_args as rqa

from .. import parsers
from ..utils import to_dict
from .restriction_exam_environment_artifact_preparation import (
    RestrictionExamEnvironmentArtifactPreparation,
)
from .restriction_exam_environment_no_preparation import (
    RestrictionExamEnvironmentNoPreparation,
)
from .restriction_exam_environment_redirect_preparation import (
    RestrictionExamEnvironmentRedirectPreparation,
)


[docs] @dataclass(kw_only=True) class RestrictionExamEnvironment: """The JSON representation of a RestrictionExamEnvironment.""" #: The exam environment the viewer must prepare for before entering, #: carrying the moment from which preparing is allowed. preparation: t.Union[ RestrictionExamEnvironmentNoPreparation, RestrictionExamEnvironmentRedirectPreparation, RestrictionExamEnvironmentArtifactPreparation, ] 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( "preparation", parsers.make_union( parsers.ParserFor.make( RestrictionExamEnvironmentNoPreparation ), parsers.ParserFor.make( RestrictionExamEnvironmentRedirectPreparation ), parsers.ParserFor.make( RestrictionExamEnvironmentArtifactPreparation ), ), doc="The exam environment the viewer must prepare for before entering, carrying the moment from which preparing is allowed.", ), ) ) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = { "preparation": to_dict(self.preparation), } return res @classmethod def from_dict( cls: t.Type[RestrictionExamEnvironment], d: t.Dict[str, t.Any] ) -> RestrictionExamEnvironment: parsed = cls.data_parser.try_parse(d) res = cls( preparation=parsed.preparation, ) res.raw_data = d return res