Skip to content

Commit

Permalink
get object or 404 (instead of 500 DoesNotExist)
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonejt committed Jul 4, 2023
1 parent df379c0 commit 9ebb215
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
13 changes: 7 additions & 6 deletions community/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand All @@ -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")
Expand All @@ -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)
37 changes: 19 additions & 18 deletions dominator/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
37 changes: 19 additions & 18 deletions psychopass/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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()

Expand Down

0 comments on commit 9ebb215

Please sign in to comment.