Skip to content

Commit

Permalink
Combine club admin and superuser viewsets for ownership requests
Browse files Browse the repository at this point in the history
  • Loading branch information
gabeweng committed Oct 18, 2024
1 parent f36d1f9 commit c5b6c4c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion backend/clubs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class OwnershipRequestAdmin(admin.ModelAdmin):
"club__name",
"created_at",
)
list_display = ("person", "club", "email", "withdrawn", "created_at")
list_display = ("requester", "club", "email", "withdrawn", "created_at")
list_filter = ("withdrawn",)

def person(self, obj):
Expand Down
14 changes: 4 additions & 10 deletions backend/clubs/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
MemberViewSet,
NoteViewSet,
OptionListView,
OwnershipRequestOwnerViewSet,
OwnershipRequestSuperuserAPIView,
OwnershipRequestManagementViewSet,
OwnershipRequestViewSet,
QuestionAnswerViewSet,
ReportViewSet,
Expand Down Expand Up @@ -73,9 +72,9 @@
router.register(r"clubvisits", ClubVisitViewSet, basename="clubvisits")
router.register(r"searches", SearchQueryViewSet, basename="searches")
router.register(r"memberships", MembershipViewSet, basename="members")
router.register(r"requests", MembershipRequestViewSet, basename="requests")
router.register(r"requests/membership", MembershipRequestViewSet, basename="requests")
router.register(
r"ownershiprequests", OwnershipRequestViewSet, basename="ownershiprequests"
r"requests/ownership", OwnershipRequestViewSet, basename="ownershiprequests"
)
router.register(r"tickets", TicketViewSet, basename="tickets")

Expand Down Expand Up @@ -116,7 +115,7 @@
)
clubs_router.register(
r"ownershiprequests",
OwnershipRequestOwnerViewSet,
OwnershipRequestManagementViewSet,
basename="club-ownership-requests",
)
clubs_router.register(r"advisors", AdvisorViewSet, basename="club-advisors")
Expand Down Expand Up @@ -176,11 +175,6 @@
path(r"emailpreview/", email_preview, name="email-preview"),
path(r"scripts/", ScriptExecutionView.as_view(), name="scripts"),
path(r"options/", OptionListView.as_view(), name="options"),
path(
r"ownershiprequestsadmin/",
OwnershipRequestSuperuserAPIView.as_view(),
name="ownershiprequestsadmin",
),
path(r"social/", include("social_django.urls", namespace="social")),
path(
r"webhook/meeting/",
Expand Down
40 changes: 23 additions & 17 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3863,27 +3863,37 @@ def get_queryset(self):
)


class OwnershipRequestOwnerViewSet(viewsets.ModelViewSet):
class OwnershipRequestManagementViewSet(viewsets.ModelViewSet):
"""
list:
Return a list of users who have sent ownership request to the club.
destroy:
Delete a ownership request for a specific user.
accept:
Accept an ownership request as a club owner.
old_requests:
Return a list of ownership requests older than a week. Used by Superusers.
"""

serializer_class = OwnershipRequestSerializer

permission_classes = [OwnershipRequestPermission | IsSuperuser]
http_method_names = ["get", "post", "delete"]
lookup_field = "requester__username"

def get_queryset(self):
return OwnershipRequest.objects.filter(
club__code=self.kwargs["club_code"], withdrawn=False
)
if self.action != "old_requests":
return OwnershipRequest.objects.filter(
club__code=self.kwargs["club_code"], withdrawn=False
)
else:
return OwnershipRequest.objects.filter(withdrawn=False)

@action(detail=True, methods=["post"])
def accept(self, request, *ages, **kwargs):
def accept(self, request, *args, **kwargs):
"""
Accept an ownership request as a club owner.
---
Expand All @@ -3903,7 +3913,7 @@ def accept(self, request, *ages, **kwargs):
"""
request_object = self.get_object()
membership, created = Membership.objects.get_or_create(
requester=request_object.requester,
person=request_object.requester,
club=request_object.club,
defaults={"role": Membership.ROLE_OWNER},
)
Expand All @@ -3915,20 +3925,16 @@ def accept(self, request, *ages, **kwargs):
request_object.delete()
return Response({"success": True})


class OwnershipRequestSuperuserAPIView(generics.ListAPIView):
"""
Return a list of ownership requests older than a week.
"""

serializer_class = OwnershipRequestSerializer
permission_classes = [IsSuperuser]

def get_queryset(self):
return OwnershipRequest.objects.filter(
@action(detail=False, methods=["get"], permission_classes=[IsSuperuser])
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)

return Response(serializer.data)


class MemberViewSet(XLSXFormatterMixin, viewsets.ModelViewSet):
"""
Expand Down

0 comments on commit c5b6c4c

Please sign in to comment.