Source code for codegrade.models.change_user_role_data
"""The module that defines the ``ChangeUserRoleData`` 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 ..utils import to_dict
[docs]
@dataclass(kw_only=True)
class ChangeUserRoleData:
"""Input data required for changing the tenant role of a user."""
#: The id of the new tenant role for the user.
role_id: int
#: The email address of the user whose role is changed. Required when the
#: new role grants tenant wide administrative permissions, ignored
#: otherwise.
confirm_email: Maybe[str] = 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.RequiredArgument(
"role_id",
rqa.SimpleValue.int,
doc="The id of the new tenant role for the user.",
),
rqa.OptionalArgument(
"confirm_email",
rqa.SimpleValue.str,
doc="The email address of the user whose role is changed. Required when the new role grants tenant wide administrative permissions, ignored otherwise.",
),
)
)
def __post_init__(self) -> None:
getattr(super(), "__post_init__", lambda: None)()
self.confirm_email = maybe_from_nullable(self.confirm_email)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"role_id": to_dict(self.role_id),
}
if self.confirm_email.is_just:
res["confirm_email"] = to_dict(self.confirm_email.value)
return res
@classmethod
def from_dict(
cls: t.Type[ChangeUserRoleData], d: t.Dict[str, t.Any]
) -> ChangeUserRoleData:
parsed = cls.data_parser.try_parse(d)
res = cls(
role_id=parsed.role_id,
confirm_email=parsed.confirm_email,
)
res.raw_data = d
return res