More fixes.

This commit is contained in:
Yaro Kasear 2025-09-24 15:04:00 -05:00
parent 2a9fb389d7
commit c6165af40e
2 changed files with 155 additions and 81 deletions

View file

@ -17,6 +17,14 @@ def init_listing_routes(app):
if cls is None:
abort(404)
# read query args
limit = int(request.args.get("limit", 15))
sort = request.args.get("sort") # <- capture sort from URL
cursor = request.args.get("cursor")
# your decode returns (key, _desc, backward) in this project
key, _desc, backward = decode_cursor(cursor)
# base spec per model
spec = {}
columns = []
row_classes = []
@ -42,7 +50,8 @@ def init_listing_routes(app):
{"field": "model"},
{"field": "device_type.description", "label": "Device Type"},
{"field": "condition"},
{"field": "owner.label", "label": "Contact", "link": {"endpoint": "entry.entry", "params": {"id": "{owner.id}", "model": "user"}}},
{"field": "owner.label", "label": "Contact",
"link": {"endpoint": "entry.entry", "params": {"id": "{owner.id}", "model": "user"}}},
{"field": "location.label", "label": "Room"},
]
elif model.lower() == 'user':
@ -54,12 +63,13 @@ def init_listing_routes(app):
"robot.overlord",
"staff",
"active",
], "sort": "first_name,last_name"}
], "sort": "first_name,last_name"} # default for users
columns = [
{"field": "label", "label": "Full Name"},
{"field": "last_name"},
{"field": "first_name"},
{"field": "supervisor.label", "label": "Supervisor", "link": {"endpoint": "entry.entry", "params": {"id": "{supervisor.id}", "model": "user"}}},
{"field": "supervisor.label", "label": "Supervisor",
"link": {"endpoint": "entry.entry", "params": {"id": "{supervisor.id}", "model": "user"}}},
{"field": "staff", "format": "yesno"},
{"field": "active", "format": "yesno"},
]
@ -79,8 +89,10 @@ def init_listing_routes(app):
"complete",
]}
columns = [
{"field": "work_item.label", "label": "Work Item", "link": {"endpoint": "entry.entry", "params": {"id": "{work_item.id}", "model": "inventory"}}},
{"field": "contact.label", "label": "Contact", "link": {"endpoint": "entry.entry", "params": {"id": "{contact.id}", "model": "user"}}},
{"field": "work_item.label", "label": "Work Item",
"link": {"endpoint": "entry.entry", "params": {"id": "{work_item.id}", "model": "inventory"}}},
{"field": "contact.label", "label": "Contact",
"link": {"endpoint": "entry.entry", "params": {"id": "{contact.id}", "model": "user"}}},
{"field": "start_time", "format": "datetime"},
{"field": "end_time", "format": "datetime"},
{"field": "complete", "format": "yesno"},
@ -89,19 +101,27 @@ def init_listing_routes(app):
{"when": {"field": "complete", "is": True}, "class": "table-success"},
{"when": {"field": "complete", "is": False}, "class": "table-danger"}
]
limit = int(request.args.get("limit", 15))
cursor = request.args.get("cursor")
key, _desc, backward = decode_cursor(cursor)
# overlay URL-provided sort if present
if sort:
spec["sort"] = sort
service = crudkit.crud.get_service(cls)
# include limit and go
window = service.seek_window(spec | {"limit": limit}, key=key, backward=backward, include_total=True)
table = render_table(window.items, columns=columns, opts={"object_class": model, "row_classes": row_classes})
return render_template("listing.html", model=model, table=table, pagination={
table = render_table(window.items, columns=columns,
opts={"object_class": model, "row_classes": row_classes})
# pass sort through so templates can preserve it on pager links, if they care
pagination_ctx = {
"limit": window.limit,
"total": window.total,
"next_cursor": encode_cursor(window.last_key, list(window.order.desc), backward=False),
"prev_cursor": encode_cursor(window.first_key, list(window.order.desc), backward=True),
})
"sort": sort or spec.get("sort") # expose current sort to the template
}
return render_template("listing.html", model=model, table=table, pagination=pagination_ctx)
app.register_blueprint(bp_listing)