Coverage for kwai/api/v1/trainings/schemas/training.py: 92%

83 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-05 17:55 +0000

1"""Schemas for training(s).""" 

2 

3from pydantic import BaseModel 

4 

5from kwai.api.converter import MarkdownConverter 

6from kwai.api.v1.trainings.schemas.training_definition import TrainingDefinitionResource 

7from kwai.core import json_api 

8from kwai.modules.training.coaches.coach import CoachEntity 

9from kwai.modules.training.teams.team import TeamEntity 

10from kwai.modules.training.trainings.training import TrainingEntity 

11 

12 

13class TrainingContent(BaseModel): 

14 """Schema for the content of a training.""" 

15 

16 locale: str 

17 format: str 

18 title: str 

19 summary: str 

20 content: str | None 

21 html_summary: str | None 

22 html_content: str | None 

23 

24 

25class TrainingCoach(BaseModel): 

26 """Schema for coach/training specific information.""" 

27 

28 id: str 

29 head: bool 

30 present: bool 

31 payed: bool 

32 

33 

34class TrainingEvent(BaseModel): 

35 """Schema for the event information of a training.""" 

36 

37 start_date: str 

38 end_date: str 

39 location: str 

40 cancelled: bool 

41 active: bool 

42 

43 

44@json_api.resource(type_="training_coaches") 

45class CoachResource: 

46 """Represent a coach attached to a training.""" 

47 

48 def __init__(self, coach: CoachEntity): 

49 self._coach = coach 

50 

51 @json_api.id 

52 def get_id(self) -> str: 

53 """Get the id of the coach.""" 

54 return str(self._coach.id) 

55 

56 @json_api.attribute(name="name") 

57 def get_name(self) -> str: 

58 """Get the name of the coach.""" 

59 return str(self._coach.name) 

60 

61 

62@json_api.resource(type_="teams") 

63class TeamResource: 

64 """Represent a team.""" 

65 

66 def __init__(self, team: TeamEntity): 

67 self._team = team 

68 

69 @json_api.id 

70 def get_id(self) -> str: 

71 """Return the id of the team.""" 

72 return str(self._team.id) 

73 

74 @json_api.attribute(name="name") 

75 def get_name(self) -> str: 

76 """Return the name of the team.""" 

77 return self._team.name 

78 

79 

80@json_api.resource(type_="trainings") 

81class TrainingResource: 

82 """Represent a JSON:API resource for a training entity.""" 

83 

84 def __init__(self, training: TrainingEntity): 

85 """Initialize a training resource. 

86 

87 Args: 

88 training: The training entity that is transformed into a JSON:API resource. 

89 """ 

90 self._training = training 

91 

92 @json_api.id 

93 def get_id(self) -> str: 

94 """Get the id of the training.""" 

95 return str(self._training.id) 

96 

97 @json_api.attribute(name="contents") 

98 def get_contents(self) -> list[TrainingContent]: 

99 """Get the content of the training.""" 

100 return [ 

101 TrainingContent( 

102 locale=content.locale, 

103 format=content.format, 

104 title=content.title, 

105 summary=content.summary, 

106 content=content.content, 

107 html_summary=MarkdownConverter().convert(content.summary), 

108 html_content=None 

109 if content.content is None 

110 else MarkdownConverter().convert(content.content), 

111 ) 

112 for content in self._training.content 

113 ] 

114 

115 @json_api.attribute(name="event") 

116 def get_event(self) -> TrainingEvent: 

117 """Get the event information from a training.""" 

118 return TrainingEvent( 

119 start_date=str(self._training.period.start_date), 

120 end_date=str(self._training.period.end_date), 

121 location=self._training.location or "", 

122 cancelled=self._training.cancelled, 

123 active=self._training.active, 

124 ) 

125 

126 @json_api.attribute(name="remark") 

127 def get_remark(self) -> str: 

128 """Get the remark of the training.""" 

129 return self._training.remark or "" 

130 

131 @json_api.attribute(name="coaches") 

132 def get_training_coaches(self) -> list[TrainingCoach]: 

133 """Get a list with coach data.""" 

134 return [ 

135 TrainingCoach( 

136 id=training_coach.coach.id.value, 

137 head=training_coach.type == 1, 

138 present=training_coach.present, 

139 payed=training_coach.payed, 

140 ) 

141 for training_coach in self._training.coaches 

142 ] 

143 

144 @json_api.relationship(name="coaches") 

145 def get_coaches(self) -> list[CoachResource]: 

146 """Get the coaches attached to the training.""" 

147 return [ 

148 CoachResource(training_coach.coach) 

149 for training_coach in self._training.coaches 

150 ] 

151 

152 @json_api.relationship(name="teams") 

153 def get_teams(self) -> list[TeamResource]: 

154 """Get the teams of the training.""" 

155 return [TeamResource(team) for team in self._training.teams] 

156 

157 @json_api.relationship(name="definition") 

158 def get_definition(self) -> TrainingDefinitionResource | None: 

159 """Get the related training definition resource.""" 

160 definition = self._training.definition 

161 if definition: 

162 return TrainingDefinitionResource(definition) 

163 return None 

164 

165 @json_api.attribute(name="created_at") 

166 def get_created_at(self) -> str | None: 

167 """Get the timestamp of creation.""" 

168 return str(self._training.traceable_time.created_at) 

169 

170 @json_api.attribute(name="updated_at") 

171 def get_updated_at(self) -> str | None: 

172 """Get the timestamp of the last update.""" 

173 return str(self._training.traceable_time.updated_at)