Source code for codegrade.models.restriction

"""The module that defines the ``Restriction`` 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 import RestrictionMixin
from .restriction_not_set_session_lockdown import (
    RestrictionNotSetSessionLockdown,
)
from .restriction_set_session_lockdown import RestrictionSetSessionLockdown


[docs] @dataclass class Restriction(RestrictionMixin): """The JSON representation of a Restriction.""" #: The unique identifier for this restriction. id: str #: Session lockdown settings, or not-set if disabled. session_lockdown: t.Union[ RestrictionSetSessionLockdown, RestrictionNotSetSessionLockdown ] raw_data: t.Optional[t.Dict[str, t.Any]] = field(init=False, repr=False) data_parser: t.ClassVar[t.Any] = rqa.Lazy( lambda: RestrictionMixin.data_parser.parser.combine( rqa.FixedMapping( rqa.RequiredArgument( "id", rqa.SimpleValue.str, doc="The unique identifier for this restriction.", ), rqa.RequiredArgument( "session_lockdown", parsers.make_union( parsers.ParserFor.make(RestrictionSetSessionLockdown), parsers.ParserFor.make( RestrictionNotSetSessionLockdown ), ), doc="Session lockdown settings, or not-set if disabled.", ), ) ).use_readable_describe(True) ) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = { "id": to_dict(self.id), "session_lockdown": to_dict(self.session_lockdown), "password": to_dict(self.password), } return res @classmethod def from_dict( cls: t.Type[Restriction], d: t.Dict[str, t.Any] ) -> Restriction: parsed = cls.data_parser.try_parse(d) res = cls( id=parsed.id, session_lockdown=parsed.session_lockdown, password=parsed.password, ) res.raw_data = d return res
import os if os.getenv("CG_GENERATING_DOCS", "False").lower() in ("", "true"): from .restriction_mixin_not_set_password import ( RestrictionMixinNotSetPassword, ) from .restriction_mixin_set_password import RestrictionMixinSetPassword