예제 #1
0
    def put(self, uuid):
        try:
            order = (Order.select().where(Order.uuid == uuid).where(
                Order.user == g.current_user).get())
        except Order.DoesNotExist:
            return None, NOT_FOUND

        parser = reqparse.RequestParser()
        parser.add_argument('items', type=is_valid_item_list, required=True)
        args = parser.parse_args(strict=True)

        total_price = 0

        items = args['items']
        uuid = [i[0] for i in items]
        items_query = Item.select().where(Item.uuid << uuid)

        if items_query.count() != len(items) or len(items) == 0:
            return None, BAD_REQUEST

        for item in items_query:
            item_quantity = [x[1] for x in items if x[0] == str(item.uuid)][0]
            total_price += float(item.price * item_quantity)

        for item in items_query:
            item_quantity = [x[1] for x in items if x[0] == str(item.uuid)][0]
            if item_quantity > item.availability:
                return None, BAD_REQUEST

        with database.transaction():
            temp_query = OrderItem.select().where(OrderItem.order == order.id)
            for order_item in temp_query:
                order_item.item.availability = (order_item.item.availability +
                                                order_item.quantity)
                order_item.item.save()

            OrderItem.delete().where(OrderItem.order == order.id).execute()

            for item in items_query:
                item_quantity = [
                    x[1] for x in items if x[0] == str(item.uuid)
                ][0]
                OrderItem.create(order=order,
                                 item=item.id,
                                 quantity=item_quantity,
                                 subtotal=float(item.price * item_quantity))
                item.availability = (item.availability - item_quantity)
                item.save()

            order.total_price = total_price
            order.save()

        return order.json(), OK
예제 #2
0
 def setup_method(self):
     OrderItem.delete().execute()
     Order.delete().execute()