Coverage for bc/kwai-bc-portal/src/kwai_bc_portal/applications/application_tables.py: 100%
15 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.11.0, 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
5from typing import Self
7from kwai_core.db.table_row import TableRow
8from kwai_core.domain.value_objects.timestamp import Timestamp
9from kwai_core.domain.value_objects.traceable_time import TraceableTime
11from kwai_bc_portal.domain.application import (
12 ApplicationEntity,
13 ApplicationIdentifier,
14)
17@dataclass(kw_only=True, frozen=True, slots=True)
18class ApplicationRow(TableRow):
19 """Represent a table row of the applications table.
21 Attributes:
22 id: the id of the application
23 title: the title of the application
24 name: a unique name for the application
25 short_description: a short description about the application
26 description: a description about the application
27 remark: a remark about the application
28 news: does this application can contain news stories?
29 pages: does this application can contain pages?
30 events: does this application can contain events?
31 weight: a weight that can be used to order the applications
32 created_at: the timestamp of creation
33 updated_at: the timestamp of the last modification
34 """
36 __table_name__ = "applications"
38 id: int
39 title: str
40 name: str
41 short_description: str
42 description: str | None
43 remark: str | None
44 news: int
45 pages: int
46 events: int
47 weight: int
48 created_at: datetime
49 updated_at: datetime | None
51 def create_entity(self) -> ApplicationEntity:
52 """Create an application entity from a table row.
54 Returns:
55 An application entity.
56 """
57 return ApplicationEntity(
58 id=ApplicationIdentifier(self.id),
59 title=self.title,
60 name=self.name,
61 short_description=self.short_description,
62 description=self.description or "",
63 remark=self.remark or "",
64 news=self.news == 1,
65 pages=self.pages == 1,
66 events=self.events == 1,
67 weight=self.weight,
68 traceable_time=TraceableTime(
69 created_at=Timestamp.create_utc(self.created_at),
70 updated_at=Timestamp.create_utc(self.updated_at),
71 ),
72 )
74 @classmethod
75 def persist(cls, application: ApplicationEntity) -> Self:
76 """Persist an application entity.
78 Args:
79 application: the entity to persist.
81 Returns:
82 A dataclass containing the table row data.
83 """
84 return cls(
85 id=application.id.value,
86 title=application.title,
87 name=application.name,
88 short_description=application.short_description,
89 description=application.description,
90 remark=application.remark,
91 news=1 if application.can_contain_news else 0,
92 pages=1 if application.can_contain_pages else 0,
93 events=1 if application.can_contain_events else 0,
94 weight=application.weight,
95 created_at=application.traceable_time.created_at.timestamp, # type: ignore
96 updated_at=application.traceable_time.updated_at.timestamp,
97 )