Coverage for bc/kwai-bc-portal/src/kwai_bc_portal/pages/_tables.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.11.0, 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_row import TableRow 

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 

11 

12from kwai_bc_portal.domain.application import ApplicationEntity 

13from kwai_bc_portal.domain.page import PageEntity, PageIdentifier 

14 

15 

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

17class PageTextRow(TextRow, TableRow): 

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

19 

20 Attributes: 

21 page_id: The id of a page. 

22 """ 

23 

24 __table_name__ = "page_contents" 

25 

26 page_id: int 

27 

28 @classmethod 

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

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

31 

32 Args: 

33 page: The page that contains the content. 

34 content: The content of a page. 

35 """ 

36 return PageTextRow( 

37 page_id=page.id.value, 

38 locale=content.locale.value, 

39 format=content.format.value, 

40 title=content.title, 

41 content=content.content, 

42 summary=content.summary, 

43 user_id=content.author.id.value, 

44 created_at=content.traceable_time.created_at.timestamp, # type: ignore 

45 updated_at=content.traceable_time.updated_at.timestamp, 

46 ) 

47 

48 

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

50class PageRow(TableRow): 

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 __table_name__ = "pages" 

64 

65 id: int 

66 enabled: int 

67 remark: str | None 

68 application_id: int 

69 priority: int 

70 created_at: datetime 

71 updated_at: datetime | None 

72 

73 def create_entity( 

74 self, application: ApplicationEntity, content: tuple[LocaleText, ...] 

75 ) -> PageEntity: 

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

77 return PageEntity( 

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

79 enabled=self.enabled == 1, 

80 application=application, 

81 priority=self.priority, 

82 texts=content, 

83 remark=self.remark or "", 

84 traceable_time=TraceableTime( 

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

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

87 ), 

88 ) 

89 

90 @classmethod 

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

92 """Persist an entity to row data. 

93 

94 Args: 

95 page: The page to persist. 

96 """ 

97 return PageRow( 

98 id=page.id.value, 

99 enabled=1 if page.enabled else 0, 

100 remark=page.remark or "", 

101 application_id=page.application.id.value, 

102 priority=page.priority, 

103 created_at=page.traceable_time.created_at.timestamp, # type: ignore 

104 updated_at=page.traceable_time.updated_at.timestamp, 

105 )