Source code for codegrade.models.extended_user

"""The module that defines the ``ExtendedUser`` 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 .. import parsers
from ..utils import to_dict
from .merged_global_tenant_perm_map import MergedGlobalTenantPermMap
from .normal_user import NormalUser


[docs] @dataclass class ExtendedUser(NormalUser): """The extended JSON representation of a user.""" #: The email of the user. This will only be provided for the currently #: logged in user. email: t.Optional[str] #: Whether this user's email address has been verified. `None` when the #: requesting user is not the owner of this account (i.e. only the logged- #: in user's own record includes this). email_verified: t.Optional[bool] #: Can this user see hidden assignments at least in one course. hidden: bool #: Whether this user's identity is linked to an LTI provider. When `True`, #: the name and email are managed by the LMS and changes made locally will #: be overwritten on the next sync. is_managed_by_lti: bool #: Whether this user's identity is linked to a SAML provider. When `True`, #: the name and email are managed by the SSO IdP and changes made locally #: will be overwritten on the next sync. is_managed_by_saml: bool #: The global and tenant permissions of the user. permissions: MergedGlobalTenantPermMap raw_data: t.Optional[t.Dict[str, t.Any]] = field(init=False, repr=False) data_parser: t.ClassVar[t.Any] = rqa.Lazy( lambda: NormalUser.data_parser.parser.combine( rqa.FixedMapping( rqa.RequiredArgument( "email", rqa.Nullable(rqa.SimpleValue.str), doc="The email of the user. This will only be provided for the currently logged in user.", ), rqa.RequiredArgument( "email_verified", rqa.Nullable(rqa.SimpleValue.bool), doc="Whether this user's email address has been verified. `None` when the requesting user is not the owner of this account (i.e. only the logged-in user's own record includes this).", ), rqa.RequiredArgument( "hidden", rqa.SimpleValue.bool, doc="Can this user see hidden assignments at least in one course.", ), rqa.RequiredArgument( "is_managed_by_lti", rqa.SimpleValue.bool, doc="Whether this user's identity is linked to an LTI provider. When `True`, the name and email are managed by the LMS and changes made locally will be overwritten on the next sync.", ), rqa.RequiredArgument( "is_managed_by_saml", rqa.SimpleValue.bool, doc="Whether this user's identity is linked to a SAML provider. When `True`, the name and email are managed by the SSO IdP and changes made locally will be overwritten on the next sync.", ), rqa.RequiredArgument( "permissions", parsers.ParserFor.make(MergedGlobalTenantPermMap), doc="The global and tenant permissions of the user.", ), ) ).use_readable_describe(True) ) def to_dict(self) -> t.Dict[str, t.Any]: res: t.Dict[str, t.Any] = { "email": to_dict(self.email), "email_verified": to_dict(self.email_verified), "hidden": to_dict(self.hidden), "is_managed_by_lti": to_dict(self.is_managed_by_lti), "is_managed_by_saml": to_dict(self.is_managed_by_saml), "permissions": to_dict(self.permissions), "type": to_dict(self.type), "name": to_dict(self.name), "is_test_student": to_dict(self.is_test_student), "id": to_dict(self.id), "username": to_dict(self.username), "tenant_id": to_dict(self.tenant_id), } return res @classmethod def from_dict( cls: t.Type[ExtendedUser], d: t.Dict[str, t.Any] ) -> ExtendedUser: parsed = cls.data_parser.try_parse(d) res = cls( email=parsed.email, email_verified=parsed.email_verified, hidden=parsed.hidden, is_managed_by_lti=parsed.is_managed_by_lti, is_managed_by_saml=parsed.is_managed_by_saml, permissions=parsed.permissions, type=parsed.type, name=parsed.name, is_test_student=parsed.is_test_student, id=parsed.id, username=parsed.username, tenant_id=parsed.tenant_id, ) res.raw_data = d return res
import os if os.getenv("CG_GENERATING_DOCS", "False").lower() in ("", "true"): from .base_user import BaseUser