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

33 statements  

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

1"""Schemas for a news item.""" 

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 NewsItemResourceIdentifier, 

12) 

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

14from kwai.modules.portal.news.news_item import NewsItemEntity 

15 

16 

17class NewsItemText(BaseModel): 

18 """Schema for the text of a news item.""" 

19 

20 locale: str 

21 format: str 

22 title: str 

23 summary: str 

24 content: str | None = None 

25 original_summary: str 

26 original_content: str | None = None 

27 

28 

29class NewsItemAttributes(BaseModel): 

30 """Attributes of a news item JSON:API resource.""" 

31 

32 priority: int = 0 

33 publish_date: str 

34 end_date: str | None = None 

35 enabled: bool = False 

36 remark: str = "" 

37 promotion_end_date: str | None = None 

38 texts: list[NewsItemText] 

39 

40 

41class NewsItemRelationships(BaseModel): 

42 """Relationships of a news item JSON:API resource.""" 

43 

44 application: Relationship[ApplicationResourceIdentifier] 

45 

46 

47class NewsItemResource( 

48 NewsItemResourceIdentifier, ResourceData[NewsItemAttributes, NewsItemRelationships] 

49): 

50 """A JSON:API resource for a news item.""" 

51 

52 

53class NewsItemApplicationResource( 

54 ApplicationResourceIdentifier, ResourceData[ApplicationBaseAttributes, NoneType] 

55): 

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

57 

58 

59class NewsItemDocument(Document[NewsItemResource, NewsItemApplicationResource]): 

60 """A JSON:API document for one or more news items.""" 

61 

62 @classmethod 

63 def create(cls, news_item: NewsItemEntity) -> "NewsItemDocument": 

64 """Create a document from a news item entity.""" 

65 data = NewsItemResource( 

66 id=str(news_item.id), 

67 meta=ResourceMeta( 

68 created_at=str(news_item.traceable_time.created_at), 

69 updated_at=str(news_item.traceable_time.updated_at), 

70 ), 

71 attributes=NewsItemAttributes( 

72 priority=news_item.promotion.priority, 

73 publish_date=str(news_item.period.start_date), 

74 end_date=str(news_item.period.end_date), 

75 enabled=news_item.is_enabled, 

76 remark=news_item.remark or "", 

77 promotion_end_date=str(news_item.promotion.end_date), 

78 texts=[ 

79 NewsItemText( 

80 locale=text.locale.value, 

81 format=text.format.value, 

82 title=text.title, 

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

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

85 if text.content 

86 else None, 

87 original_summary=text.summary, 

88 original_content=text.content, 

89 ) 

90 for text in news_item.texts 

91 ], 

92 ), 

93 relationships=NewsItemRelationships( 

94 application=Relationship[ApplicationResourceIdentifier]( 

95 data=ApplicationResourceIdentifier(id=str(news_item.application.id)) 

96 ) 

97 ), 

98 ) 

99 

100 included = { 

101 NewsItemApplicationResource( 

102 id=str(news_item.application.id), 

103 attributes=ApplicationBaseAttributes( 

104 name=news_item.application.name, title=news_item.application.title 

105 ), 

106 ) 

107 } 

108 

109 return NewsItemDocument(data=data, included=included)