Skip to content

Commit b327947

Browse files
authored
Merge pull request #401 from PROCOLLAB-github/fix/delete-collabs
fixed endpoint to delete collab
2 parents a65fe24 + 42a05d4 commit b327947

2 files changed

Lines changed: 58 additions & 31 deletions

File tree

projects/urls.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
ProjectSubscribers,
1818
SwitchLeaderRole,
1919
LeaveProject,
20-
DeleteProjectCollaborators,
2120
SwitchLeaderRole,
2221
)
2322

@@ -35,7 +34,6 @@
3534
path("<int:project_pk>/news/<int:pk>/set_liked/", NewsDetailSetLiked.as_view()),
3635
path("<int:pk>/collaborators/", ProjectCollaborators.as_view()),
3736
path("<int:project_pk>/collaborators/leave/", LeaveProject.as_view()),
38-
path("<int:project_pk>/collaborators/kick/", DeleteProjectCollaborators.as_view()),
3937
path(
4038
"<int:project_pk>/collaborators/<int:user_to_leader_pk>/switch-leader/",
4139
SwitchLeaderRole.as_view(),

projects/views.py

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -251,15 +251,48 @@ def post(self, request, pk: int):
251251
return Response(status=200)
252252

253253
def delete(self, request, pk: int):
254-
"""delete collaborators from the project"""
255-
m2m_manager = self.get_object().collaborators
256-
serializer = self.get_serializer(data=request.data)
257-
serializer.is_valid(raise_exception=True)
258-
collaborators = serializer.validated_data["collaborators"]
259-
for user in collaborators:
260-
# note: doesn't raise an error when we try to delete someone who isn't a collaborator
261-
m2m_manager.remove(user)
262-
return Response(status=200)
254+
"""delete collaborator from project"""
255+
requested_collab_id: int = int(self.request.query_params.get("id"))
256+
257+
project_id, leader_id = self._project_data(pk)
258+
existing_collab_id = self._collabs_queryset(
259+
project_id, requested_collab_id, leader_id
260+
)
261+
262+
if leader_id == requested_collab_id:
263+
return Response(
264+
{
265+
"error": f"User with id: {leader_id} is a leader of a project. "
266+
f"Be careful not to delete yourself from a project!"
267+
},
268+
status=status.HTTP_422_UNPROCESSABLE_ENTITY,
269+
)
270+
if not existing_collab_id:
271+
return Response(
272+
{
273+
"error": f"User with id: {requested_collab_id} are not part of this project."
274+
},
275+
status=status.HTTP_422_UNPROCESSABLE_ENTITY,
276+
)
277+
278+
existing_collab_id.delete()
279+
return Response(status=204)
280+
281+
def _project_data(
282+
self, project_pk: int
283+
) -> tuple[Annotated[int, "ID проекта"], Annotated[int, "ID лидера проекта"]]:
284+
project = get_object_or_404(
285+
Project.objects.select_related("leader"), id=project_pk
286+
)
287+
return project.id, project.leader.id
288+
289+
@staticmethod
290+
def _collabs_queryset(project_id: int, requested_id: int, leader_id: int) -> QuerySet:
291+
return Collaborator.objects.exclude(
292+
user__id=leader_id
293+
).get( # чтоб случайно лидер сам себя не удалил
294+
user__id=requested_id, project__id=project_id
295+
)
263296

264297

265298
class ProjectSteps(APIView):
@@ -525,42 +558,38 @@ def _project_data(
525558
return project.id, project.leader.id
526559

527560
@staticmethod
528-
def _collabs_queryset(
529-
project_id: int, requested_ids: set[int], leader_id: int
530-
) -> QuerySet:
531-
return (
532-
Collaborator.objects.filter(
533-
user__id__in=requested_ids, project__id=project_id
534-
)
535-
.exclude(user__id=leader_id) # чтоб случайно лидер сам себя не удалил
536-
.values_list("id", flat=True)
561+
def _collabs_queryset(project_id: int, requested_id: int, leader_id: int) -> QuerySet:
562+
return Collaborator.objects.exclude(
563+
user__id=leader_id
564+
).get( # чтоб случайно лидер сам себя не удалил
565+
user__id=requested_id, project__id=project_id
537566
)
538567

539568
def delete(self, request, project_pk: int) -> Response:
540-
requested_collabs_ids: set[int] = set(request.data)
569+
requested_collab_id: int = int(self.request.query_params.get("id"))
541570

542571
project_id, leader_id = self._project_data(project_pk)
543-
existing_collabs_ids: set[int] = set(
544-
self._collabs_queryset(project_id, requested_collabs_ids, leader_id)
572+
existing_collab_id = self._collabs_queryset(
573+
project_id, requested_collab_id, leader_id
545574
)
546575

547-
if leader_id in requested_collabs_ids:
576+
if leader_id == requested_collab_id:
548577
return Response(
549-
{"error": f"User with id: {leader_id} is a leader of a project."},
578+
{
579+
"error": f"User with id: {leader_id} is a leader of a project. "
580+
f"Be careful not to delete yourself from a project!"
581+
},
550582
status=status.HTTP_422_UNPROCESSABLE_ENTITY,
551583
)
552-
if unexisting_collabs := requested_collabs_ids - existing_collabs_ids:
553-
raise ValueError(
554-
unexisting_collabs, ".", requested_collabs_ids, ".", existing_collabs_ids
555-
)
584+
if not existing_collab_id:
556585
return Response(
557586
{
558-
"error": f"Users with ids: {list(unexisting_collabs)} are not part of this project."
587+
"error": f"User with id: {requested_collab_id} are not part of this project."
559588
},
560589
status=status.HTTP_422_UNPROCESSABLE_ENTITY,
561590
)
562591

563-
Collaborator.objects.filter(id__in=existing_collabs_ids).delete()
592+
existing_collab_id.delete()
564593
return Response(status=204)
565594

566595

0 commit comments

Comments
 (0)