Coverage for kwai/modules/training/trainings/training_tables.py: 99%
89 statements
« prev ^ index » next coverage.py v7.3.0, created at 2023-09-05 17:55 +0000
« 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 trainings."""
3from dataclasses import dataclass
4from datetime import datetime, time
6from kwai.core.db.rows import ContentRow
7from kwai.core.db.table import Table
8from kwai.core.domain.value_objects.local_timestamp import LocalTimestamp
9from kwai.core.domain.value_objects.owner import Owner
10from kwai.core.domain.value_objects.period import Period
11from kwai.core.domain.value_objects.text import LocaleText
12from kwai.core.domain.value_objects.time_period import TimePeriod
13from kwai.core.domain.value_objects.traceable_time import TraceableTime
14from kwai.core.domain.value_objects.weekday import Weekday
15from kwai.modules.training.coaches.coach import CoachEntity
16from kwai.modules.training.teams.team import TeamEntity
17from kwai.modules.training.trainings.training import (
18 TrainingEntity,
19 TrainingIdentifier,
20)
21from kwai.modules.training.trainings.training_definition import (
22 TrainingDefinitionEntity,
23 TrainingDefinitionIdentifier,
24)
25from kwai.modules.training.trainings.value_objects import TrainingCoach
28@dataclass(kw_only=True, frozen=True, slots=True)
29class TrainingContentRow(ContentRow):
30 """Represent a row in the training_contents table.
32 Attributes:
33 training_id: The id of the training
34 """
36 training_id: int
38 @classmethod
39 def persist(cls, training: TrainingEntity, content: LocaleText):
40 """Persist a content value object to this table.
42 Args:
43 training: The training that contains the text content.
44 content: The text content of the training.
45 """
46 return TrainingContentRow(
47 training_id=training.id.value,
48 locale=content.locale,
49 format=content.format,
50 title=content.title,
51 content=content.content,
52 summary=content.summary,
53 user_id=content.author.id.value,
54 created_at=content.traceable_time.created_at.timestamp,
55 updated_at=content.traceable_time.updated_at.timestamp,
56 )
59TrainingContentsTable = Table("training_contents", TrainingContentRow)
62@dataclass(kw_only=True, frozen=True, slots=True)
63class TrainingRow:
64 """Represent a table row of the trainings table.
66 Attributes:
67 id: the id of the training
68 definition_id: the id of the relation training definition
69 season_id: the id of the related season
70 created_at: the timestamp of creation
71 updated_at: the timestamp of the last modification
72 start_date: the timestamp of the start of the training
73 end_date: the timestamp of the end of the training
74 active: is this training active?
75 cancelled: is this training cancelled?
76 location: the location of the training
77 remark: a remark about the training
78 """
80 id: int
81 definition_id: int | None
82 season_id: int | None
83 created_at: datetime
84 updated_at: datetime | None
85 start_date: datetime
86 end_date: datetime
87 active: int
88 cancelled: int
89 location: str | None
90 remark: str | None
92 def create_entity(
93 self,
94 content: list[LocaleText],
95 definition: TrainingDefinitionEntity | None = None,
96 ) -> TrainingEntity:
97 """Create a training entity from the table row.
99 Returns:
100 A training entity.
101 """
102 return TrainingEntity(
103 id_=TrainingIdentifier(self.id),
104 content=content,
105 definition=definition,
106 period=Period(
107 start_date=LocalTimestamp(self.start_date),
108 end_date=LocalTimestamp(self.end_date),
109 ),
110 active=self.active == 1,
111 cancelled=self.cancelled == 1,
112 location=self.location,
113 remark=self.remark,
114 traceable_time=TraceableTime(
115 created_at=LocalTimestamp(self.created_at),
116 updated_at=LocalTimestamp(self.updated_at),
117 ),
118 )
120 @classmethod
121 def persist(cls, training: TrainingEntity) -> "TrainingRow":
122 """Persist a training.
124 Args:
125 training: The training to persist.
127 Returns:
128 A dataclass containing the table row data.
129 """
130 return TrainingRow(
131 id=training.id.value,
132 definition_id=None
133 if training.definition is None
134 else training.definition.id.value,
135 season_id=None,
136 created_at=training.traceable_time.created_at.timestamp,
137 updated_at=training.traceable_time.updated_at.timestamp,
138 start_date=training.period.start_date.timestamp,
139 end_date=training.period.end_date.timestamp,
140 active=1 if training.active else 0,
141 cancelled=1 if training.cancelled else 0,
142 location=training.location,
143 remark=training.remark or "",
144 )
147TrainingsTable = Table("trainings", TrainingRow)
150@dataclass(kw_only=True, frozen=True, slots=True)
151class TrainingDefinitionRow:
152 """Represent a table row of the training definitions table."""
154 id: int
155 name: str
156 description: str
157 season_id: int | None
158 team_id: int | None
159 weekday: int
160 start_time: time
161 end_time: time
162 active: int
163 location: str | None
164 remark: str | None
165 user_id: int
166 created_at: datetime
167 updated_at: datetime | None
169 def create_entity(self, owner: Owner) -> TrainingDefinitionEntity:
170 """Create a training definition entity from a table row.
172 Args:
173 owner: The owner of the training definition.
175 Returns:
176 A training definition entity.
177 """
178 return TrainingDefinitionEntity(
179 id_=TrainingDefinitionIdentifier(self.id),
180 name=self.name,
181 description=self.description,
182 weekday=Weekday(self.weekday),
183 period=TimePeriod(
184 start=self.start_time,
185 end=self.end_time,
186 ),
187 active=self.active == 1,
188 location=self.location or "",
189 remark=self.remark or "",
190 owner=owner,
191 traceable_time=TraceableTime(
192 created_at=LocalTimestamp(self.created_at),
193 updated_at=LocalTimestamp(self.updated_at),
194 ),
195 )
197 @classmethod
198 def persist(
199 cls, training_definition: TrainingDefinitionEntity
200 ) -> "TrainingDefinitionRow":
201 """Persist a training definition entity."""
202 return TrainingDefinitionRow(
203 id=training_definition.id.value,
204 name=training_definition.name,
205 description=training_definition.description,
206 season_id=None,
207 team_id=None,
208 weekday=training_definition.weekday.value,
209 start_time=training_definition.period.start,
210 end_time=training_definition.period.end,
211 active=1 if training_definition.active else 0,
212 location=training_definition.location,
213 remark=training_definition.remark,
214 user_id=training_definition.owner.id.value,
215 created_at=training_definition.traceable_time.created_at.timestamp,
216 updated_at=training_definition.traceable_time.updated_at.timestamp,
217 )
220TrainingDefinitionsTable = Table("training_definitions", TrainingDefinitionRow)
223@dataclass(kw_only=True, frozen=True, slots=True)
224class TrainingCoachRow:
225 """Represent a row of the training_coaches table."""
227 training_id: int
228 coach_id: int
229 coach_type: int
230 present: int
231 payed: int
232 remark: str | None
233 user_id: int
234 created_at: datetime
235 updated_at: datetime | None
237 def create_coach(self, coach: CoachEntity, owner: Owner) -> TrainingCoach:
238 """Create a TrainingCoach value object."""
239 return TrainingCoach(
240 coach=coach,
241 owner=owner,
242 present=self.present == 1,
243 type=self.coach_type,
244 payed=self.payed == 1,
245 remark="" if self.remark is None else self.remark,
246 )
248 @classmethod
249 def persist(cls, training, training_coach: TrainingCoach) -> "TrainingCoachRow":
250 """Persist a TrainingCoach value object into a table row."""
251 return TrainingCoachRow(
252 training_id=training.id.value,
253 coach_id=training_coach.coach.id.value,
254 coach_type=training_coach.type,
255 present=1 if training_coach.present else 0,
256 payed=1 if training_coach.payed else 0,
257 remark=training_coach.remark,
258 user_id=training_coach.owner.id.value,
259 created_at=training_coach.traceable_time.created_at.timestamp,
260 updated_at=training_coach.traceable_time.updated_at.timestamp,
261 )
264TrainingCoachesTable = Table("training_coaches", TrainingCoachRow)
267@dataclass(kw_only=True, frozen=True, slots=True)
268class TrainingTeamRow:
269 """Represent a row of the training_teams table."""
271 training_id: int
272 team_id: int
274 @classmethod
275 def persist(cls, training: TrainingEntity, team: TeamEntity) -> "TrainingTeamRow":
276 """Persist a team of a training to a table row."""
277 return TrainingTeamRow(
278 training_id=training.id.value,
279 team_id=team.id.value,
280 )
283TrainingTeamsTable = Table("training_teams", TrainingTeamRow)