From c82cfd2941ce959181c3d7043da9a35ab7b46691 Mon Sep 17 00:00:00 2001 From: Alexey Kudelko Date: Wed, 18 Sep 2024 14:45:58 +0300 Subject: [PATCH] projects that linked to programs aren't shown in projects/all --- projects/views.py | 72 ++++++++++++++++++++++++++--------------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/projects/views.py b/projects/views.py index 82670c59..d6be9244 100644 --- a/projects/views.py +++ b/projects/views.py @@ -55,13 +55,22 @@ class ProjectList(generics.ListCreateAPIView): - queryset = Project.objects.get_projects_for_list_view() serializer_class = ProjectListSerializer permission_classes = [IsAuthenticated, permissions.IsAuthenticatedOrReadOnly] filter_backends = (filters.DjangoFilterBackend,) filterset_class = ProjectFilter pagination_class = ProjectsPagination + def get_queryset(self) -> QuerySet[Project]: + queryset = Project.objects.get_projects_for_list_view() + is_program_needed = self.request.query_params.get("partner_program", None) + if not is_program_needed: + queryset_without_projects_linked_to_programs = queryset.filter( + partner_program_profiles__isnull=True + ) + return queryset_without_projects_linked_to_programs + return queryset + def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) @@ -500,39 +509,38 @@ def post(self, request, project_pk): ) +# class SwitchLeaderRole(generics.GenericAPIView): +# permission_classes = [IsProjectLeader] +# queryset = Project.objects.all().select_related("leader") +# +# def _get_new_leader(self, user_id: int, project: Project) -> Collaborator: +# try: +# return Collaborator.objects.select_related("user").get( +# user_id=user_id, project=project +# ) +# except ObjectDoesNotExist: +# raise CollaboratorDoesNotExist( +# f"""Collaborator with user_id: {user_id} does not exist. Either user_id is not correct, or project_id +# is not correct, or try adding this user to a project (as collaborator) before making them a leader. """ +# ) +# +# def patch(self, request, pk: int): +# project = self.get_object() +# +# new_leader_id = int(request.data["new_leader_id"]) +# new_leader = self._get_new_leader(new_leader_id, project) +# +# if project.leader.id == new_leader_id: +# return Response( +# {"error": "User is already a leader of a project"}, +# status=status.HTTP_422_UNPROCESSABLE_ENTITY, +# ) +# +# project.leader = new_leader.user +# project.save() +# return Response(status=204) -class SwitchLeaderRole(generics.GenericAPIView): - permission_classes = [IsProjectLeader] - queryset = Project.objects.all().select_related("leader") - - def _get_new_leader(self, user_id: int, project: Project) -> Collaborator: - try: - return Collaborator.objects.select_related("user").get( - user_id=user_id, project=project - ) - except ObjectDoesNotExist: - raise CollaboratorDoesNotExist( - f"""Collaborator with user_id: {user_id} does not exist. Either user_id is not correct, or project_id - is not correct, or try adding this user to a project (as collaborator) before making them a leader. """ - ) - - def patch(self, request, pk: int): - project = self.get_object() - - new_leader_id = int(request.data["new_leader_id"]) - new_leader = self._get_new_leader(new_leader_id, project) - - if project.leader.id == new_leader_id: - return Response( - {"error": "User is already a leader of a project"}, - status=status.HTTP_422_UNPROCESSABLE_ENTITY, - ) - - project.leader = new_leader.user - project.save() - return Response(status=204) - class LeaveProject(generics.GenericAPIView): permission_classes = [IsAuthenticated]