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

1"""Schemas for a user account resource.""" 

2 

3from typing import Self 

4 

5from pydantic import BaseModel 

6 

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 

16 

17 

18class BaseUserAccountAttributes(BaseModel): 

19 """Base attributes for a user account JSON:API resource.""" 

20 

21 email: str 

22 first_name: str 

23 last_name: str 

24 remark: str 

25 

26 

27class UserAccountAttributes(BaseUserAccountAttributes): 

28 """Attributes of a user account JSON:API resource.""" 

29 

30 last_login: str | None 

31 last_unsuccessful_login: str | None 

32 revoked: bool 

33 admin: bool 

34 

35 

36class CreateUserAccountAttributes(BaseUserAccountAttributes): 

37 """Attributes for creating a user account.""" 

38 

39 password: str 

40 

41 

42class UserAccountResource( 

43 UserAccountResourceIdentifier, ResourceData[UserAccountAttributes, None] 

44): 

45 """A JSON:API resource for a user account.""" 

46 

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 ) 

67 

68 

69UserAccountDocument = SingleDocument[UserAccountResource, None] 

70UserAccountsDocument = MultipleDocument[UserAccountResource, None] 

71 

72 

73class CreateUserAccountRelationships(BaseModel): 

74 """Relationships needed for creating a user account.""" 

75 

76 user_invitation: Relationship[UserInvitationResourceIdentifier] 

77 

78 

79class CreateUserAccountResource( 

80 UserAccountResourceIdentifier, 

81 ResourceData[CreateUserAccountAttributes, CreateUserAccountRelationships], 

82): 

83 """A JSON:API resource for creating a user account."""