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 post(self, request, *args, **kwargs): cartList = Cart.objects.filter(user_id=self.request.user.id) n = random.randint(0,123445) # shuffle list # convert list to string now = datetime.datetime.now() finalStr = '' user = User.objects.get(id=self.request.user.id) mrp = 'mrp1' try: mrp = user.profile.state.dbp except Exception as e: pass order, created = Order.objects.get_or_create(orderId = n, total = 0, user = user,addedon = datetime.datetime.now()) totalPrice = 0 if cartList.exists(): for cart in cartList: finalStr = 'EC/'+str(now.year)+'/OR'+str(cart.product.category.id)+str(n) cart_product = cart.product.mrp1 if mrp=='dbp1': cart_product = cart.product.dbp1 cart_quantity = cart.quantity totalPrice = totalPrice+int(cart_product)*int(cart_quantity) OrderDetail.objects.create(order_number=finalStr, order = order, product_id = cart.product_id, quantity = cart.quantity, user = user,addedon = datetime.datetime.now()) cart.delete() order.total = totalPrice order.save() oreder_data = OrderSerializer(order) queryset = OrderDetail.objects.filter(order_id=order.id).distinct().values('order_number') csv_arr = [] for csvorder in queryset: orderMain = OrderDetail.objects.filter(order_number=csvorder['order_number']) data = OrderDetailSerializer(orderMain,many=True) for order in data.data: arr = {} arr['Ecatalogue_orderno'] = csvorder['order_number'] arr['Ecatalogue_orderDate'] = order['addedon'] arr['Customer_Code'] = order['user'] arr['Shipping_point'] = "" arr['line_no'] = 1 arr['Item_Code'] = "'"+str(order['product']['item_code']) arr['Item_Variant'] = order['product']['variant'] arr['Quantity'] = order['quantity'] arr['Remarks'] = "" arr['Company_Code'] = user.profile.company_code csv_arr.append(arr) csvfilename = str(csvorder['order_number']) csvfilename = 'EC-00-'+str(n)+'-OR.csv' filename = settings.MEDIA_ROOT+'/order_csv/'+csvfilename csv_url = settings.ROOT_URL+'/api/media/order_csv/'+csvfilename try: write_to_csv(csv_arr,filename) sendemail(oreder_data.data,csv_url) except Exception as e: pass return Response('created')
def myOrderDetail(request, orderID): if request.method == 'GET': try: order = orderMaster.objects.get(id=orderID) productDetail = orderDetail.objects.filter(order=order) except product.DoesNotExist: raise Response(status=404) serializer = OrderDetailSerializer(productDetail, many=True) return Response(serializer.data)
def put(self, request, *args, **kwargs): logger.info(request) logger.info(kwargs['pk']) order = confirm_order(kwargs['pk']) try: ser = OrderDetailSerializer(order) data = ser.data except KeyError: data = order return Response(status=200, data=data)
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 get(self, request, order_id): queryset = models.OrderDetail.objects.filter(order_id=order_id, is_delete=False) if not queryset: return Response({'status_code': 1, 'data': None, 'msg': '未查询到结果'}) serailizer = OrderDetailSerializer(queryset, many=True) return Response({ 'status_code': 0, 'data': serailizer.data, 'msg': 'ok' })
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)
def retrieve(self, request, pk=None): """Retrieves a certain order from the user's list Arguments: request: the request data sent by the user, it is used to checks the user's permissions pk: the sort of the address that the user want info about, it should by an integer. Returns: HTTP 403 Response if the user is not authorized to see that order, HTTP 404 Response if order is not found, if not, returns HTTP 200 Response with the order's JSON data. """ order = get_object_or_404(OrderModel, pk=pk) self.check_object_permissions(request, order) serializer = OrderDetailSerializer(order) return Response(serializer.data)
def test_shops_nearby(self): """test whether all shop are near the shipping address""" # true serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertTrue(serializer.is_valid()) # far away from the product's shop self.shop_address.location_longitude = 12 self.shop_address.location_latitude = 54 self.shop_address.save() serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertFalse(serializer.is_valid())
def test_driver_available(self): """test if there are any drivers available or not""" # true serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertTrue(serializer.is_valid()) # no driver in this range serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 12, 'location_latitude': 43 } }) self.assertFalse(serializer.is_valid())
def test_wrong_item_data(self): """test if any order item data is wrong""" # right serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertTrue(serializer.is_valid()) # wrong product pk serializer = OrderDetailSerializer( data={ 'items': [{ 'product': 221 }], # backer street :) 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertFalse(serializer.is_valid())
def retrieve(self, request, *args, **kwargs): order_details = self.get_object() serializer = OrderDetailSerializer(instance=order_details) return Response(serializer.data)
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 test_shop_available(self): """test if product's shop is available""" # test for is_active self.shop.is_active = False self.shop.save() # wrong because shop is not active serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertFalse(serializer.is_valid()) # revert it back self.shop.is_active = True self.shop.save() # test for is_open self.shop.is_open = False self.shop.save() # wrong because shop is not active serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertFalse(serializer.is_valid()) # revert it back self.shop.is_open = True self.shop.save() # test for opens_at self.shop.opens_at = timezone.now() + timezone.timedelta( hours=2) # two hours after now (not open yet) self.shop.save() # wrong because shop is not active serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertFalse(serializer.is_valid()) # revert it back self.shop.opens_at = timezone.now() - timezone.timedelta(hours=2) self.shop.save() # test for closes_at self.shop.closes_at = timezone.now() - timezone.timedelta( hours=2) # closed two hours before now (closed now) self.shop.save() # wrong because shop is not active serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertFalse(serializer.is_valid()) # revert it back self.shop.closes_at = timezone.now() + timezone.timedelta(hours=2) self.shop.save() # true serializer = OrderDetailSerializer( data={ 'items': [{ 'product': self.product.pk }], 'shipping_address': { 'area': 'area', 'type': 'A', 'street': 'street', 'building': 'building', 'location_longitude': 30, 'location_latitude': 30 } }) self.assertTrue(serializer.is_valid())