Source code for codegrade.models.patch_restriction_data

"""The module that defines the ``PatchRestrictionData`` 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 cg_maybe import Maybe, Nothing
from cg_maybe.utils import maybe_from_nullable

from .. import parsers
from ..utils import to_dict
from .clear_exam_environment_data import ClearExamEnvironmentData
from .clear_password_data import ClearPasswordData
from .clear_session_lockdown_data import ClearSessionLockdownData
from .set_password_data import SetPasswordData
from .set_schoolyear_exam_environment_data import (
    SetSchoolyearExamEnvironmentData,
)
from .set_session_lockdown_data import SetSessionLockdownData


[docs] @dataclass(kw_only=True) class PatchRestrictionData: """Input data required for the `Restriction::Patch` operation.""" #: Password settings password: Maybe[t.Union[SetPasswordData, ClearPasswordData]] = Nothing #: Session lockdown settings session_lockdown: Maybe[ t.Union[SetSessionLockdownData, ClearSessionLockdownData] ] = Nothing #: Exam environment provider settings. Attaching provisions the exam at the #: provider (and requires a configured entry window); detaching archives it #: at the provider while retaining its state, so re-attaching revives the #: same provider-side exam. Provider state is committed atomically with the #: rest of the patch: a provider failure fails the whole request. exam_environment: Maybe[ t.Union[SetSchoolyearExamEnvironmentData, ClearExamEnvironmentData] ] = Nothing 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.OptionalArgument( "password", parsers.make_union( parsers.ParserFor.make(SetPasswordData), parsers.ParserFor.make(ClearPasswordData), ), doc="Password settings", ), rqa.OptionalArgument( "session_lockdown", parsers.make_union( parsers.ParserFor.make(SetSessionLockdownData), parsers.ParserFor.make(ClearSessionLockdownData), ), doc="Session lockdown settings", ), rqa.OptionalArgument( "exam_environment", parsers.make_union( parsers.ParserFor.make(SetSchoolyearExamEnvironmentData), parsers.ParserFor.make(ClearExamEnvironmentData), ), doc="Exam environment provider settings. Attaching provisions the exam at the provider (and requires a configured entry window); detaching archives it at the provider while retaining its state, so re-attaching revives the same provider-side exam. Provider state is committed atomically with the rest of the patch: a provider failure fails the whole request.", ), ) ) def __post_init__(self) -> None: getattr(super(), "__post_init__", lambda: None)() self.password = maybe_from_nullable(self.password) self.session_lockdown = maybe_from_nullable(self.session_lockdown) self.exam_environment = maybe_from_nullable(self.exam_environment) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = {} if self.password.is_just: res["password"] = to_dict(self.password.value) if self.session_lockdown.is_just: res["session_lockdown"] = to_dict(self.session_lockdown.value) if self.exam_environment.is_just: res["exam_environment"] = to_dict(self.exam_environment.value) return res @classmethod def from_dict( cls: t.Type[PatchRestrictionData], d: t.Dict[str, t.Any] ) -> PatchRestrictionData: parsed = cls.data_parser.try_parse(d) res = cls( password=parsed.password, session_lockdown=parsed.session_lockdown, exam_environment=parsed.exam_environment, ) res.raw_data = d return res