Coverage for src/kwai/api/v1/auth/schemas/user_account.py: 100%
27 statements
« prev ^ index » next coverage.py v7.7.1, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2024-01-01 00:00 +0000
1"""Schemas for a user account resource."""
3from typing import Self
5from pydantic import BaseModel
7from kwai.api.schemas.resources import UserInvitationResourceIdentifier
8from kwai.api.v1.auth.schemas.resources import UserAccountResourceIdentifier
9from kwai.core.json_api import (
10 MultipleDocument,
11 Relationship,
12 ResourceData,
13 SingleDocument,
14)
15from kwai.modules.identity.users.user_account import UserAccountEntity
18class BaseUserAccountAttributes(BaseModel):
19 """Base attributes for a user account JSON:API resource."""
21 email: str
22 first_name: str
23 last_name: str
24 remark: str
27class UserAccountAttributes(BaseUserAccountAttributes):
28 """Attributes of a user account JSON:API resource."""
30 last_login: str | None
31 last_unsuccessful_login: str | None
32 revoked: bool
33 admin: bool
36class CreateUserAccountAttributes(BaseUserAccountAttributes):
37 """Attributes for creating a user account."""
39 password: str
42class UserAccountResource(
43 UserAccountResourceIdentifier, ResourceData[UserAccountAttributes, None]
44):
45 """A JSON:API resource for a user account."""
47 @classmethod
48 def create(cls, user_account: UserAccountEntity) -> Self:
49 """Create a document for a user account."""
50 return cls(
51 id=str(user_account.user.uuid),
52 attributes=UserAccountAttributes(
53 email=str(user_account.user.email),
54 last_login=None
55 if user_account.last_login.empty
56 else str(user_account.last_login),
57 last_unsuccessful_login=None
58 if user_account.last_unsuccessful_login.empty
59 else str(user_account.last_unsuccessful_login),
60 revoked=user_account.revoked,
61 admin=user_account.admin,
62 first_name=user_account.user.name.first_name,
63 last_name=user_account.user.name.last_name,
64 remark=user_account.user.remark,
65 ),
66 )
69UserAccountDocument = SingleDocument[UserAccountResource, None]
70UserAccountsDocument = MultipleDocument[UserAccountResource, None]
73class CreateUserAccountRelationships(BaseModel):
74 """Relationships needed for creating a user account."""
76 user_invitation: Relationship[UserInvitationResourceIdentifier]
79class CreateUserAccountResource(
80 UserAccountResourceIdentifier,
81 ResourceData[CreateUserAccountAttributes, CreateUserAccountRelationships],
82):
83 """A JSON:API resource for creating a user account."""