Coverage for kwai/api/v1/portal/schemas/story.py: 76%
41 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
1"""Schemas for a story on a portal."""
2from pydantic import BaseModel
4from kwai.api.converter import MarkdownConverter
5from kwai.core import json_api
6from kwai.modules.news.stories.story import Application, StoryEntity
9@json_api.resource(type_="applications")
10class PortalApplicationResource:
11 """JSON:API resource for an application."""
13 def __init__(self, application: Application):
14 self._application = application
16 @json_api.id
17 def get_id(self) -> str:
18 """Get the id."""
19 return str(self._application.id)
21 @json_api.attribute(name="name")
22 def get_name(self) -> str:
23 """Get the name of the application."""
24 return self._application.name
26 @json_api.attribute(name="title")
27 def get_title(self) -> str:
28 """Get the title of the application."""
29 return self._application.title
32class StoryContent(BaseModel):
33 """Schema for the content of a story."""
35 locale: str
36 title: str
37 summary: str
38 content: str | None
41@json_api.resource(type_="stories")
42class PortalStoryResource:
43 """Represent a JSONAPI resource for a news story."""
45 def __init__(self, story: StoryEntity):
46 """Construct.
48 Args:
49 story: The story news entity that is transformed into a JSONAPI resource.
50 """
51 self._story = story
53 @json_api.id
54 def get_id(self) -> str:
55 """Get the id of the story."""
56 return str(self._story.id)
58 @json_api.attribute(name="priority")
59 def get_priority(self) -> int:
60 """Get the priority of the promotion."""
61 return self._story.promotion.priority
63 @json_api.attribute(name="publish_date")
64 def get_publish_date(self) -> str:
65 """Get the publication date."""
66 return str(self._story.period.start_date)
68 @json_api.attribute(name="content")
69 def get_content(self) -> list[StoryContent]:
70 """Get the content of the story."""
71 return [
72 StoryContent(
73 locale=content.locale,
74 title=content.title,
75 summary=MarkdownConverter().convert(content.summary),
76 content=MarkdownConverter().convert(content.content)
77 if content.content
78 else None,
79 )
80 for content in self._story.content
81 ]
83 @json_api.relationship(name="application")
84 def get_application(self) -> PortalApplicationResource:
85 """Get the application of the story."""
86 return PortalApplicationResource(self._story.application)