Coverage for src/kwai/api/v1/teams/presenters.py: 93%

29 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2024-01-01 00:00 +0000

1"""Module for defining presenters of the teams api.""" 

2 

3from kwai.api.v1.teams.schemas import TeamDocument, TeamMemberDocument 

4from kwai.core.domain.presenter import AsyncPresenter, IterableResult, Presenter 

5from kwai.core.json_api import JsonApiPresenter, Meta 

6from kwai.modules.teams.domain.team import TeamEntity 

7from kwai.modules.teams.domain.team_member import MemberEntity, TeamMember 

8 

9 

10class JsonApiTeamPresenter(JsonApiPresenter[TeamDocument], Presenter[TeamEntity]): 

11 """A presenter that transform a team entity into a JSON:API document.""" 

12 

13 def present(self, team: TeamEntity) -> None: 

14 self._document = TeamDocument.create(team) 

15 

16 

17class JsonApiTeamsPresenter( 

18 JsonApiPresenter[TeamDocument], AsyncPresenter[IterableResult[TeamEntity]] 

19): 

20 """A presenter that transforms an iterator of teams into a JSON:API document.""" 

21 

22 async def present(self, result: IterableResult[TeamEntity]) -> None: 

23 self._document = TeamDocument( 

24 meta=Meta(count=result.count, offset=result.offset, limit=result.limit), 

25 data=[], 

26 ) 

27 async for team in result.iterator: 

28 team_document = TeamDocument.create(team) 

29 self._document.merge(team_document) 

30 

31 

32class JsonApiMembersPresenter( 

33 JsonApiPresenter[TeamMemberDocument], AsyncPresenter[IterableResult[MemberEntity]] 

34): 

35 """A presenter that transforms an iterator of members into a TeamMember document.""" 

36 

37 async def present(self, result: IterableResult[MemberEntity]) -> None: 

38 self._document = TeamMemberDocument( 

39 meta=Meta(count=result.count, offset=result.offset, limit=result.limit), 

40 data=[], 

41 ) 

42 async for member in result.iterator: 

43 team_member_document = TeamMemberDocument.create(TeamMember(member=member)) 

44 self._document.merge(team_member_document) 

45 

46 

47class JsonApiTeamMembersPresenter( 

48 JsonApiPresenter[TeamMemberDocument], Presenter[TeamEntity] 

49): 

50 """A presenter that transforms team members into a JSON:API document.""" 

51 

52 def present(self, team: TeamEntity) -> None: 

53 self._document = TeamMemberDocument( 

54 meta=Meta(count=len(team.members)), 

55 data=[], 

56 ) 

57 for member in team.members.values(): 

58 team_member_document = TeamMemberDocument.create(member, team) 

59 self._document.merge(team_member_document) 

60 

61 

62class JsonApiTeamMemberPresenter( 

63 JsonApiPresenter[TeamMemberDocument], Presenter[tuple[TeamMember, TeamEntity]] 

64): 

65 """A presenter that transforms a team member into a JSON:API document.""" 

66 

67 def present(self, use_case_result: tuple[TeamMember, TeamEntity]) -> None: 

68 self._document = TeamMemberDocument.create( 

69 use_case_result[0], use_case_result[1] 

70 )