Fakultas Ilmu Komputer UI

Commit 9e458e95 authored by rayhan muzakki's avatar rayhan muzakki
Browse files

fix productdetail in views so that profit is updated for any changes

parent b72011d9
......@@ -1200,7 +1200,8 @@ class ProductTest(rest_framework_test.APITestCase):
))
data = {
'name': 'Dummy',
'price':'4000'
'price':'4000',
'modal':'2000'
}
response = request(
'PATCH',
......@@ -1210,6 +1211,7 @@ class ProductTest(rest_framework_test.APITestCase):
url_args=[product.id]
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(models.Product.objects.get(id=product.id).profit,2000)
self.assertEqual(models.Product.objects.get(id=product.id).name, data['name'])
data = dict(seeds.PRODUCT_DATA, subcategory=self.subcategory.id)
response = request(
......
......@@ -658,6 +658,54 @@ class ProductDetail(generics.RetrieveUpdateDestroyAPIView):
queryset = models.Product.objects.all()
serializer_class = api_serializers.ProductSerializer
def get_serializer(self, *args, **kwargs):
# leave this intact
serializer_class = self.get_serializer_class()
kwargs["context"] = self.get_serializer_context()
instance=self.get_object()
"""
Intercept the request and see if it needs tweaking
"""
if (self.request.data.get("price") != None) and (self.request.data.get("modal") != None):
draft_request_data = self.request.data.copy()
profit= int(self.request.data['price'])-int(self.request.data['modal'])
new_profit={'profit': profit}
draft_request_data.update(new_profit)
kwargs["data"] = draft_request_data
return serializer_class(*args, **kwargs)
else:
if (self.request.data.get("price") != None):
#
# Copy and manipulate the request
draft_request_data = self.request.data.copy()
profit= int(self.request.data['price'])-int(instance.modal)
new_profit={'profit': profit}
draft_request_data.update(new_profit)
kwargs["data"] = draft_request_data
return serializer_class(*args, **kwargs)
elif (self.request.data.get("modal") != None):
draft_request_data = self.request.data.copy()
profit= int(instance.price)-int(self.request.data['modal'])
new_profit={'profit': profit}
draft_request_data.update(new_profit)
kwargs["data"] = draft_request_data
return serializer_class(*args, **kwargs)
"""
If not mind your own business and move on
"""
return serializer_class(*args, **kwargs)
def partial_update(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(instance,data=request.data, partial=True)
serializer.is_valid(raise_exception=True)
return super().partial_update(request, *args, **kwargs) # pylint: disable=no-member
class ProgramProgressList(generics.ListCreateAPIView):
......
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