Source code for codegrade.models.create_email_change_data

"""The module that defines the ``CreateEmailChangeData`` 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 CreateEmailChangeData: """Input data required for the `Email Change::Create` operation.""" #: The new email address. new_email: str #: The current password (required if the user has one). old_password: Maybe[str] = 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( "new_email", rqa.SimpleValue.str, doc="The new email address.", ), rqa.OptionalArgument( "old_password", rqa.SimpleValue.str, doc="The current password (required if the user has one).", ), ).use_readable_describe(True) ) def __post_init__(self) -> None: getattr(super(), "__post_init__", lambda: None)() self.old_password = maybe_from_nullable(self.old_password) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = { "new_email": to_dict(self.new_email), } if self.old_password.is_just: res["old_password"] = to_dict(self.old_password.value) return res @classmethod def from_dict( cls: t.Type[CreateEmailChangeData], d: t.Dict[str, t.Any] ) -> CreateEmailChangeData: parsed = cls.data_parser.try_parse(d) res = cls( new_email=parsed.new_email, old_password=parsed.old_password, ) res.raw_data = d return res