Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

projects that linked to programs aren't shown in projects/all #436

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading