Files

34 lines
1.2 KiB
Python

"""Generic paginated read access to every importable table through its
`Repository`, so clients can inspect what an import wrote."""
from flask import Blueprint, jsonify, request
from webapi.imports.schemas.registry import ENTITY_REGISTRY
data_bp = Blueprint("data", __name__)
_MAX_PAGE = 500
@data_bp.get("/<target>")
def list_rows(target: str):
spec = ENTITY_REGISTRY.get(target)
if spec is None:
return jsonify({"error": f"destino desconocido: {target!r}"}), 404
try:
limit = min(max(int(request.args.get("limit", 50)), 1), _MAX_PAGE)
offset = max(int(request.args.get("offset", 0)), 0)
except ValueError:
return jsonify({"error": "limit y offset deben ser números enteros"}), 400
rows = spec.repository.list(limit=limit, offset=offset)
return jsonify(
{
"target": target,
"total": spec.repository.count(),
"limit": limit,
"offset": offset,
# Table order (JSON objects get their keys sorted on the way out).
"columns": [column.name for column in spec.model.__table__.columns],
"rows": [row.model_dump() for row in rows],
}
)