Coverage for src/kwai/api/v1/auth/schemas/user_invitation.py: 100%
19 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 invitation resource."""
3from typing import Self
5from pydantic import BaseModel
7from kwai.api.schemas.resources import UserInvitationResourceIdentifier
8from kwai.core.json_api import Document, ResourceData
9from kwai.modules.identity.user_invitations.user_invitation import UserInvitationEntity
12class UserInvitationAttributes(BaseModel):
13 """Attributes of a user invitation JSON:API resource."""
15 email: str
16 first_name: str
17 last_name: str
18 remark: str = ""
19 mailed_at: str | None = None
20 expired_at: str | None = None
21 confirmed_at: str | None = None
22 revoked: bool
25class UserInvitationResource(
26 UserInvitationResourceIdentifier,
27 ResourceData[UserInvitationAttributes, None],
28):
29 """A JSON:API resource of a user invitation."""
32class UserInvitationDocument(Document[UserInvitationResource, None]):
33 """A JSON:API document for one or more user invitations."""
35 @classmethod
36 def create(cls, user_invitation: UserInvitationEntity) -> Self:
37 """Create a document for a user invitation."""
38 return cls(
39 data=UserInvitationResource(
40 id=str(user_invitation.uuid),
41 attributes=UserInvitationAttributes(
42 email=str(user_invitation.email),
43 first_name=user_invitation.name.first_name or "",
44 last_name=user_invitation.name.last_name or "",
45 remark=user_invitation.remark,
46 mailed_at=(
47 str(user_invitation.mailed_at)
48 if user_invitation.mailed
49 else None
50 ),
51 expired_at=str(user_invitation.expired_at),
52 confirmed_at=(
53 str(user_invitation.confirmed_at)
54 if user_invitation.confirmed
55 else None
56 ),
57 revoked=user_invitation.revoked,
58 ),
59 )
60 )