Coverage for apps/kwai-api/src/kwai_api/v1/trainings/coaches/presenters.py: 100%

18 statements  

« 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.""" 

2 

3from typing import Self 

4 

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 

9 

10from kwai_api.v1.trainings.coaches.schemas import ( 

11 CoachAttributes, 

12 CoachesDocument, 

13 CoachResource, 

14 TrainingCoachAttributes, 

15 TrainingCoachesDocument, 

16 TrainingCoachResource, 

17) 

18 

19 

20class JsonApiCoachesPresenter( 

21 JsonApiPresenter[CoachesDocument], AsyncPresenter[IterableResult[CoachEntity]] 

22): 

23 """A presenter that transforms an iterable of coaches into a JSON:API document.""" 

24 

25 async def present(self, result: IterableResult[CoachEntity]) -> Self: 

26 self._document = CoachesDocument( 

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

28 ) 

29 async for coach in result.iterator: 

30 self._document.data.append( 

31 CoachResource( 

32 id=str(coach.id), attributes=CoachAttributes(name=str(coach.name)) 

33 ) 

34 ) 

35 return self 

36 

37 

38class JsonApiTrainingCoachesPresenter( 

39 JsonApiPresenter[TrainingCoachesDocument], 

40 Presenter[TrainingEntity], 

41): 

42 """A presenter that transforms an iterable of coaches of a training into a JSON:API document.""" 

43 

44 def present(self, result: TrainingEntity) -> Self: 

45 self._document = TrainingCoachesDocument(meta=Meta(count=len(result.coaches))) 

46 for training_coach in result.coaches: 

47 self._document.data.append( 

48 TrainingCoachResource( 

49 id=str(training_coach.coach.id), 

50 attributes=TrainingCoachAttributes( 

51 name=str(training_coach.coach.name), 

52 payed=training_coach.payed, 

53 present=training_coach.present, 

54 head=training_coach.type == 1, 

55 remark=training_coach.remark, 

56 ), 

57 ) 

58 ) 

59 return self