Fakultas Ilmu Komputer UI

Commit a49f119f authored by PRIMO GIANCARLO UNEPUTTY's avatar PRIMO GIANCARLO UNEPUTTY
Browse files

feat: Add exception handling when trying to delete Investasi

parent 7a84e931
#pylint: disable=too-many-ancestors
from rest_framework import generics
from rest_framework import permissions
from rest_framework.exceptions import ValidationError
from djangorestframework_camel_case.render import (CamelCaseJSONRenderer,
CamelCaseBrowsableAPIRenderer)
from djangorestframework_camel_case.parser import (CamelCaseJSONParser,
......@@ -50,11 +50,17 @@ class InvestasiDetail(generics.RetrieveUpdateDestroyAPIView):
return Investasi.objects.filter(investor=self.request.user)
def get_serializer_class(self):
if self.request.method == "POST" or self.request.method == "PUT":
if self.request.method in ["PATCH", "PUT", "DELETE"]:
return InvestasiSerializer
return InvestasiReadSerializer
serializer_class = InvestasiReadSerializer
def perform_destroy(self, instance):
if instance.status_investasi == DIMILIKI:
instance.delete()
else:
error_msg = {'error': "Tidak dapat menghapus investasi yang sedang dijual"}
raise ValidationError(error_msg)
permission_classes = [permissions.IsAuthenticated, IsOwner]
parser_classes = (CamelCaseJSONParser, CamelCaseFormParser, CamelCaseMultiPartParser, )
renderer_classes = (CamelCaseJSONRenderer, CamelCaseBrowsableAPIRenderer, )
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment