Coverage for src/kwai/api/v1/auth/authors/schemas.py: 100%

16 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2024-01-01 00:00 +0000

1"""Module for defining schemas for an author resource.""" 

2 

3from typing import Literal, Self, TypeAlias 

4 

5from pydantic import BaseModel 

6 

7from kwai.core.json_api import ( 

8 MultipleDocument, 

9 ResourceData, 

10 ResourceIdentifier, 

11 ResourceMeta, 

12 SingleDocument, 

13) 

14from kwai.modules.portal.domain.author import AuthorEntity 

15 

16 

17class AuthorResourceIdentifier(ResourceIdentifier): 

18 """A JSON:API resource identifier for an author.""" 

19 

20 type: Literal["authors"] = "authors" 

21 

22 

23class AuthorAttributes(BaseModel): 

24 """Attributes for an author.""" 

25 

26 name: str 

27 remark: str 

28 active: bool 

29 

30 

31class AuthorResource(AuthorResourceIdentifier, ResourceData[AuthorAttributes, None]): 

32 """A JSON:API resource for an author.""" 

33 

34 @classmethod 

35 def create(cls, author: AuthorEntity) -> Self: 

36 """Create a JSON:API resource for an author.""" 

37 return cls( 

38 id=str(author.uuid), 

39 attributes=AuthorAttributes( 

40 name=author.name, remark=author.remark, active=author.active 

41 ), 

42 meta=ResourceMeta( 

43 created_at=str(author.traceable_time.created_at), 

44 updated_at=None 

45 if author.traceable_time.updated_at 

46 else str(author.traceable_time.updated_at), 

47 ), 

48 ) 

49 

50 

51AuthorDocument: TypeAlias = SingleDocument[AuthorResource, None] 

52AuthorsDocument: TypeAlias = MultipleDocument[AuthorResource, None]