Ejemplo n.º 1
0
def add():
    """
    {
        "name":"Egg Burger",
        "price":10.00,
        "category":"hamburguer"
    }
    :return: {"result": "product has been created","status_code": 201}
    """
    try:
        data = request.get_json(force=True)
        if not data or not data.get("name") or not data.get("price"):
            return json_response(
                status_=StatusCodeEnum.HTTP_400_BAD_REQUEST,
                result=get_messages("PRODUCT_REQUIRED_FIELDS"),
            )
        product = Product(data)
        db_session.add(product)
        db_session.commit()
        return json_response(
            status_=StatusCodeEnum.HTTP_201_CREATED,
            result=get_messages("PRODUCT_CREATED"),
        )
    except ValueError as ex:
        db_session.rollback()
        raise ex
    finally:
        db_session.close()
Ejemplo n.º 2
0
 def list(self, request):
     if request.user.is_authenticated:
         messages = get_messages(language=request.user.language)
     else:
         messages = get_messages(language="en")
     serializer = self.serializer_class(messages, many=True)
     response = Response(serializer.data)
     response['Access-Control-Allow-Origin'] = '*'
     return response
Ejemplo n.º 3
0
def close():
    """
    {
        "products":[{"id":1,"quantity":2},{"id":2,"quantity":3},{"id":3,"quantity":4}]
    }
    :return: {"result": "Order 1 closed.","status_code": 201}
    """
    json_data = request.get_json(force=True)
    if not json_data:
        return json_response(status_=StatusCodeEnum.HTTP_400_BAD_REQUEST)
    try:
        order = Order(None)
        db_session.add(order)
        db_session.commit()
        if not order:
            return json_response(
                status_=StatusCodeEnum.HTTP_404_NOT_FOUND,
                result=get_messages("ORDER_NOT_FOUND"),
            )

        products_order = ProductsOrderSchema().dump(json_data)
        product_ids = list(map(lambda p: p.get("id"), products_order.get("products")))
        products = Product.query.filter(Product.id.in_(product_ids))
        if not products:
            return json_response(
                status_=StatusCodeEnum.HTTP_404_NOT_FOUND,
                result=get_messages("PRODUCT_NOT_FOUND"),
            )
        if order.dt_closing:
            return json_response(
                status_=StatusCodeEnum.HTTP_400_BAD_REQUEST,
                result=get_messages("ORDER_HAS_CLOSED").format(order.id),
            )
        if order.is_sentent:
            return json_response(
                status_=StatusCodeEnum.HTTP_400_BAD_REQUEST,
                result=get_messages("ORDER_HAS_SENTENT").format(order.id),
            )
        calc_order(products_order.get("products"), order, products)
        *_, money_ext_brl = money_format(order.price)
        return json_response(
            status_=StatusCodeEnum.HTTP_201_CREATED,
            result=get_messages("CLOSING_ORDER_MESSAGE_POPUP").format(
                order.id, money_ext_brl, order.quantity
            ),
        )
    except ValueError as ex:
        db_session.rollback()
        raise ex
    finally:
        db_session.close()
Ejemplo n.º 4
0
def edit(product_id):
    """
    {
        "name":"Burger Cabulozo",
        "price":20,55,
        "category":"FastFood"
    }
    :param product_id: Product identify
    :return: {"result": "product has been changed","status_code": 202}
    """
    try:
        product = Product.query.get(product_id)
        if not product:
            return json_response(
                status_=StatusCodeEnum.HTTP_404_NOT_FOUND,
                result=get_messages("PRODUCT_NOT_FOUND_COD").format(id),
            )
        data = request.get_json(force=True)
        if not data or not data.get("name") or not data.get("price"):
            return json_response(
                status_=StatusCodeEnum.HTTP_400_BAD_REQUEST,
                result=get_messages("PRODUCT_REQUIRED_FIELDS"),
            )
        product_schema = ProductSchema().dump(data).data
        if product_schema.get("name"):
            product.name = product_schema["name"]
        if product_schema.get("price"):
            product.price = data["price"]
        if product_schema.get("category"):
            product.category = data["category"]
        db_session.add(product)
        db_session.commit()
        return json_response(
            status_=StatusCodeEnum.HTTP_202_ACCEPTED,
            result=get_messages("PRODUCT_CHANGED"),
        )
    except ValueError as ex:
        db_session.rollback()
        raise ex
    finally:
        db_session.close()
Ejemplo n.º 5
0
def called():
    """
    Get order has called
    """
    order = Order.query.filter(Order.is_called and Order.is_sentent == 0).first()
    if order:
        mapper = OrderSchema().dump(order)
        return json.dumps(mapper)
    return json_response(
        status_=StatusCodeEnum.HTTP_404_NOT_FOUND,
        result=get_messages("ORDER_NOT_FOUND"),
    )
Ejemplo n.º 6
0
def sent(order_id):
    """
    Sentent order after click on button
    """
    if not order_id:
        return json_response(
            status_=StatusCodeEnum.HTTP_400_BAD_REQUEST,
            result=get_messages("ORDER_NOT_FOUND"),
        )
    order = Order.query.filter(
        Order.id == order_id and not Order.is_sentent and Order.is_called
    ).one()
    if order:
        sent_order(order)
        return json_response(
            status_=StatusCodeEnum.HTTP_200_OK,
            result=get_messages("SENTENT_ORDER_MESSAGE").format(order.id),
        )
    return json_response(
        status_=StatusCodeEnum.HTTP_404_NOT_FOUND,
        result=get_messages("ORDER_NOT_FOUND_COD").format(order_id),
    )
Ejemplo n.º 7
0
def call(order_id):
    """
    Call order by Id
    """
    if not order_id:
        return json_response(
            status_=StatusCodeEnum.HTTP_400_BAD_REQUEST,
            result=get_messages("ORDER_COD_NOT_SPECIFIED"),
        )
    order = Order.query.filter(
        Order.id == order_id and not Order.is_sentent and not Order.is_called
    ).one()
    if order:
        call_order(order)
        return json_response(
            status_=StatusCodeEnum.HTTP_200_OK,
            result=get_messages("CALL_ORDER_MESSAGE").format(order.id),
        )
    return json_response(
        status_=StatusCodeEnum.HTTP_404_NOT_FOUND,
        result=get_messages("ORDER_NOT_FOUND_COD").format(order_id),
    )
Ejemplo n.º 8
0
def delete(product_id):
    """
    http://192.168.25.20:9098/api/product/1
    :param product_id: Product identify
    :return: {"result": "product has been removed","status_code": 202}
    """
    try:
        product = Product.query.get(product_id)
        if not product:
            return json_response(
                status_=StatusCodeEnum.HTTP_404_NOT_FOUND,
                result=get_messages("PRODUCT_NOT_FOUND_COD").format(id),
            )
        db_session.delete(product)
        db_session.commit()
        return json_response(
            status_=StatusCodeEnum.HTTP_202_ACCEPTED,
            result=get_messages("PRODUCT_REMOVED"),
        )
    except ValueError as ex:
        db_session.rollback()
        raise ex
    finally:
        db_session.close()