fix(types): sanitizar extraccion de payload y tipado estricto en buildings y reservations

This commit is contained in:
2026-10-19 15:02:47 -03:00
parent ae329c4c36
commit 263e0a17a6
3 changed files with 58 additions and 49 deletions
+21 -23
View File
@@ -11,7 +11,7 @@ from app import db
from app.models.building import Building
from app.models.classroom import Classroom
from app.services.audit_service import AuditService
from dependencies import get_current_user, require_roles
from dependencies import get_current_user, require_roles, get_request_payload
from app.models.user import User
from database import get_flask_app
@@ -96,22 +96,21 @@ async def create_building(
current_user: User = Depends(require_roles(["ADMIN", "BEDELIA"]))
):
"""Crea una nueva sede o edificio institucional."""
data = {}
try:
data = await request.json()
except Exception:
form = await request.form()
data = dict(form)
data = await get_request_payload(request)
with flask_app.app_context():
name = (data.get('name') or '').strip()
code = (data.get('code') or '').strip().upper() or None
address = (data.get('address') or '').strip() or None
name = str(data.get('name') or '').strip()
code_str = str(data.get('code') or '').strip().upper()
code = code_str if code_str else None
address_str = str(data.get('address') or '').strip()
address = address_str if address_str else None
try:
floors = int(data.get('floors', 1) or 1)
floors_val = data.get('floors', 1)
floors = int(str(floors_val)) if floors_val is not None else 1
except (ValueError, TypeError):
floors = 1
description = (data.get('description') or '').strip() or None
desc_str = str(data.get('description') or '').strip()
description = desc_str if desc_str else None
is_active = data.get('is_active', True)
if isinstance(is_active, str):
is_active = is_active.lower() in ['true', '1', 'on']
@@ -157,19 +156,14 @@ async def update_building(
current_user: User = Depends(require_roles(["ADMIN", "BEDELIA"]))
):
"""Actualiza la información de un edificio existente."""
data = {}
try:
data = await request.json()
except Exception:
form = await request.form()
data = dict(form)
data = await get_request_payload(request)
with flask_app.app_context():
building = Building.query.get(id)
if not building:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Edificio con ID {id} no encontrado.")
name = (data.get('name') or '').strip()
name = str(data.get('name') or '').strip()
if not name:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="El nombre del edificio es obligatorio.")
@@ -183,16 +177,20 @@ async def update_building(
old_name = building.name
building.name = name
if 'code' in data:
building.code = (data.get('code') or '').strip().upper() or None
c_val = str(data.get('code') or '').strip().upper()
building.code = c_val if c_val else None
if 'address' in data:
building.address = (data.get('address') or '').strip() or None
a_val = str(data.get('address') or '').strip()
building.address = a_val if a_val else None
if 'floors' in data:
try:
building.floors = int(data.get('floors', 0) or 0)
fl_val = data.get('floors', 0)
building.floors = int(str(fl_val)) if fl_val is not None else 0
except (ValueError, TypeError):
pass
if 'description' in data:
building.description = (data.get('description') or '').strip() or None
d_val = str(data.get('description') or '').strip()
building.description = d_val if d_val else None
if 'is_active' in data:
is_active = data.get('is_active')
if isinstance(is_active, str):