Coverage for src/kwai/modules/portal/applications/application_tables.py: 100%
26 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module that defines all dataclasses for the tables containing applications."""
3from dataclasses import dataclass
4from datetime import datetime
6from kwai.core.db.table import Table
7from kwai.core.domain.value_objects.timestamp import Timestamp
8from kwai.core.domain.value_objects.traceable_time import TraceableTime
9from kwai.modules.portal.applications.application import (
10 ApplicationEntity,
11 ApplicationIdentifier,
12)
15@dataclass(kw_only=True, frozen=True, slots=True)
16class ApplicationRow:
17 """Represent a table row of the applications table.
19 Attributes:
20 id: the id of the application
21 title: the title of the application
22 name: a unique name for the application
23 short_description: a short description about the application
24 description: a description about the application
25 remark: a remark about the application
26 news: does this application can contain news stories?
27 pages: does this application can contain pages?
28 events: does this application can contain events?
29 weight: a weight that can be used to order the applications
30 created_at: the timestamp of creation
31 updated_at: the timestamp of the last modification
32 """
34 id: int
35 title: str
36 name: str
37 short_description: str
38 description: str | None
39 remark: str | None
40 news: int
41 pages: int
42 events: int
43 weight: int
44 created_at: datetime
45 updated_at: datetime | None
47 def create_entity(self) -> ApplicationEntity:
48 """Create an application entity from a table row.
50 Returns:
51 An application entity.
52 """
53 return ApplicationEntity(
54 id_=ApplicationIdentifier(self.id),
55 title=self.title,
56 name=self.name,
57 short_description=self.short_description,
58 description=self.description or "",
59 remark=self.remark or "",
60 news=self.news == 1,
61 pages=self.pages == 1,
62 events=self.events == 1,
63 weight=self.weight,
64 traceable_time=TraceableTime(
65 created_at=Timestamp.create_utc(self.created_at),
66 updated_at=Timestamp.create_utc(self.updated_at),
67 ),
68 )
70 @classmethod
71 def persist(cls, application: ApplicationEntity) -> "ApplicationRow":
72 """Persist an application entity.
74 Args:
75 application: the entity to persist.
77 Returns:
78 A dataclass containing the table row data.
79 """
80 return ApplicationRow(
81 id=application.id.value,
82 title=application.title,
83 name=application.name,
84 short_description=application.short_description,
85 description=application.description,
86 remark=application.remark,
87 news=1 if application.can_contain_news else 0,
88 pages=1 if application.can_contain_pages else 0,
89 events=1 if application.can_contain_events else 0,
90 weight=application.weight,
91 created_at=application.traceable_time.created_at.timestamp,
92 updated_at=application.traceable_time.updated_at.timestamp,
93 )
96ApplicationsTable = Table("applications", ApplicationRow)