Fixed two of the most annoying bugs in the universe: Extra sessions and malfunctioning params.
This commit is contained in:
parent
981d3ea933
commit
803eb83ca5
2 changed files with 110 additions and 198 deletions
|
|
@ -18,10 +18,11 @@ def init_listing_routes(app):
|
|||
abort(404)
|
||||
|
||||
# read query args
|
||||
limit = int(request.args.get("limit", 15))
|
||||
sort = request.args.get("sort") # <- capture sort from URL
|
||||
limit = request.args.get("limit", None)
|
||||
limit = int(limit) if (limit is not None and str(limit).isdigit()) else 15
|
||||
sort = request.args.get("sort")
|
||||
fields_qs = request.args.get("fields")
|
||||
cursor = request.args.get("cursor")
|
||||
# your decode returns (key, _desc, backward) in this project
|
||||
key, _desc, backward = decode_cursor(cursor)
|
||||
|
||||
# base spec per model
|
||||
|
|
@ -102,42 +103,42 @@ def init_listing_routes(app):
|
|||
{"when": {"field": "complete", "is": False}, "class": "table-danger"}
|
||||
]
|
||||
|
||||
# overlay URL-provided sort if present
|
||||
# Build params to feed CRUDService (flat dict; parse_filters expects flat keys)
|
||||
params = dict(spec)
|
||||
|
||||
# overlay fields from query (?fields=...)
|
||||
if fields_qs:
|
||||
params["fields"] = [p.strip() for p in fields_qs.split(",") if p.strip()]
|
||||
|
||||
# overlay sort from query (?sort=...)
|
||||
if sort:
|
||||
spec["sort"] = sort
|
||||
params["sort"] = sort
|
||||
|
||||
# limit semantics: 0 means "unlimited" in your service layer
|
||||
params["limit"] = limit
|
||||
|
||||
# forward *all other* query params as filters (flat), excluding known control keys
|
||||
CONTROL_KEYS = {"limit", "cursor", "sort", "fields"}
|
||||
for k, v in request.args.items():
|
||||
if k in CONTROL_KEYS:
|
||||
continue
|
||||
if v is None or v == "":
|
||||
continue
|
||||
params[k] = v
|
||||
|
||||
service = crudkit.crud.get_service(cls)
|
||||
try:
|
||||
rt = app.extensions["crudkit"]["runtime"]
|
||||
rt_engine = rt.engine
|
||||
except Exception:
|
||||
rt_engine = None
|
||||
|
||||
try:
|
||||
SessionFactory = app.extensions["crudkit"].get("SessionFactory")
|
||||
sf_engine = getattr(SessionFactory, "bind", None) or getattr(SessionFactory, "kw", {}).get("bind")
|
||||
except Exception:
|
||||
sf_engine = None
|
||||
|
||||
try:
|
||||
bind = service.session.get_bind()
|
||||
svc_engine = getattr(bind, "engine", bind)
|
||||
except Exception:
|
||||
svc_engine = None
|
||||
|
||||
# include limit and go
|
||||
window = service.seek_window(spec | {"limit": limit}, key=key, backward=backward, include_total=True)
|
||||
window = service.seek_window(params, key=key, backward=backward, include_total=True)
|
||||
|
||||
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
|
||||
"sort": params.get("sort") # expose current sort to the template
|
||||
}
|
||||
|
||||
return render_template("listing.html", model=model, table=table, pagination=pagination_ctx)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue