Exemplo 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()
Exemplo n.º 2
0
def database():
    db_session.close()
    db_session.remove()
    EntityDeclarativeBase.metadata.create_all(engine)
    yield db_session
    db_session.close()
    db_session.remove()
    EntityDeclarativeBase.metadata.drop_all(engine)
Exemplo n.º 3
0
def add_all_episodes():

    for ep in all_episodes(episodes):
        t = titles.loc[titles['tconst'] == ep].to_dict('records')[0]
        e = episodes.loc[episodes['tconst'] == ep].to_dict('records')[0]
        r = ratings.loc[ratings['tconst'] == ep].to_dict('records')
        db_session.add(episode_obj(ep, t, e, r))
        db_session.commit()
    db_session.close()
Exemplo n.º 4
0
 def tearDown(self):
     from app.entities.datasource import DataSourceEntity
     uploads = [
         datasource.location for datasource in DataSourceEntity.query.all()
     ]
     for upload in uploads:
         os.remove(upload)
     db_session.close()
     db_session.remove()
Exemplo n.º 5
0
def call_order(order):
    try:
        order.is_called = True
        db_session.add(order)
        db_session.commit()
        message = get_messages("CALL_ORDER_MESSAGE").format(order.id)
        make_conversion(message)
    except ValueError as ex:
        print(ex)
        db_session.rollback()
    finally:
        db_session.close()
Exemplo n.º 6
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()
Exemplo n.º 7
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()
Exemplo n.º 8
0
def calc_order(products_order, order, products):
    try:
        order_items = list()
        for i, p in enumerate(products):
            quantity = products_order[i].get("quantity")
            order.price += p.price * quantity
            order.quantity += quantity
            p.order.append(order)
            order_items.append(p.name)
        order.dt_closing = datetime.now()
        db_session.add(order)
        db_session.commit()
        money_ext, money_ext_brl = money_format(order.price)
        order_itens_text = ", ".join(order_items).replace(".", " ponto ")
        message = get_messages("CLOSING_ORDER_MESSAGE").format(
            order.id, money_ext, order_itens_text, order.quantity)
        make_conversion(message)
    except ValueError as ex:
        print(ex)
        db_session.rollback()
    finally:
        db_session.close()
Exemplo n.º 9
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()
Exemplo n.º 10
0
def teardown_request(exception):
    db_session.close()
    db_session.remove()
 def setUp(self):
     db_session.close()
     db_session.remove()
     EntityDeclarativeBase.metadata.drop_all(engine)
     EntityDeclarativeBase.metadata.create_all(engine)
Exemplo n.º 12
0
def shutdown_session(exception=None):
    db_session.close()
Exemplo n.º 13
0
 def __call__(self, *args, **kwargs):
     try:
         return super().__call__(*args, **kwargs)
     finally:
         db_session.close()