Coverage for apps/kwai-api/src/kwai_api/v1/club/coaches/presenters.py: 97%
32 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 coaches endpoint of the club API."""
3from typing import Self
5from kwai_bc_club.domain.club_coach import ClubCoachEntity
6from kwai_core.domain.presenter import AsyncPresenter, IterableResult, Presenter
7from kwai_core.json_api import JsonApiPresenter, Meta, Relationship, ResourceMeta
9from kwai_api.v1.club.coaches.schemas import (
10 CoachAttributes,
11 CoachDocument,
12 CoachesDocument,
13 CoachRelationships,
14 CoachResource,
15 PublicCoachAttributes,
16 PublicCoachDocument,
17 PublicCoachesDocument,
18 PublicCoachResource,
19 UserAttributes,
20 UserResource,
21)
22from kwai_api.v1.club.schemas.resources import (
23 MemberResourceIdentifier,
24 UserResourceIdentifier,
25)
28class JsonApiPublicCoachPresenter(
29 JsonApiPresenter[PublicCoachDocument], Presenter[ClubCoachEntity]
30):
31 """A presenter that transforms a club coach entity into a JSON:API document."""
33 def present(self, club_coach: ClubCoachEntity) -> Self:
34 self._document = PublicCoachDocument(
35 data=PublicCoachResource(
36 meta=ResourceMeta(
37 created_at=str(club_coach.traceable_time.created_at),
38 updated_at=str(club_coach.traceable_time.updated_at),
39 ),
40 id=str(club_coach.id),
41 attributes=PublicCoachAttributes(
42 diploma=club_coach.diploma,
43 description=club_coach.description,
44 name=str(club_coach.name),
45 ),
46 )
47 )
48 return self
51class JsonApiPublicCoachesPresenter(
52 JsonApiPresenter[PublicCoachesDocument],
53 AsyncPresenter[IterableResult[ClubCoachEntity]],
54):
55 """A presenter for transforming an iterator with coaches into a JSON:API document."""
57 async def present(self, result: IterableResult[ClubCoachEntity]) -> Self:
58 self._document = PublicCoachesDocument(
59 meta=Meta(count=result.count, offset=result.offset, limit=result.limit)
60 )
61 async for coach in result.iterator:
62 coach_document = JsonApiPublicCoachPresenter().present(coach).get_document()
63 self._document.data.append(coach_document.data)
65 return self
68class JsonApiCoachPresenter(
69 JsonApiPresenter[CoachDocument], Presenter[ClubCoachEntity]
70):
71 """A presenter that transforms a club coach entity into a JSON:API document."""
73 def present(self, club_coach: ClubCoachEntity) -> Self:
74 if club_coach.member.user is None:
75 included = set()
76 else:
77 included = {
78 UserResource(
79 id=str(club_coach.member.user.uuid),
80 attributes=UserAttributes(uuid=str(club_coach.member.user.uuid)),
81 )
82 }
84 self._document = CoachDocument(
85 data=CoachResource(
86 meta=ResourceMeta(
87 created_at=str(club_coach.traceable_time.created_at),
88 updated_at=str(club_coach.traceable_time.updated_at),
89 ),
90 id=str(club_coach.id),
91 attributes=CoachAttributes(
92 diploma=club_coach.diploma,
93 description=club_coach.description,
94 name=str(club_coach.name),
95 active=club_coach.active,
96 remark=club_coach.remark,
97 license=club_coach.member.license.number,
98 license_end_date=str(club_coach.member.license.end_date),
99 birthdate=str(club_coach.member.person.birthdate),
100 ),
101 relationships=CoachRelationships(
102 user=Relationship[UserResourceIdentifier](
103 data=None
104 if club_coach.member.user is None
105 else UserResourceIdentifier(id=str(club_coach.member.user.uuid))
106 ),
107 member=Relationship[MemberResourceIdentifier](
108 data=MemberResourceIdentifier(id=str(club_coach.member.uuid))
109 ),
110 ),
111 ),
112 included=included,
113 )
114 return self
117class JsonApiCoachesPresenter(
118 JsonApiPresenter[CoachesDocument],
119 AsyncPresenter[IterableResult[ClubCoachEntity]],
120):
121 """A presenter for transforming an iterator with coaches into a JSON:API document."""
123 async def present(self, result: IterableResult[ClubCoachEntity]) -> Self:
124 self._document = CoachesDocument(
125 meta=Meta(count=result.count, offset=result.offset, limit=result.limit)
126 )
127 async for coach in result.iterator:
128 coach_document = JsonApiCoachPresenter().present(coach).get_document()
129 self._document.data.append(coach_document.data)
130 self._document.included |= coach_document.included
132 return self