|
1 | | -from rest_framework.permissions import IsAuthenticated |
2 | 1 | from rest_framework.viewsets import ModelViewSet |
3 | 2 | from rest_framework.response import Response |
4 | | -from api.permissions import LeaderboardNotHidden, LeaderboardIsOrganizerOrCollaborator |
| 3 | +from rest_framework.status import HTTP_405_METHOD_NOT_ALLOWED |
| 4 | +from api.permissions import LeaderboardNotHidden |
5 | 5 | from api.serializers.leaderboards import LeaderboardEntriesSerializer |
6 | 6 | from api.serializers.submissions import SubmissionScoreSerializer |
7 | 7 | from leaderboards.models import Leaderboard, SubmissionScore |
|
10 | 10 | class LeaderboardViewSet(ModelViewSet): |
11 | 11 | queryset = Leaderboard.objects.all() |
12 | 12 | serializer_class = LeaderboardEntriesSerializer |
| 13 | + http_method_names = ['get'] # Only allow GET requests |
13 | 14 |
|
14 | | - # TODO: The retrieve and list actions are the only ones used, apparently. Delete other permission checks soon! |
15 | | - def get_permissions(self): |
16 | | - if self.action in ['update', 'partial_update', 'destroy']: |
17 | | - raise Exception('Unexpected code branch execution.') |
18 | | - self.permission_classes = [LeaderboardIsOrganizerOrCollaborator] |
19 | | - elif self.action in ['create']: |
20 | | - raise Exception('Unexpected code branch execution.') |
21 | | - self.permission_classes = [IsAuthenticated] |
22 | | - elif self.action in ['retrieve', 'list']: |
23 | | - self.permission_classes = [LeaderboardNotHidden] |
| 15 | + def create(self, request, *args, **kwargs): |
| 16 | + return Response({'detail': 'Method not allowed.'}, status=HTTP_405_METHOD_NOT_ALLOWED) |
24 | 17 |
|
25 | | - return [permission() for permission in self.permission_classes] |
| 18 | + def update(self, request, *args, **kwargs): |
| 19 | + return Response({'detail': 'Method not allowed.'}, status=HTTP_405_METHOD_NOT_ALLOWED) |
| 20 | + |
| 21 | + def partial_update(self, request, *args, **kwargs): |
| 22 | + return Response({'detail': 'Method not allowed.'}, status=HTTP_405_METHOD_NOT_ALLOWED) |
| 23 | + |
| 24 | + def destroy(self, request, *args, **kwargs): |
| 25 | + return Response({'detail': 'Method not allowed.'}, status=HTTP_405_METHOD_NOT_ALLOWED) |
26 | 26 |
|
27 | 27 | def list(self, request, *args, **kwargs): |
28 | 28 | # Return an empty list for the leaderboard-list endpoint |
29 | 29 | return Response([]) |
30 | 30 |
|
| 31 | + def get_permissions(self): |
| 32 | + if self.action in ['create', 'update', 'partial_update', 'destroy']: |
| 33 | + return [] # No permissions, effectively disables the action |
| 34 | + elif self.action in ['retrieve', 'list']: |
| 35 | + self.permission_classes = [LeaderboardNotHidden] |
| 36 | + |
| 37 | + return [permission() for permission in self.permission_classes] |
| 38 | + |
31 | 39 |
|
32 | 40 | class SubmissionScoreViewSet(ModelViewSet): |
33 | 41 | queryset = SubmissionScore.objects.all() |
|
0 commit comments