Untitled

mail@pastecode.io avatar
unknown
python
a year ago
2.1 kB
5
Indexable
Never
#####################################################################################
# orders - adicionar um botão a dizer prosseguir para pagamento; se ja estivesse paga dava o aviso (estar na tabela pay;cc diz encomenda paga)

@app.route("/order", methods=("GET",))
def order_index():
    """Show all the orders."""
    error = None
    try:
        with pool.connection() as conn:
            with conn.cursor(row_factory=namedtuple_row) as cur:
                orders = cur.execute(
                    """
                    SELECT o.order_no, o.cust_no, STRING_AGG(c.sku, ',') AS sku_list
                    FROM orders o
                    JOIN contains c ON o.order_no=c.order_no
                    JOIN product p ON c.sku=p.sku
                    GROUP BY o.order_no 
                    ORDER BY o.order_no DESC;
                    """,
                    {},
                ).fetchall()
                
                #log.debug(f"Found {cur.rowcount} rows.")
        # API-like response is returned to clients that request JSON explicitly (e.g., fetch)
        if (
            request.accept_mimetypes["application/json"]
            and not request.accept_mimetypes["text/html"]
        ):
            return jsonify(orders)
        
        # Obtém o parâmetro da página (ou o default = 1)
        page = request.args.get("page", 1, type=int)
        items_per_page = 2 
        total_orders = len(orders)
        page_start_index = (page - 1) * items_per_page
        page_end_index = page_start_index + items_per_page
        orders_page = orders[page_start_index:page_end_index]
        # Se dividíssemos apenas total orders / items_per_page não consideraríamos
        # os possíveis iteis que não satisfazem uma página inteira e por isso fazem com que 
        # a divisão não seja inteira.
        total_pages = (total_orders + items_per_page - 1) // items_per_page
        return render_template("order/index.html", orders=orders_page, total_pages=total_pages, page = page)
    except Exception as e:
        return str(e)