HTMX detail crud not working

70 views
Skip to first unread message

Jorge Salvat

unread,
Jul 18, 2024, 3:39:38 PM7/18/24
to py4web
Hi Jim, in HTMX_DEMO, when editing an order,
you can't go further editing an order_line

htmx_demo.jpg
I think there is a global bug since the same happens in SOUTHBREEZE
Also, python running environment gets corrupted if you try to click the edit button on the detail order_line.

Jim Steil

unread,
Jul 18, 2024, 5:33:41 PM7/18/24
to py4web
Jorge

Yup, looks like something changed in py4web. I haven't updated those applications in a year or two.

I'll have to take a look and see what changed that needs to be updated in them.

I'll get back to you

-Jim

Jim Steil

unread,
Jul 19, 2024, 2:35:28 PM7/19/24
to py4web

Found the problem. Must have been a change in pydal or web2py grid (I didn't research) but a simple fix.

I'll submit a PR to py4web for a fix, but not sure how quickly Massimo can merge. Seems like he has some merge plans for this weekend.

In the grid in master on github, lines 1665 and 1666 need to change from:

    if parent_id and "&" in parent_id:
        parent_id = parent_id.split("&")[0]

to:

    if parent_id and "&" in str(parent_id):
        parent_id = str(parent_id).split("&")[0]

parent_id in some instances cannot be evaluated as a string and I'm performing str() functions against it. Not sure if something changed in pydal or py4web that caused this, but the fix is pretty simple.

-Jim

Jorge Salvat

unread,
Jul 19, 2024, 2:42:40 PM7/19/24
to py4web
Hi Jim, Solved!!
the problem is that when you  detail/edit/delete  an  order_line  cannot use:
   order_id = get_parent(
       path,
       parent_field=db.order_line.order,
   )
Here is the code that works for HTMX_DEMO:

@action("order_lines", method=["POST", "GET"])
@action("order_lines/<path:path>", method=["POST", "GET"])
@action.uses("order_lines.html", session, db, auth)
def order_lines(path=None):
    if not path or path.split("/")[0] in ["select", "new"]:
        #  set the default
        order_id = get_parent(
            path,
            parent_field=db.order_line.order,
        )
        db.order_line.order.default = order_id
        left = db.product.on(db.order_line.product == db.product.id)
        #  get order total
        total = (
            db(db.order_line.order == order_id)
            .select(db.order_line.price.sum())
            .first()[db.order_line.price.sum()]
        )
        grid = Grid(
            path,
            fields=[
                db.product.name,
                db.order_line.quantity,
                db.product.price,
                db.order_line.price,
            ],
            headings=["PRODUCT", "QTY", "UNIT", "EXT"],
            query=reduce(
                lambda a, b: (a & b),
                [db.order_line.order == order_id],
            ),
            left=left,
            orderby=[db.order_line.id],
            auto_process=False,
            rows_per_page=5,
            grid_class_style=GridClassStyleBulma,
            formstyle=FormStyleBulma,
            details=False,
            include_action_button_text=False,
        )
        attrs = {
            "_hx-get": URL("order_lines", vars=dict(parent_id=order_id)),
            "_class": "button is-default",
        }
    else:
        total = 0
        grid = Grid(
            path,
            db.order_line,
            fields=[
                db.product.name,
                db.order_line.quantity,
                db.product.price,
                db.order_line.price,
            ],
            headings=["PRODUCT", "QTY", "UNIT", "EXT"],
            auto_process=False,
            grid_class_style=GridClassStyleBulma,
            formstyle=FormStyleBulma,
            include_action_button_text=False,
        )
        attrs = {
            "_onclick": "window.history.back(); return false;",
            "_class": "button is-default",
        }
    if path and path.split("/")[0] in ["new", "details", "edit"]:
        db.order_line.price.readable = False
        db.order_line.price.writable = False
    formstyle = FormStyleFactory()
    formstyle.classes = FormStyleBulma.classes
    formstyle.class_inner_exceptions = FormStyleBulma.class_inner_exceptions
    if path and path.split("/")[0] in ["select", "new"]:
        formstyle.widgets["product"] = NewHtmxAutocompleteWidget(
            url=URL("product_autocomplete"), order_id=order_id
        )
    grid.attributes_plugin = AttributesPluginHtmx("#htmx-target")
    grid.param.new_sidecar = BUTTON("Cancel", **attrs)
    grid.param.edit_sidecar = BUTTON("Cancel", **attrs)
    grid.process()
    return dict(grid=grid, total=total)


Same fix should be done to SOUTHBREEZE app

Jorge Salvat

unread,
Jul 19, 2024, 2:45:11 PM7/19/24
to py4web
We crossed responses at the same time !!!!
I'll take a look to what you say, i have to go away now.
Thanks

Jim Steil

unread,
Jul 19, 2024, 3:02:34 PM7/19/24
to py4web
Jorge

I submitted a PR to fix in grid.py. It was happening on all my htmx grids.

I have not yet tested Massimo's recent grid refactoring. That may have some issues too

-Jim

Jorge Salvat

unread,
Jul 19, 2024, 5:27:48 PM7/19/24
to py4web
Well done Jim, you got-it right, that solved the problem.
Thanks :-)

Reply all
Reply to author
Forward
0 new messages