def partial_update(self, request, pk=None): """Partially Updates a certain order from the user's list. only driver can update the order, there is only one field that can be updated which is the order status. Arguments: request: the request data sent by the user, it is used to check the user's permissions and get the data. pk: the id of the order that the user wants to change, it should by an integer. Returns: HTTP 403 Response if the user is not authorized to update that order, HTTP 400 Response if the data is not valid with the errors, HTTP 404 Response if the order is not found if not returns HTTP 200 Response with the updated JSON data. """ order = get_object_or_404(OrderModel, pk=pk) self.check_object_permissions(request, order) serializer = OrderDetailSerializer(order, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def test_nothing(self): """this is NOT a test but i needed this function to run with other tests""" serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }, { 'product': self.product2.pk, 'choices': [{ 'option_group_id': self.option_group.sort, 'choosed_option_id': self.option.sort }], 'add_ons_sorts': [self.addon.sort] }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertTrue(serializer.is_valid()) serializer.save()
def test_order_status(self): """test for order status post validate""" order = OrderModel.objects.create(final_price=0, subtotal=0, delivery_fee=0, vat=0) # true serializer = OrderDetailSerializer(data={'status': 'P'}, instance=order, partial=True) self.assertTrue(serializer.is_valid()) serializer.save() # wrong status serializer = OrderDetailSerializer(data={'status': 'wrong'}, instance=order, partial=True) self.assertFalse(serializer.is_valid()) # can't reverse the status back serializer = OrderDetailSerializer(data={'status': 'C'}, instance=order, partial=True) self.assertFalse(serializer.is_valid())
def create(self, request): """Creates a new order and adds it to the user's list. Arguments: request: the request data sent by the user, it is used to get the request data Returns: HTTP 403 Response if the user is not authorized to add an order, HTTP 400 Response if the data is not valid, if not, returns HTTP 201 Response with the order's JSON data. """ serializer = OrderDetailSerializer(data=request.data) if serializer.is_valid(): serializer.save(user=request.user.profile) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def ConfirmOrderDetail(request, format=None): if request.method == 'POST': print(request.data) try: cart = cartItem.objects.filter(user=request.user) except product.DoesNotExist: raise Response(status=404) for x in cart: serializer = OrderDetailSerializer(data=request.data) if serializer.is_valid(): data = {} detail = serializer.save(product=x.item, price=x.item.price, Shoesize=x.Shoesize) data['success'] = True cart.delete() return Response(data)