Skip to content

Commit

Permalink
Merge pull request #436 from PROCOLLAB-github/flexivanov237-pro-451
Browse files Browse the repository at this point in the history
projects that linked to programs aren't shown in projects/all
  • Loading branch information
sh1nkey authored Sep 18, 2024
2 parents 3583f9f + c82cfd2 commit 4aef521
Showing 1 changed file with 40 additions and 32 deletions.
72 changes: 40 additions & 32 deletions projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]

Expand Down

0 comments on commit 4aef521

Please sign in to comment.