Beispiel #1
0
def create_order(request: Request):
    try:
        order_json = request.json_body
    except Exception as ex:
        print(ex)
        return Response(status=400, body='Bad input.')

    order_validator = OrderValidator(order_json)
    order_validator.validate()

    if order_validator.errors:
        return Response(status=400, body=order_validator.error_msg())

    try:
        order = OrderRepository.create(order_validator.order)

        cart_cookie = request.cookies.get('Cart')
        if cart_cookie is None:
            return Response(status=400, body='Cart is empty.')
        else:
            cart = eval(cart_cookie)
            for cart_item in cart:
                ordered_product = OrderedProduct(
                    odrer_id=order.id,
                    product_id=cart_item['product_id'],
                    quantity=cart_item['quantity'])
                OrderedProductRepository.create(ordered_product)
            response = Response(status=201, json_body=order.to_dict())
            response.delete_cookie('Cart')
        return response
    except Exception as ex:
        return Response(status=500, body='Something bad happened, call admin!')
Beispiel #2
0
def __delete_cookie_callback(response: Response, name: str):
    response.delete_cookie(name)