Source code for codegrade.models.impersonate_by_id
"""The module that defines the ``ImpersonateById`` 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 ..utils import to_dict
from .base_impersonate_data import BaseImpersonateData
from .user_input_by_id import UserInputById
[docs]
@dataclass(kw_only=True)
class ImpersonateById(UserInputById, BaseImpersonateData):
"""Payload for impersonating a user identified by id."""
raw_data: t.Optional[t.Dict[str, t.Any]] = field(init=False, repr=False)
data_parser: t.ClassVar[t.Any] = rqa.Lazy(
lambda: UserInputById.data_parser.parser.combine(
BaseImpersonateData.data_parser.parser
).combine(rqa.FixedMapping())
)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"user_id": to_dict(self.user_id),
"by": to_dict(self.by),
"own_password": to_dict(self.own_password),
"confirm_session_termination": to_dict(
self.confirm_session_termination
),
"mode": to_dict(self.mode),
}
return res
@classmethod
def from_dict(
cls: t.Type[ImpersonateById], d: t.Dict[str, t.Any]
) -> ImpersonateById:
parsed = cls.data_parser.try_parse(d)
res = cls(
user_id=parsed.user_id,
by=parsed.by,
own_password=parsed.own_password,
confirm_session_termination=parsed.confirm_session_termination,
mode=parsed.mode,
)
res.raw_data = d
return res
import os
if os.getenv("CG_GENERATING_DOCS", "False").lower() in ("", "true"):
from .base_login_data import BaseLoginData