Source code for codegrade.models.restrict_token_data

"""The module that defines the ``RestrictTokenData`` 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 .. import parsers
from ..utils import to_dict
from .removed_permissions import RemovedPermissions
from .session_restriction_context import SessionRestrictionContext


[docs] @dataclass(kw_only=True) class RestrictTokenData: """The restrictions a caller may add to their own token. Only fields that make a token strictly more restrictive are accepted. Access-granting fields such as `verified_course_ids` are absent by design, so entry to password- or lockdown-restricted content can never be self- granted through this route. """ #: Narrow the session to a single course context. for_context: Maybe[SessionRestrictionContext] = Nothing #: Course and tenant permissions to drop from this session. removed_permissions: Maybe[RemovedPermissions] = 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.OptionalArgument( "for_context", parsers.ParserFor.make(SessionRestrictionContext), doc="Narrow the session to a single course context.", ), rqa.OptionalArgument( "removed_permissions", parsers.ParserFor.make(RemovedPermissions), doc="Course and tenant permissions to drop from this session.", ), ) ) def __post_init__(self) -> None: getattr(super(), "__post_init__", lambda: None)() self.for_context = maybe_from_nullable(self.for_context) self.removed_permissions = maybe_from_nullable( self.removed_permissions ) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = {} if self.for_context.is_just: res["for_context"] = to_dict(self.for_context.value) if self.removed_permissions.is_just: res["removed_permissions"] = to_dict( self.removed_permissions.value ) return res @classmethod def from_dict( cls: t.Type[RestrictTokenData], d: t.Dict[str, t.Any] ) -> RestrictTokenData: parsed = cls.data_parser.try_parse(d) res = cls( for_context=parsed.for_context, removed_permissions=parsed.removed_permissions, ) res.raw_data = d return res