From bf105d4ba73baad508dcdcdd017cc501694b5b33 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Fri, 22 Nov 2024 18:04:20 -0500 Subject: [PATCH] Change old-requests to all-requests --- backend/clubs/views.py | 16 +++---- backend/tests/clubs/test_views.py | 79 ++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 37 deletions(-) diff --git a/backend/clubs/views.py b/backend/clubs/views.py index 0a1bd6457..6fbc91e06 100644 --- a/backend/clubs/views.py +++ b/backend/clubs/views.py @@ -3954,12 +3954,14 @@ class OwnershipRequestManagementViewSet(viewsets.ModelViewSet): lookup_field = "requester__username" def get_queryset(self): - if self.action != "old_requests": + if self.action != "all_requests": return OwnershipRequest.objects.filter( club__code=self.kwargs["club_code"], withdrawn=False ) else: - return OwnershipRequest.objects.filter(withdrawn=False) + return OwnershipRequest.objects.filter(withdrawn=False).order_by( + "created_at" + ) @action(detail=True, methods=["post"]) def accept(self, request, *args, **kwargs): @@ -3995,9 +3997,9 @@ def accept(self, request, *args, **kwargs): return Response({"success": True}) @action(detail=False, methods=["get"], permission_classes=[IsSuperuser]) - def old_requests(self, request, *args, **kwargs): + def all_requests(self, request, *args, **kwargs): """ - View unaddressed ownership requests that are older than a week. + View unaddressed ownership requests, sorted by date. --- requestBody: {} responses: @@ -4045,11 +4047,7 @@ def old_requests(self, request, *args, **kwargs): --- """ - queryset = OwnershipRequest.objects.filter( - withdrawn=False, created_at__lte=timezone.now() - datetime.timedelta(days=7) - ) - - serializer = self.get_serializer(queryset, many=True) + serializer = self.get_serializer(self.get_queryset(), many=True) return Response(serializer.data) diff --git a/backend/tests/clubs/test_views.py b/backend/tests/clubs/test_views.py index 979261f92..bb571cb64 100644 --- a/backend/tests/clubs/test_views.py +++ b/backend/tests/clubs/test_views.py @@ -3003,25 +3003,6 @@ def test_ownership_requests_create_and_view(self): self.assertEqual(resp.status_code, 200, resp.content) self.assertEqual(resp.json()["club"], self.club1.code, resp.content) - # Superuser should only see old requests through old-requests - self.client.login(username=self.user5.username, password="test") - resp = self.client.get( - reverse("club-ownership-requests-old-requests", args=("anystring",)) - ) - self.assertEqual(resp.status_code, 200, resp.content) - self.assertEqual(len(resp.json()), 0, resp.content) - - OwnershipRequest.objects.filter(club=self.club1, requester=self.user2).update( - created_at=timezone.now() - timezone.timedelta(days=8) - ) - - self.client.login(username=self.user5.username, password="test") - resp = self.client.get( - reverse("club-ownership-requests-old-requests", args=("anystring",)) - ) - self.assertEqual(resp.status_code, 200, resp.content) - self.assertEqual(len(resp.json()), 1, resp.content) - def test_ownership_requests_withdraw(self): """ Test the ownership requests withdraw feature @@ -3098,15 +3079,6 @@ def test_ownership_requests_withdraw(self): self.assertEqual(resp.status_code, 200, resp.content) self.assertEqual(len(resp.json()), 1, resp.content) - # Recreated ownership requests are not old - - self.client.login(username=self.user5.username, password="test") - resp = self.client.get( - reverse("club-ownership-requests-old-requests", args=("anystring",)) - ) - self.assertEqual(resp.status_code, 200, resp.content) - self.assertEqual(len(resp.json()), 0, resp.content) - def test_ownership_requests_accept(self): """ Test the ownership requests accept feature @@ -3221,6 +3193,57 @@ def test_ownership_requests_destroy(self): 0, ) + def test_ownership_requests_list_all_requests(self): + """ + Test the ownership requests list all requests feature + """ + + self.client.login(username=self.user5.username, password="test") + resp = self.client.get( + reverse("club-ownership-requests-all-requests", args=("anystring",)) + ) + self.assertEqual(resp.status_code, 200, resp.content) + self.assertEqual(len(resp.json()), 0, resp.content) + + OwnershipRequest.objects.create( + club=self.club1, + requester=self.user2, + ) + OwnershipRequest.objects.filter( + club=self.club1, + requester=self.user2, + ).update(created_at=timezone.now() - timezone.timedelta(days=1)) + + OwnershipRequest.objects.create( + club=self.club1, + requester=self.user3, + ) + OwnershipRequest.objects.filter( + club=self.club1, + requester=self.user3, + ).update(created_at=timezone.now() - timezone.timedelta(days=100)) + + OwnershipRequest.objects.create( + club=self.club1, + requester=self.user4, + ) + OwnershipRequest.objects.filter( + club=self.club1, + requester=self.user4, + ).update(created_at=timezone.now() - timezone.timedelta(days=10)) + + self.client.login(username=self.user5.username, password="test") + resp = self.client.get( + reverse("club-ownership-requests-all-requests", args=("anystring",)) + ) + self.assertEqual(resp.status_code, 200, resp.content) + self.assertEqual(len(resp.json()), 3, resp.content) + + # Check oldest requests first + self.assertEqual(resp.json()[0]["username"], self.user3.username, resp.content) + self.assertEqual(resp.json()[1]["username"], self.user4.username, resp.content) + self.assertEqual(resp.json()[2]["username"], self.user2.username, resp.content) + def test_membership_requests_create_and_view(self): """ Test the membership requests creation and viewing permissions