Skip to content

Commit 40a80da

Browse files
authored
Merge pull request #136 from swecc-uw/transfer-cohort
Add route to transfer user to other cohort
2 parents 4556292 + cea30f0 commit 40a80da

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

server/cohort/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
path(
1313
"remove/", views.CohortRemoveMemberView.as_view(), name="cohort-remove-member"
1414
),
15+
path("transfer/", views.CohortTransferView.as_view(), name="cohort-transfer"),
1516
]

server/cohort/views.py

+61
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,64 @@ def post(self, request):
245245
{"message": f"Member {member_id} removed from cohort {cohort_id}"},
246246
status=status.HTTP_200_OK,
247247
)
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

Comments
 (0)