Coverage for kwai/modules/portal/applications/application_tables.py: 100%

26 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-05 17:55 +0000

1"""Module that defines all dataclasses for the tables containing applications.""" 

2from dataclasses import dataclass 

3from datetime import datetime 

4 

5from kwai.core.db.table import Table 

6from kwai.core.domain.value_objects.local_timestamp import LocalTimestamp 

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

8from kwai.modules.portal.applications.application import ( 

9 ApplicationEntity, 

10 ApplicationIdentifier, 

11) 

12 

13 

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

15class ApplicationRow: 

16 """Represent a table row of the applications table. 

17 

18 Attributes: 

19 id: the id of the application 

20 title: the title of the application 

21 name: a unique name for the application 

22 short_description: a short description about the application 

23 description: a description about the application 

24 remark: a remark about the application 

25 news: does this application can contain news stories? 

26 pages: does this application can contain pages? 

27 events does this application can contain events? 

28 weight: a weight that can be used to order the applications 

29 created_at: the timestamp of creation 

30 updated_at: the timestamp of the last modification 

31 """ 

32 

33 id: int 

34 title: str 

35 name: str 

36 short_description: str 

37 description: str | None 

38 remark: str | None 

39 news: int 

40 pages: int 

41 events: int 

42 weight: int 

43 created_at: datetime 

44 updated_at: datetime | None 

45 

46 def create_entity(self) -> ApplicationEntity: 

47 """Create an application entity from a table row. 

48 

49 Returns: 

50 An application entity. 

51 """ 

52 return ApplicationEntity( 

53 id_=ApplicationIdentifier(self.id), 

54 title=self.title, 

55 name=self.name, 

56 short_description=self.short_description, 

57 description=self.description or "", 

58 remark=self.remark or "", 

59 news=self.news == 1, 

60 pages=self.pages == 1, 

61 events=self.events == 1, 

62 weight=self.weight, 

63 traceable_time=TraceableTime( 

64 created_at=LocalTimestamp(self.created_at), 

65 updated_at=LocalTimestamp(self.updated_at), 

66 ), 

67 ) 

68 

69 @classmethod 

70 def persist(cls, application: ApplicationEntity) -> "ApplicationRow": 

71 """Persist an application entity. 

72 

73 Args: 

74 application: the entity to persist. 

75 

76 Returns: 

77 A dataclass containing the table row data. 

78 """ 

79 return ApplicationRow( 

80 id=application.id.value, 

81 title=application.title, 

82 name=application.name, 

83 short_description=application.short_description, 

84 description=application.description, 

85 remark=application.remark, 

86 news=1 if application.can_contain_news else 0, 

87 pages=1 if application.can_contain_pages else 0, 

88 events=1 if application.can_contain_events else 0, 

89 weight=application.weight, 

90 created_at=application.traceable_time.created_at.timestamp, 

91 updated_at=application.traceable_time.updated_at.timestamp, 

92 ) 

93 

94 

95ApplicationsTable = Table("applications", ApplicationRow)