Skip to content

Commit 42a05d4

Browse files
committed
fixed endpoint to delete collab
1 parent a3535be commit 42a05d4

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
@@ -16,7 +16,6 @@
1616
ProjectUnsubscribe,
1717
ProjectSubscribers,
1818
LeaveProject,
19-
DeleteProjectCollaborators,
2019
SwitchLeaderRole,
2120
)
2221

@@ -34,7 +33,6 @@
3433
path("<int:project_pk>/news/<int:pk>/set_liked/", NewsDetailSetLiked.as_view()),
3534
path("<int:pk>/collaborators/", ProjectCollaborators.as_view()),
3635
path("<int:project_pk>/collaborators/leave/", LeaveProject.as_view()),
37-
path("<int:project_pk>/collaborators/kick/", DeleteProjectCollaborators.as_view()),
3836
path(
3937
"<int:project_pk>/collaborators/<int:user_to_leader_pk>/switch-leader/",
4038
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):
@@ -492,42 +525,38 @@ def _project_data(
492525
return project.id, project.leader.id
493526

494527
@staticmethod
495-
def _collabs_queryset(
496-
project_id: int, requested_ids: set[int], leader_id: int
497-
) -> QuerySet:
498-
return (
499-
Collaborator.objects.filter(
500-
user__id__in=requested_ids, project__id=project_id
501-
)
502-
.exclude(user__id=leader_id) # чтоб случайно лидер сам себя не удалил
503-
.values_list("id", flat=True)
528+
def _collabs_queryset(project_id: int, requested_id: int, leader_id: int) -> QuerySet:
529+
return Collaborator.objects.exclude(
530+
user__id=leader_id
531+
).get( # чтоб случайно лидер сам себя не удалил
532+
user__id=requested_id, project__id=project_id
504533
)
505534

506535
def delete(self, request, project_pk: int) -> Response:
507-
requested_collabs_ids: set[int] = set(request.data)
536+
requested_collab_id: int = int(self.request.query_params.get("id"))
508537

509538
project_id, leader_id = self._project_data(project_pk)
510-
existing_collabs_ids: set[int] = set(
511-
self._collabs_queryset(project_id, requested_collabs_ids, leader_id)
539+
existing_collab_id = self._collabs_queryset(
540+
project_id, requested_collab_id, leader_id
512541
)
513542

514-
if leader_id in requested_collabs_ids:
543+
if leader_id == requested_collab_id:
515544
return Response(
516-
{"error": f"User with id: {leader_id} is a leader of a project."},
545+
{
546+
"error": f"User with id: {leader_id} is a leader of a project. "
547+
f"Be careful not to delete yourself from a project!"
548+
},
517549
status=status.HTTP_422_UNPROCESSABLE_ENTITY,
518550
)
519-
if unexisting_collabs := requested_collabs_ids - existing_collabs_ids:
520-
raise ValueError(
521-
unexisting_collabs, ".", requested_collabs_ids, ".", existing_collabs_ids
522-
)
551+
if not existing_collab_id:
523552
return Response(
524553
{
525-
"error": f"Users with ids: {list(unexisting_collabs)} are not part of this project."
554+
"error": f"User with id: {requested_collab_id} are not part of this project."
526555
},
527556
status=status.HTTP_422_UNPROCESSABLE_ENTITY,
528557
)
529558

530-
Collaborator.objects.filter(id__in=existing_collabs_ids).delete()
559+
existing_collab_id.delete()
531560
return Response(status=204)
532561

533562

0 commit comments

Comments
 (0)