Coverage for apps/kwai-api/src/kwai_api/v1/trainings/coaches/presenters.py: 100%
25 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 presenters for the trainings/coaches endpoints."""
3from typing import Self
5from kwai_bc_training.coaches.coach import CoachEntity
6from kwai_bc_training.trainings.training import TrainingEntity
7from kwai_core.domain.presenter import AsyncPresenter, IterableResult, Presenter
8from kwai_core.json_api import JsonApiPresenter, Meta, Relationship
10from kwai_api.schemas.resources import CoachResourceIdentifier
11from kwai_api.v1.trainings.coaches.schemas import (
12 CoachAttributes,
13 CoachDocument,
14 CoachesDocument,
15 CoachResource,
16 TrainingCoachAttributes,
17 TrainingCoachesDocument,
18 TrainingCoachRelationships,
19 TrainingCoachResource,
20)
23class JsonApiCoachPresenter(JsonApiPresenter[CoachDocument], Presenter[CoachEntity]):
24 """A presenter that transforms an iterable of coaches into a JSON:API document."""
26 def present(self, coach: CoachEntity) -> Self:
27 self._document = CoachDocument(
28 data=CoachResource(
29 id=str(coach.uuid),
30 attributes=CoachAttributes(name=str(coach.name), head=coach.head),
31 )
32 )
33 return self
36class JsonApiCoachesPresenter(
37 JsonApiPresenter[CoachesDocument], AsyncPresenter[IterableResult[CoachEntity]]
38):
39 """A presenter that transforms an iterable of coaches into a JSON:API document."""
41 async def present(self, use_case_result: IterableResult[CoachEntity]) -> Self:
42 self._document = CoachesDocument(
43 meta=Meta(
44 count=use_case_result.count,
45 offset=use_case_result.offset,
46 limit=use_case_result.limit,
47 )
48 )
49 async for coach in use_case_result:
50 self._document.data.append(
51 CoachResource(
52 id=str(coach.uuid),
53 attributes=CoachAttributes(name=str(coach.name), head=coach.head),
54 )
55 )
56 return self
59class JsonApiTrainingCoachesPresenter(
60 JsonApiPresenter[TrainingCoachesDocument],
61 Presenter[TrainingEntity],
62):
63 """A presenter that transforms an iterable of coaches of a training into a JSON:API document."""
65 def present(self, use_case_result: TrainingEntity) -> Self:
66 self._document = TrainingCoachesDocument(
67 meta=Meta(count=len(use_case_result.coaches))
68 )
69 for training_coach in use_case_result.coaches:
70 self._document.data.append(
71 TrainingCoachResource(
72 id=str(training_coach.id),
73 attributes=TrainingCoachAttributes(
74 payed=training_coach.payed,
75 present=training_coach.present,
76 head=training_coach.type == 1,
77 remark=training_coach.remark,
78 ),
79 relationships=TrainingCoachRelationships(
80 coach=Relationship[CoachResourceIdentifier](
81 data=CoachResourceIdentifier(
82 id=str(training_coach.coach.uuid)
83 )
84 )
85 ),
86 )
87 )
88 coach_document = (
89 JsonApiCoachPresenter().present(training_coach.coach).get_document()
90 )
91 self._document.included |= {coach_document.data}
92 return self