Source code for codegrade.models.attest_defense_settings_sections

"""The module that defines the ``AttestDefenseSettingsSections`` 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 .attest_defense_example_question import AttestDefenseExampleQuestion
from .attest_defense_policy import AttestDefensePolicy
from .attest_defense_question_frame import AttestDefenseQuestionFrame
from .attest_defense_timing import AttestDefenseTiming


[docs] @dataclass(kw_only=True) class AttestDefenseSettingsSections: """The settings sections of an Attest Defense assignment. A stub serves the sections, so all four are absent while the Attest stubs are off. """ #: The AI policy. policy: Maybe[AttestDefensePolicy] = Nothing #: How the defense frames its questions. question_frame: Maybe[AttestDefenseQuestionFrame] = Nothing #: The questions the teacher saved as examples, in the order of the #: teacher. This is `None` for a reader who may not edit the assignment, so #: that a student cannot prepare the defense from the examples. example_questions: Maybe[ t.Optional[t.Sequence[AttestDefenseExampleQuestion]] ] = Nothing #: The timing of the defense. defense: Maybe[AttestDefenseTiming] = 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( "policy", parsers.ParserFor.make(AttestDefensePolicy), doc="The AI policy.", ), rqa.OptionalArgument( "question_frame", parsers.ParserFor.make(AttestDefenseQuestionFrame), doc="How the defense frames its questions.", ), rqa.OptionalArgument( "example_questions", rqa.Nullable( rqa.List( parsers.ParserFor.make(AttestDefenseExampleQuestion) ) ), doc="The questions the teacher saved as examples, in the order of the teacher. This is `None` for a reader who may not edit the assignment, so that a student cannot prepare the defense from the examples.", ), rqa.OptionalArgument( "defense", parsers.ParserFor.make(AttestDefenseTiming), doc="The timing of the defense.", ), ) ) def __post_init__(self) -> None: getattr(super(), "__post_init__", lambda: None)() self.policy = maybe_from_nullable(self.policy) self.question_frame = maybe_from_nullable(self.question_frame) self.example_questions = maybe_from_nullable(self.example_questions) self.defense = maybe_from_nullable(self.defense) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = {} if self.policy.is_just: res["policy"] = to_dict(self.policy.value) if self.question_frame.is_just: res["question_frame"] = to_dict(self.question_frame.value) if self.example_questions.is_just: res["example_questions"] = to_dict(self.example_questions.value) if self.defense.is_just: res["defense"] = to_dict(self.defense.value) return res @classmethod def from_dict( cls: t.Type[AttestDefenseSettingsSections], d: t.Dict[str, t.Any] ) -> AttestDefenseSettingsSections: parsed = cls.data_parser.try_parse(d) res = cls( policy=parsed.policy, question_frame=parsed.question_frame, example_questions=parsed.example_questions, defense=parsed.defense, ) res.raw_data = d return res