Source code for codegrade.models.allow_registration_domain

"""The module that defines the ``AllowRegistrationDomain`` 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 .tenant_role_with_tenant import TenantRoleWithTenant


[docs] @dataclass class AllowRegistrationDomain: """An ALLOW registration domain rule that maps a domain to a tenant role.""" #: Always `'allow'`. type: t.Literal["allow"] #: The id of the rule. id: str #: The email domain. domain: str #: Whether subdomains are also matched. match_subdomains: bool #: The tenant role this rule maps to. tenant_role: TenantRoleWithTenant 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="Always `'allow'`.", ), rqa.RequiredArgument( "id", rqa.SimpleValue.str, doc="The id of the rule.", ), rqa.RequiredArgument( "domain", rqa.SimpleValue.str, doc="The email domain.", ), rqa.RequiredArgument( "match_subdomains", rqa.SimpleValue.bool, doc="Whether subdomains are also matched.", ), rqa.RequiredArgument( "tenant_role", parsers.ParserFor.make(TenantRoleWithTenant), doc="The tenant role this rule maps to.", ), ).use_readable_describe(True) ) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = { "type": to_dict(self.type), "id": to_dict(self.id), "domain": to_dict(self.domain), "match_subdomains": to_dict(self.match_subdomains), "tenant_role": to_dict(self.tenant_role), } return res @classmethod def from_dict( cls: t.Type[AllowRegistrationDomain], d: t.Dict[str, t.Any] ) -> AllowRegistrationDomain: parsed = cls.data_parser.try_parse(d) res = cls( type=parsed.type, id=parsed.id, domain=parsed.domain, match_subdomains=parsed.match_subdomains, tenant_role=parsed.tenant_role, ) res.raw_data = d return res