Fakultas Ilmu Komputer UI

Commit 6f098821 authored by PRIMO GIANCARLO UNEPUTTY's avatar PRIMO GIANCARLO UNEPUTTY
Browse files

fix: Fix style in views, serializers & tests for Investasi

parent ba048584
......@@ -4,7 +4,6 @@ from toko.serializers import PengadaanSerializer
from .models import Investasi
class InvestasiSerializer(serializers.ModelSerializer):
investor = serializers.ReadOnlyField(source='investor.email')
uang_investasi = serializers.SerializerMethodField('get_investasi_dalam_bentuk_uang')
@classmethod
......@@ -28,3 +27,5 @@ class InvestasiSerializer(serializers.ModelSerializer):
class InvestasiReadSerializer(InvestasiSerializer):
pengadaan = PengadaanSerializer(read_only=True)
investor = serializers.ReadOnlyField(source='investor.email', allow_null=True)
pembeli = serializers.ReadOnlyField(source='pembeli.email', allow_null=True)
......@@ -355,7 +355,7 @@ class InvestasiTest(TestCase):
)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['pembeli'], user.pk)
self.assertEqual(response.json()['pembeli'], user.email)
self.assertEqual(response.json()['statusPembelian'], 'MPA')
def test_cancel_beli_investasi_fail(self):
......
......@@ -17,6 +17,10 @@ class IsOwner(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
return obj.investor == request.user
class IsPembeli(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
return obj.pembeli == request.user
class InvestasiList(generics.ListCreateAPIView):
def get_queryset(self):
return Investasi.objects.filter(investor=self.request.user)
......@@ -75,10 +79,14 @@ class InvestasiDijualDetail(generics.RetrieveAPIView):
class JualInvestasi(generics.UpdateAPIView):
def get_queryset(self):
return Investasi.objects.filter(investor=self.request.user)
return Investasi.objects.filter(investor=self.request.user, status_investasi=DIMILIKI)
def perform_update(self, serializer):
serializer.save(pembeli=None, status_investasi=DIJUAL)
serializer.save(
pembeli=None,
status_pembelian=BELUM_DIBELI,
status_investasi=DIJUAL
)
serializer_class = InvestasiReadSerializer
permission_classes = [permissions.IsAuthenticated, IsOwner]
......@@ -103,7 +111,7 @@ class CancelJualInvestasi(generics.UpdateAPIView):
class BeliInvestasi(generics.UpdateAPIView):
def get_queryset(self):
return Investasi.objects.filter(status_investasi=DIJUAL)
return Investasi.objects.filter(pembeli=None, status_investasi=DIJUAL)
def perform_update(self, serializer):
serializer.save(
......@@ -119,7 +127,10 @@ class BeliInvestasi(generics.UpdateAPIView):
class CancelBeliInvestasi(generics.UpdateAPIView):
def get_queryset(self):
return Investasi.objects.filter(status_investasi=SEDANG_DIBELI)
return Investasi.objects.filter(
pembeli=self.request.user,
status_investasi=SEDANG_DIBELI
)
def perform_update(self, serializer):
serializer.save(
......@@ -129,6 +140,6 @@ class CancelBeliInvestasi(generics.UpdateAPIView):
)
serializer_class = InvestasiReadSerializer
permission_classes = [permissions.IsAuthenticated]
permission_classes = [permissions.IsAuthenticated, IsPembeli]
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