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_password_data import ClearPasswordData
from .clear_session_lockdown_data import ClearSessionLockdownData
from .set_password_data import SetPasswordData
from .set_session_lockdown_data import SetSessionLockdownData


[docs] @dataclass 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 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", ), ).use_readable_describe(True) ) 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) 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) 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, ) res.raw_data = d return res