Source code for codegrade.models.tenant_membership
"""The module that defines the ``TenantMembership`` 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 .abstract_role import AbstractRole
from .normal_user import NormalUser
[docs]
@dataclass(kw_only=True)
class TenantMembership:
"""A user and their role in a tenant."""
#: The user. Never carries an email address: an address is serialised only
#: for the account that owns it.
user: NormalUser
#: The role they have in the tenant.
tenant_role: AbstractRole
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(
"user",
parsers.ParserFor.make(NormalUser),
doc="The user. Never carries an email address: an address is serialised only for the account that owns it.",
),
rqa.RequiredArgument(
"tenant_role",
parsers.ParserFor.make(AbstractRole),
doc="The role they have in the tenant.",
),
)
)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"user": to_dict(self.user),
"tenant_role": to_dict(self.tenant_role),
}
return res
@classmethod
def from_dict(
cls: t.Type[TenantMembership], d: t.Dict[str, t.Any]
) -> TenantMembership:
parsed = cls.data_parser.try_parse(d)
res = cls(
user=parsed.user,
tenant_role=parsed.tenant_role,
)
res.raw_data = d
return res