From 9ebb21526d1edc4ae24c8cea5a59cb52b81a8ba1 Mon Sep 17 00:00:00 2001 From: Evan Tung Date: Mon, 3 Jul 2023 20:37:37 -0700 Subject: [PATCH] get object or 404 (instead of 500 DoesNotExist) --- community/views.py | 13 +++++++------ dominator/views.py | 37 +++++++++++++++++++------------------ psychopass/views.py | 37 +++++++++++++++++++------------------ 3 files changed, 45 insertions(+), 42 deletions(-) diff --git a/community/views.py b/community/views.py index 9ba5a77..f1707d9 100644 --- a/community/views.py +++ b/community/views.py @@ -1,3 +1,4 @@ +from django.shortcuts import get_object_or_404 from rest_framework import status from rest_framework.views import APIView from rest_framework.permissions import IsAdminUser @@ -14,8 +15,8 @@ class CommunityView(APIView): permission_classes = [IsAdminUser] def get(self, request: Request) -> Response: - community = Community.objects.get( - platform=request.user, community_id=request.query_params.get("id")) + community = get_object_or_404( + Community, platform=request.user, community_id=request.query_params.get("id")) return Response(CommunitySerializer(community).data, status=status.HTTP_200_OK) @@ -35,8 +36,8 @@ def post(self, request: Request) -> Response: return Response(CommunitySerializer(community).data, status=status.HTTP_201_CREATED) def put(self, request: Request) -> Response: - community = Community.objects.get( - community_id=request.data.get("communityID")) + community = get_object_or_404( + Community, community_id=request.data.get("communityID")) community_data = request.data.copy() community_data["platform"] = community.platform.id community_data["community_id"] = request.data.get("communityID") @@ -47,8 +48,8 @@ def put(self, request: Request) -> Response: return Response(CommunitySerializer(community).data, status=status.HTTP_202_ACCEPTED) def delete(self, request: Request) -> Response: - community = Community.objects.get( - platform=request.user, community_id=request.query_params.get("id")) + community = get_object_or_404( + Community, platform=request.user, community_id=request.query_params.get("id")) community.delete() return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/dominator/views.py b/dominator/views.py index 2536501..b9b6643 100644 --- a/dominator/views.py +++ b/dominator/views.py @@ -1,3 +1,4 @@ +from django.shortcuts import get_object_or_404 from rest_framework.views import APIView from rest_framework.request import Request from rest_framework.response import Response @@ -13,16 +14,16 @@ class MemberDominatorView(APIView): permission_classes = [IsAdminUser] def get(self, request: Request) -> Response: - community = Community.objects.get( - community_id=request.query_params.get("id")) - dominator = MemberDominator.objects.get(community=community) + community = get_object_or_404( + Community, community_id=request.query_params.get("id")) + dominator = get_object_or_404(MemberDominator, community=community) return Response(MemberDominatorSerializer(dominator).data, status=status.HTTP_200_OK) def put(self, request: Request) -> Response: - community = Community.objects.get( - community_id=request.data.get("communityID")) - dominator = MemberDominator.objects.get(community=community) + community = get_object_or_404( + Community, community_id=request.data.get("communityID")) + dominator = get_object_or_404(MemberDominator, community=community) trigger_data = request.data.copy() trigger_data.pop("communityID") trigger_data["community"] = community.id @@ -34,9 +35,9 @@ def put(self, request: Request) -> Response: return Response(MemberDominatorSerializer(dominator).data, status=status.HTTP_202_ACCEPTED) def delete(self, request: Request) -> Response: - community = Community.objects.get( - community_id=request.query_params.get("id")) - dominator = MemberDominator.objects.get(community=community) + community = get_object_or_404( + Community, community_id=request.query_params.get("id")) + dominator = get_object_or_404(MemberDominator, community=community) dominator.delete() return Response(status=status.HTTP_204_NO_CONTENT) @@ -46,16 +47,16 @@ class MessageDominatorView(APIView): permission_classes = [IsAdminUser] def get(self, request: Request) -> Response: - community = Community.objects.get( - community_id=request.query_params.get("id")) - dominator = MessageDominator.objects.get(community=community) + community = get_object_or_404( + Community, community_id=request.query_params.get("id")) + dominator = get_object_or_404(MessageDominator, community=community) return Response(MessageDominatorSerializer(dominator).data, status=status.HTTP_200_OK) def put(self, request: Request) -> Response: - community = Community.objects.get( - community_id=request.data.get("communityID")) - dominator = MessageDominator.objects.get(community=community) + community = get_object_or_404( + Community, community_id=request.data.get("communityID")) + dominator = get_object_or_404(MessageDominator, community=community) trigger_data = request.data.copy() trigger_data.pop("communityID") trigger_data["community"] = community.id @@ -67,9 +68,9 @@ def put(self, request: Request) -> Response: return Response(MessageDominatorSerializer(dominator).data, status=status.HTTP_202_ACCEPTED) def delete(self, request: Request) -> Response: - community = Community.objects.get( - community_id=request.query_params.get("id")) - dominator = MessageDominator.objects.get(community=community) + community = get_object_or_404( + Community, community_id=request.query_params.get("id")) + dominator = get_object_or_404(MessageDominator, community=community) dominator.delete() return Response(status=status.HTTP_204_NO_CONTENT) diff --git a/psychopass/views.py b/psychopass/views.py index 8a64601..f21d34f 100644 --- a/psychopass/views.py +++ b/psychopass/views.py @@ -1,3 +1,4 @@ +from django.shortcuts import get_object_or_404 from rest_framework.views import APIView from rest_framework.request import Request from rest_framework.response import Response @@ -14,10 +15,10 @@ def ingest_message(request: Request) -> Response: psycho_pass, _ = UserPsychoPass.objects.get_or_create( platform=request.user, user_id=request.data.get("userID")) - community = Community.objects.get( - platform=request.user, community_id=request.data.get("communityID")) - community_psycho_pass = CommunityPsychoPass.objects.get( - community=community) + community = get_object_or_404( + Community, platform=request.user, community_id=request.data.get("communityID")) + community_psycho_pass = get_object_or_404( + CommunityPsychoPass, community=community) psycho_pass.ingest_message(request.data.get("attributeScores")) psycho_pass.save() @@ -34,8 +35,8 @@ class UserPsychoPassView(APIView): permission_classes = [IsAdminUser] def get(self, request: Request) -> Response: - psycho_pass = UserPsychoPass.objects.get( - user_id=request.query_params.get("id")) + psycho_pass = get_object_or_404( + UserPsychoPass, user_id=request.query_params.get("id")) return Response(UserPsychoPassSerializer(psycho_pass).data, status=status.HTTP_200_OK) @@ -47,8 +48,8 @@ def post(self, request: Request) -> Response: return Response(UserPsychoPassSerializer(psycho_pass).data, status=status.HTTP_201_CREATED) def delete(self, request: Request) -> Response: - psycho_pass = UserPsychoPass.objects.get( - user_id=request.query_params.get("id")) + psycho_pass = get_object_or_404( + UserPsychoPass, user_id=request.query_params.get("id")) psycho_pass.delete() return Response(status=status.HTTP_204_NO_CONTENT) @@ -58,20 +59,20 @@ class CommunityPsychoPassView(APIView): permission_classes = [IsAdminUser] def get(self, request: Request) -> Response: - community = Community.objects.get( - platform=request.user, community_id=request.query_params.get("id")) - community_psycho_pass = CommunityPsychoPass.objects.get( - community=community) + community = get_object_or_404( + Community, platform=request.user, community_id=request.query_params.get("id")) + community_psycho_pass = get_object_or_404( + CommunityPsychoPass, community=community) return Response(CommunityPsychoPassSerializer(community_psycho_pass).data, status=status.HTTP_200_OK) def put(self, request: Request) -> Response: - community = Community.objects.get( - platform=request.user, community_id=request.data.get("communityID")) - community_psycho_pass = CommunityPsychoPass.objects.get( - community=community) - psycho_pass = UserPsychoPass.objects.get( - user_id=request.data.get("userID")) + community = get_object_or_404( + Community, platform=request.user, community_id=request.data.get("communityID")) + community_psycho_pass = get_object_or_404( + CommunityPsychoPass, community=community) + psycho_pass = get_object_or_404( + UserPsychoPass, user_id=request.data.get("userID")) community_psycho_pass.users.remove(psycho_pass) community_psycho_pass.save()