Coverage for src/kwai/api/schemas/page.py: 100%

30 statements  

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

1"""Module for defining the page JSON:API resource.""" 

2 

3from types import NoneType 

4 

5from pydantic import BaseModel 

6 

7from kwai.api.converter import MarkdownConverter 

8from kwai.api.schemas.application import ApplicationBaseAttributes 

9from kwai.api.schemas.resources import ( 

10 ApplicationResourceIdentifier, 

11 PageResourceIdentifier, 

12) 

13from kwai.core.json_api import Document, Relationship, ResourceData, ResourceMeta 

14from kwai.modules.portal.pages.page import PageEntity 

15 

16 

17class PageText(BaseModel): 

18 """Schema for the text of a page.""" 

19 

20 locale: str 

21 format: str 

22 title: str 

23 summary: str 

24 content: str | None 

25 original_summary: str 

26 original_content: str | None 

27 

28 

29class PageAttributes(BaseModel): 

30 """Attributes of a page JSON:API resource.""" 

31 

32 enabled: bool 

33 priority: int 

34 remark: str 

35 texts: list[PageText] 

36 

37 

38class PageRelationships(BaseModel): 

39 """Relationships of a page JSON:API resource.""" 

40 

41 application: Relationship[ApplicationResourceIdentifier] 

42 

43 

44class PageResource( 

45 PageResourceIdentifier, ResourceData[PageAttributes, PageRelationships] 

46): 

47 """A JSON:API resource for a page.""" 

48 

49 

50class PageApplicationResource( 

51 ApplicationResourceIdentifier, ResourceData[ApplicationBaseAttributes, NoneType] 

52): 

53 """A JSON:API resource for an application associated with a page.""" 

54 

55 

56class PageDocument(Document[PageResource, PageApplicationResource]): 

57 """A JSON:API document for one or more pages.""" 

58 

59 @classmethod 

60 def create(cls, page: PageEntity): 

61 """Create a document from a page entity.""" 

62 data = PageResource( 

63 id=str(page.id), 

64 meta=ResourceMeta( 

65 created_at=str(page.traceable_time.created_at), 

66 updated_at=str(page.traceable_time.updated_at), 

67 ), 

68 attributes=PageAttributes( 

69 enabled=page.enabled, 

70 priority=page.priority, 

71 remark=page.remark or "", 

72 texts=[ 

73 PageText( 

74 locale=text.locale.value, 

75 format=text.format.value, 

76 title=text.title, 

77 summary=MarkdownConverter().convert(text.summary), 

78 content=MarkdownConverter().convert(text.content) 

79 if text.content 

80 else None, 

81 original_summary=text.summary, 

82 original_content=text.content, 

83 ) 

84 for text in page.texts 

85 ], 

86 ), 

87 relationships=PageRelationships( 

88 application=Relationship[ApplicationResourceIdentifier]( 

89 data=ApplicationResourceIdentifier(id=str(page.application.id)) 

90 ) 

91 ), 

92 ) 

93 included = { 

94 PageApplicationResource( 

95 id=str(page.application.id), 

96 attributes=ApplicationBaseAttributes( 

97 name=page.application.name, title=page.application.title 

98 ), 

99 ) 

100 } 

101 

102 return PageDocument(data=data, included=included)