Source code for codegrade.models.session_restriction_context

"""The module that defines the ``SessionRestrictionContext`` 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 SessionRestrictionContext: """The context to which this session is restricted, currently only course contexts are supported. """ #: The id of the course to which this session is restricted. course_id: int #: Whether this is an isolated exam session. Defaults to `False` for #: backward compatibility with existing login-link sessions. isolated: 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( "course_id", rqa.SimpleValue.int, doc="The id of the course to which this session is restricted.", ), rqa.OptionalArgument( "isolated", rqa.SimpleValue.bool, doc="Whether this is an isolated exam session. Defaults to `False` for backward compatibility with existing login-link sessions.", ), ).use_readable_describe(True) ) def __post_init__(self) -> None: getattr(super(), "__post_init__", lambda: None)() self.isolated = maybe_from_nullable(self.isolated) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = { "course_id": to_dict(self.course_id), } if self.isolated.is_just: res["isolated"] = to_dict(self.isolated.value) return res @classmethod def from_dict( cls: t.Type[SessionRestrictionContext], d: t.Dict[str, t.Any] ) -> SessionRestrictionContext: parsed = cls.data_parser.try_parse(d) res = cls( course_id=parsed.course_id, isolated=parsed.isolated, ) res.raw_data = d return res