Source code for codegrade.models.restriction_mixin

"""The module that defines the ``RestrictionMixin`` 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 .restriction_mixin_not_set_password import RestrictionMixinNotSetPassword
from .restriction_mixin_set_password import RestrictionMixinSetPassword


[docs] @dataclass class RestrictionMixin: """Base JSON representation shared by all restriction types.""" #: Whether a password is set (tag: 'set') or not (tag: 'not-set'). password: t.Union[ RestrictionMixinSetPassword, RestrictionMixinNotSetPassword ] 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( "password", parsers.make_union( parsers.ParserFor.make(RestrictionMixinSetPassword), parsers.ParserFor.make(RestrictionMixinNotSetPassword), ), doc="Whether a password is set (tag: 'set') or not (tag: 'not-set').", ), ).use_readable_describe(True) ) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = { "password": to_dict(self.password), } return res @classmethod def from_dict( cls: t.Type[RestrictionMixin], d: t.Dict[str, t.Any] ) -> RestrictionMixin: parsed = cls.data_parser.try_parse(d) res = cls( password=parsed.password, ) res.raw_data = d return res