Agrega filtros y ruta sintética de seguimiento CaCu
This commit is contained in:
@@ -17,9 +17,19 @@ from datetime import date
|
||||
|
||||
from flask import Blueprint, Response, jsonify, request
|
||||
from webapi.follow_up.listing import PaginaFuera, PedidoInvalido, seguimiento_de
|
||||
from webapi.follow_up.screening import detalle_de
|
||||
from sqlmodel import Session
|
||||
|
||||
FECHA = re.compile(r"\d{4}-\d{2}-\d{2}")
|
||||
RUTA_ID = re.compile(r"[0-9a-f]{64}")
|
||||
|
||||
|
||||
def _rutas_sinteticas(sesion: Session) -> bool:
|
||||
"""DEV no acredita autorización por paciente. Limitar los dos contratos al fixture local."""
|
||||
if os.environ.get("DEV_REVISION") != "cacu-dev-sintetico":
|
||||
return False
|
||||
url = sesion.get_bind().url
|
||||
return url.drivername == "sqlite" and url.database == "/workspace/entorno-dev/datos-sinteticos/cacu.db"
|
||||
|
||||
|
||||
def in_dev() -> bool:
|
||||
@@ -40,6 +50,8 @@ def follow_up_blueprint(get_session: Callable[[], Session]) -> Blueprint:
|
||||
return jsonify({"error": str(error)}), 400
|
||||
sesion = get_session()
|
||||
try:
|
||||
if pedido.get("incluir_ruta") and not _rutas_sinteticas(sesion):
|
||||
return jsonify({"error": "Ruta de tamizaje no disponible"}), 404
|
||||
cuerpo = seguimiento_de(sesion, **pedido)
|
||||
except PedidoInvalido as error:
|
||||
return jsonify({"error": str(error)}), 400
|
||||
@@ -49,12 +61,39 @@ def follow_up_blueprint(get_session: Callable[[], Session]) -> Blueprint:
|
||||
sesion.close()
|
||||
return Response(json.dumps(cuerpo, ensure_ascii=False), mimetype="application/json")
|
||||
|
||||
@blueprint.get("/seguimiento/rutas/<int:pac_id>/<ruta_id>")
|
||||
def get_ruta(pac_id: int, ruta_id: str):
|
||||
no_disponible = {"error": "Ruta de tamizaje no disponible"}
|
||||
if not RUTA_ID.fullmatch(ruta_id) or pac_id > 2**63 - 1:
|
||||
return jsonify(no_disponible), 404
|
||||
sesion = get_session()
|
||||
try:
|
||||
if not _rutas_sinteticas(sesion):
|
||||
return jsonify(no_disponible), 404
|
||||
texto = request.args.get("corte", "")
|
||||
try:
|
||||
if not FECHA.fullmatch(texto):
|
||||
raise ValueError
|
||||
corte = date.fromisoformat(texto)
|
||||
except ValueError:
|
||||
return jsonify({"error": "corte debe ser una fecha válida AAAA-MM-DD"}), 400
|
||||
ruta = detalle_de(sesion, pac_id, ruta_id, corte)
|
||||
if ruta is None:
|
||||
return jsonify(no_disponible), 404
|
||||
return jsonify(ruta)
|
||||
finally:
|
||||
sesion.close()
|
||||
|
||||
return blueprint
|
||||
|
||||
|
||||
def _requested(args) -> dict:
|
||||
"""Los parámetros de la consulta, con su tipo. `ValueError` si uno viene mal escrito."""
|
||||
pedido = {}
|
||||
if "incluir" in args:
|
||||
if args.getlist("incluir") != ["ruta_tamizaje"]:
|
||||
raise ValueError("incluir admite ruta_tamizaje")
|
||||
pedido["incluir_ruta"] = True
|
||||
if corte := args.get("corte"):
|
||||
if not FECHA.fullmatch(corte):
|
||||
raise ValueError(f"corte mal escrito {corte!r}; use AAAA-MM-DD")
|
||||
@@ -64,6 +103,19 @@ def _requested(args) -> dict:
|
||||
if not texto.isdigit():
|
||||
raise ValueError(f"{nombre} debe ser un entero; llegó {texto!r}")
|
||||
pedido[nombre] = int(texto)
|
||||
for nombre in ("edad_min", "edad_max"):
|
||||
if nombre in args:
|
||||
texto = args.get(nombre, "")
|
||||
if not texto.isascii() or not texto.isdigit():
|
||||
raise ValueError(f"{nombre} debe ser un entero no negativo")
|
||||
pedido[nombre] = int(texto)
|
||||
if "q" in args:
|
||||
texto = args.get("q", "")
|
||||
if len(texto) > 100:
|
||||
raise ValueError("q admite hasta 100 caracteres")
|
||||
q = " ".join(texto.split())
|
||||
if q:
|
||||
pedido["q"] = q
|
||||
if estatus := args.get("estatus"):
|
||||
pedido["estatus"] = estatus
|
||||
if orden := args.get("orden"):
|
||||
|
||||
Reference in New Issue
Block a user