@@ -245,3 +245,64 @@ def post(self, request):
245
245
{"message" : f"Member { member_id } removed from cohort { cohort_id } " },
246
246
status = status .HTTP_200_OK ,
247
247
)
248
+
249
+
250
+ class CohortTransferView (APIView ):
251
+ permission_classes = [IsAdmin ]
252
+
253
+ def post (self , request ):
254
+ """
255
+ - Transfer a member from one cohort to another
256
+ - Update stats for that member accordingly
257
+ """
258
+ try :
259
+ member_id = request .data .get ("member_id" )
260
+ from_cohort_id = request .data .get ("from_cohort_id" )
261
+ to_cohort_id = request .data .get ("to_cohort_id" )
262
+ except KeyError :
263
+ return Response (
264
+ {
265
+ "error" : "Please provide 'member_id', 'from_cohort_id', and 'to_cohort_id'"
266
+ },
267
+ status = status .HTTP_400_BAD_REQUEST ,
268
+ )
269
+
270
+ try :
271
+ member = User .objects .get (pk = member_id )
272
+ except User .DoesNotExist :
273
+ return Response (
274
+ {"error" : f"No user found with id: { member_id } " },
275
+ status = status .HTTP_404_NOT_FOUND ,
276
+ )
277
+
278
+ try :
279
+ from_cohort = Cohort .objects .get (pk = from_cohort_id )
280
+ except Cohort .DoesNotExist :
281
+ return Response (
282
+ {"error" : f"No cohort found with id: { from_cohort_id } " },
283
+ status = status .HTTP_404_NOT_FOUND ,
284
+ )
285
+
286
+ try :
287
+ to_cohort = Cohort .objects .get (pk = to_cohort_id )
288
+ except Cohort .DoesNotExist :
289
+ return Response (
290
+ {"error" : f"No cohort found with id: { to_cohort_id } " },
291
+ status = status .HTTP_404_NOT_FOUND ,
292
+ )
293
+
294
+ from_cohort .members .remove (member )
295
+ to_cohort .members .add (member )
296
+
297
+ associated_cohort_stats = CohortStats .objects .filter (
298
+ member = member , cohort = from_cohort
299
+ ).first ()
300
+ associated_cohort_stats .cohort = to_cohort
301
+ associated_cohort_stats .save ()
302
+
303
+ return Response (
304
+ {
305
+ "message" : f"Member { member_id } transferred from cohort { from_cohort_id } to cohort { to_cohort_id } "
306
+ },
307
+ status = status .HTTP_200_OK ,
308
+ )
0 commit comments