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

17 statements  

« prev     ^ index     » next       coverage.py v7.8.0, 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 editor: bool 

30 

31 

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

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

34 

35 @classmethod 

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

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

38 return cls( 

39 id=str(author.uuid), 

40 attributes=AuthorAttributes( 

41 name=author.name, 

42 remark=author.remark, 

43 active=author.active, 

44 editor=author.editor, 

45 ), 

46 meta=ResourceMeta( 

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

48 updated_at=None 

49 if author.traceable_time.updated_at 

50 else str(author.traceable_time.updated_at), 

51 ), 

52 ) 

53 

54 

55AuthorDocument: TypeAlias = SingleDocument[AuthorResource, None] 

56AuthorsDocument: TypeAlias = MultipleDocument[AuthorResource, None]