Source code for codegrade.models.user_input_by_id
"""The module that defines the ``UserInputById`` 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 ..utils import to_dict
[docs]
@dataclass(kw_only=True)
class UserInputById:
"""Reference to a user by id."""
#: The id of the user.
user_id: int
#: Discriminator selecting the by-id variant. Optional on the wire;
#: defaults server-side to `'id'`.
by: t.Literal["id"] = "id"
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(
"user_id",
rqa.SimpleValue.int,
doc="The id of the user.",
),
rqa.DefaultArgument(
"by",
rqa.StringEnum("id"),
doc="Discriminator selecting the by-id variant. Optional on the wire; defaults server-side to `'id'`.",
default=lambda: "id",
),
)
)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"user_id": to_dict(self.user_id),
"by": to_dict(self.by),
}
return res
@classmethod
def from_dict(
cls: t.Type[UserInputById], d: t.Dict[str, t.Any]
) -> UserInputById:
parsed = cls.data_parser.try_parse(d)
res = cls(
user_id=parsed.user_id,
by=parsed.by,
)
res.raw_data = d
return res