Source code for codegrade.models.patch_allow_registration_domain_data
"""The module that defines the ``PatchAllowRegistrationDomainData`` 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
class PatchAllowRegistrationDomainData:
""" """
#: The type of rule.
type: t.Literal["allow"]
#: The tenant role to assign.
tenant_role_id: int
#: The email domain to match.
domain: Maybe[str] = Nothing
#: Whether to also match subdomains.
match_subdomains: Maybe[bool] = 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(
"type",
rqa.StringEnum("allow"),
doc="The type of rule.",
),
rqa.RequiredArgument(
"tenant_role_id",
rqa.SimpleValue.int,
doc="The tenant role to assign.",
),
rqa.OptionalArgument(
"domain",
rqa.SimpleValue.str,
doc="The email domain to match.",
),
rqa.OptionalArgument(
"match_subdomains",
rqa.SimpleValue.bool,
doc="Whether to also match subdomains.",
),
).use_readable_describe(True)
)
def __post_init__(self) -> None:
getattr(super(), "__post_init__", lambda: None)()
self.domain = maybe_from_nullable(self.domain)
self.match_subdomains = maybe_from_nullable(self.match_subdomains)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"type": to_dict(self.type),
"tenant_role_id": to_dict(self.tenant_role_id),
}
if self.domain.is_just:
res["domain"] = to_dict(self.domain.value)
if self.match_subdomains.is_just:
res["match_subdomains"] = to_dict(self.match_subdomains.value)
return res
@classmethod
def from_dict(
cls: t.Type[PatchAllowRegistrationDomainData], d: t.Dict[str, t.Any]
) -> PatchAllowRegistrationDomainData:
parsed = cls.data_parser.try_parse(d)
res = cls(
type=parsed.type,
tenant_role_id=parsed.tenant_role_id,
domain=parsed.domain,
match_subdomains=parsed.match_subdomains,
)
res.raw_data = d
return res