Fakultas Ilmu Komputer UI

Commit 9b3440f6 authored by Bunga Amalia Kurniawati's avatar Bunga Amalia Kurniawati 🌺
Browse files

[REFACTOR] change validating data method

parent 87f9fe34
Pipeline #83208 passed with stages
in 12 minutes and 22 seconds
......@@ -36,11 +36,7 @@ class CartUpdate(rest_framework_views.APIView):
product=product,
shopping_cart=shopping_cart
)
validated_hampers_messages = ''
try:
validated_hampers_messages = serializer.validated_data['hampers_messages']
except KeyError:
pass
validated_hampers_messages = serializer.validated_data.get('hampers_messages', '')
if serializer.validated_data['quantity'] == 0:
cart_item.delete()
else:
......
......@@ -105,25 +105,12 @@ class ProductList(generics.ListCreateAPIView):
def post(self, request, _format=None):
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
validated_image = None
validated_is_hampers = False
validated_hampers_price = 0
try:
validated_image = serializer.validated_data['image']
validated_is_hampers=serializer.validated_data['is_hampers']
validated_hampers_price = serializer.validated_data['hampers_price']
except KeyError:
pass
try:
validated_is_hampers=serializer.validated_data['is_hampers']
except KeyError:
pass
try:
validated_hampers_price = serializer.validated_data['hampers_price']
except KeyError:
pass
if serializer.validated_data['preorder']:
product = models.Product.objects.create(
validated_image = serializer.validated_data.get('image', None)
validated_is_hampers = serializer.validated_data.get('is_hampers', False)
validated_hampers_price = serializer.validated_data.get('hampers_price', 0)
validated_preorder = serializer.validated_data.get('preorder', False)
validated_preorder_duration = serializer.validated_data.get('preorder_duration', 0)
product = models.Product.objects.create(
name=serializer.validated_data['name'],
description=serializer.validated_data['description'],
price=serializer.validated_data['price'],
......@@ -131,25 +118,11 @@ class ProductList(generics.ListCreateAPIView):
subcategory=models.Subcategory.objects.get(name=serializer.validated_data['subcategory']),
total_profit=0,
unit=serializer.validated_data['unit'],
is_hampers=validated_is_hampers,
hampers_price=validated_hampers_price,
image=validated_image,
preorder=serializer.validated_data['preorder'],
preorder_duration=serializer.validated_data['preorder_duration'],
)
else:
product = models.Product.objects.create(
name=serializer.validated_data['name'],
description=serializer.validated_data['description'],
price=serializer.validated_data['price'],
stock=serializer.validated_data['stock'],
modal=serializer.validated_data['modal'],
subcategory=models.Subcategory.objects.get(name=serializer.validated_data['subcategory']),
total_profit=0,
unit=serializer.validated_data['unit'],
is_hampers=validated_is_hampers,
hampers_price=validated_hampers_price,
image=validated_image,
preorder=validated_preorder,
preorder_duration=validated_preorder_duration,
)
product.profit = (product.price - product.modal)
product.save()
......
Markdown is supported
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