Coverage for src/kwai/modules/portal/pages/page_tables.py: 100%

31 statements  

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

1"""Module for defining all table classes for page.""" 

2 

3from dataclasses import dataclass 

4from datetime import datetime 

5 

6from kwai.core.db.rows import TextRow 

7from kwai.core.db.table import Table 

8from kwai.core.domain.value_objects.text import LocaleText 

9from kwai.core.domain.value_objects.timestamp import Timestamp 

10from kwai.core.domain.value_objects.traceable_time import TraceableTime 

11from kwai.modules.portal.applications.application import ApplicationEntity 

12from kwai.modules.portal.pages.page import PageEntity, PageIdentifier 

13 

14 

15@dataclass(kw_only=True, frozen=True, slots=True) 

16class PageTextRow(TextRow): 

17 """Represent a row in the page_contents table. 

18 

19 Attributes: 

20 page_id: The id of a page. 

21 """ 

22 

23 page_id: int 

24 

25 @classmethod 

26 def persist(cls, page: PageEntity, content: LocaleText): 

27 """Persist a content value object to the table. 

28 

29 Args: 

30 page: The page that contains the content. 

31 content: The content of a page. 

32 """ 

33 return PageTextRow( 

34 page_id=page.id.value, 

35 locale=content.locale.value, 

36 format=content.format.value, 

37 title=content.title, 

38 content=content.content, 

39 summary=content.summary, 

40 user_id=content.author.id.value, 

41 created_at=content.traceable_time.created_at.timestamp, 

42 updated_at=content.traceable_time.updated_at.timestamp, 

43 ) 

44 

45 

46PageContentsTable = Table("page_contents", PageTextRow) 

47 

48 

49@dataclass(kw_only=True, frozen=True, slots=True) 

50class PageRow: 

51 """Represent a table row of the page table. 

52 

53 Attributes: 

54 id: the id of the page. 

55 enabled: is this page enabled? 

56 remark: a remark about the page 

57 application_id: the link to the application 

58 priority: the priority of the page 

59 created_at: the timestamp of creation 

60 updated_at: the timestamp of the last modification 

61 """ 

62 

63 id: int 

64 enabled: int 

65 remark: str | None 

66 application_id: int 

67 priority: int 

68 created_at: datetime 

69 updated_at: datetime | None 

70 

71 def create_entity( 

72 self, application: ApplicationEntity, content: list[LocaleText] 

73 ) -> PageEntity: 

74 """Create a page entity from a table row.""" 

75 return PageEntity( 

76 id_=PageIdentifier(id_=self.id), 

77 enabled=self.enabled == 1, 

78 application=application, 

79 priority=self.priority, 

80 texts=content, 

81 remark=self.remark, 

82 traceable_time=TraceableTime( 

83 created_at=Timestamp.create_utc(timestamp=self.created_at), 

84 updated_at=Timestamp.create_utc(timestamp=self.updated_at), 

85 ), 

86 ) 

87 

88 @classmethod 

89 def persist(cls, page: PageEntity) -> "PageRow": 

90 """Persist an entity to row data. 

91 

92 Args: 

93 page: The page to persist. 

94 """ 

95 return PageRow( 

96 id=page.id.value, 

97 enabled=1 if page.enabled else 0, 

98 remark=page.remark or "", 

99 application_id=page.application.id.value, 

100 priority=page.priority, 

101 created_at=page.traceable_time.created_at.timestamp, 

102 updated_at=page.traceable_time.updated_at.timestamp, 

103 ) 

104 

105 

106PagesTable = Table("pages", PageRow)