Mate11 commited on
Commit Β·
2ad902b
1
Parent(s): 2c5d6e7
CAMBIOS en el modelo de llamado a la bbdd
Browse files- app/config.py +3 -2
- app/dependencies.py +4 -14
- app/models/cama.py +53 -38
app/config.py
CHANGED
|
@@ -8,8 +8,9 @@ class Settings(BaseSettings):
|
|
| 8 |
extra="ignore",
|
| 9 |
)
|
| 10 |
|
| 11 |
-
# Base de datos
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
# JWT β sin default para evitar secretos hardcodeados en cΓ³digo fuente
|
| 15 |
jwt_secret: str
|
|
|
|
| 8 |
extra="ignore",
|
| 9 |
)
|
| 10 |
|
| 11 |
+
# Base de datos (Supabase)
|
| 12 |
+
supabase_url: str
|
| 13 |
+
supabase_key: str
|
| 14 |
|
| 15 |
# JWT β sin default para evitar secretos hardcodeados en cΓ³digo fuente
|
| 16 |
jwt_secret: str
|
app/dependencies.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
Dependencias compartidas de FastAPI: BD (
|
| 3 |
"""
|
| 4 |
import logging
|
| 5 |
from datetime import datetime, timedelta, timezone
|
|
@@ -7,8 +7,7 @@ from datetime import datetime, timedelta, timezone
|
|
| 7 |
from fastapi import Depends, HTTPException, status
|
| 8 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 9 |
from jose import JWTError, jwt
|
| 10 |
-
from
|
| 11 |
-
from sqlalchemy.orm import declarative_base, sessionmaker
|
| 12 |
|
| 13 |
from app.config import settings
|
| 14 |
|
|
@@ -16,17 +15,8 @@ logger = logging.getLogger(__name__)
|
|
| 16 |
|
| 17 |
# βββ Base de datos βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
Base = declarative_base()
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def get_db():
|
| 25 |
-
db = SessionLocal()
|
| 26 |
-
try:
|
| 27 |
-
yield db
|
| 28 |
-
finally:
|
| 29 |
-
db.close()
|
| 30 |
|
| 31 |
|
| 32 |
# βββ JWT βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 1 |
"""
|
| 2 |
+
Dependencias compartidas de FastAPI: BD (Supabase), JWT y autenticaciΓ³n.
|
| 3 |
"""
|
| 4 |
import logging
|
| 5 |
from datetime import datetime, timedelta, timezone
|
|
|
|
| 7 |
from fastapi import Depends, HTTPException, status
|
| 8 |
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 9 |
from jose import JWTError, jwt
|
| 10 |
+
from supabase import create_client, Client
|
|
|
|
| 11 |
|
| 12 |
from app.config import settings
|
| 13 |
|
|
|
|
| 15 |
|
| 16 |
# βββ Base de datos βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 17 |
|
| 18 |
+
def get_db() -> Client:
|
| 19 |
+
return create_client(settings.supabase_url, settings.supabase_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
# βββ JWT βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
app/models/cama.py
CHANGED
|
@@ -1,43 +1,58 @@
|
|
| 1 |
from __future__ import annotations
|
| 2 |
-
from datetime import datetime
|
| 3 |
-
from
|
| 4 |
-
from sqlalchemy.dialects.postgresql import ARRAY
|
| 5 |
-
from sqlalchemy.orm import relationship
|
| 6 |
-
from app.dependencies import Base
|
| 7 |
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
id = Column(Integer, primary_key=True, autoincrement=True)
|
| 13 |
-
numero = Column(Integer, nullable=False)
|
| 14 |
-
piso = Column(Integer, nullable=False)
|
| 15 |
-
# SectorEnum: UTI | SALA_COMUN | QUIROFANO | GUARDIA | NEONATOLOGIA
|
| 16 |
-
sector = Column(String(20), nullable=False)
|
| 17 |
-
# TipoCamaEnum: ESTANDAR | PEDIATRICA | AISLAMIENTO | CRITICA | CIRUGIA
|
| 18 |
-
tipo = Column(String(20), nullable=False)
|
| 19 |
-
# EstadoCamaEnum: DISPONIBLE | OCUPADA | EN_LIMPIEZA | FUERA_DE_SERVICIO | BAJA_PERMANENTE | CODIGO_ROJO
|
| 20 |
-
estado = Column(String(20), nullable=False, default="DISPONIBLE")
|
| 21 |
-
specialty_id = Column(Integer, nullable=True)
|
| 22 |
-
paciente_id = Column(Integer, nullable=True)
|
| 23 |
-
hospital_id = Column(String(50), nullable=False)
|
| 24 |
-
contraindicaciones_incompatibles = Column(ARRAY(String), nullable=False, default=list)
|
| 25 |
-
ultima_actualizacion = Column(
|
| 26 |
-
DateTime(timezone=True),
|
| 27 |
-
default=lambda: datetime.now(timezone.utc),
|
| 28 |
-
onupdate=lambda: datetime.now(timezone.utc),
|
| 29 |
-
)
|
| 30 |
-
# Rastrea tipo del ΓΊltimo movimiento para saber si disparar CAMA_LISTA al quedar DISPONIBLE
|
| 31 |
-
ultimo_movimiento_tipo = Column(String(20), nullable=True)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
"
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from __future__ import annotations
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from typing import Optional
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
+
def _parse_dt(v) -> Optional[datetime]:
|
| 7 |
+
if v is None:
|
| 8 |
+
return None
|
| 9 |
+
if isinstance(v, datetime):
|
| 10 |
+
return v
|
| 11 |
+
return datetime.fromisoformat(str(v))
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
class Cama:
|
| 15 |
+
def __init__(
|
| 16 |
+
self,
|
| 17 |
+
id: Optional[int] = None,
|
| 18 |
+
numero: int = 0,
|
| 19 |
+
piso: int = 0,
|
| 20 |
+
sector: str = "",
|
| 21 |
+
tipo: str = "",
|
| 22 |
+
estado: str = "DISPONIBLE",
|
| 23 |
+
specialty_id: Optional[int] = None,
|
| 24 |
+
paciente_id: Optional[int] = None,
|
| 25 |
+
hospital_id: str = "",
|
| 26 |
+
contraindicaciones_incompatibles: Optional[list] = None,
|
| 27 |
+
ultima_actualizacion: Optional[datetime] = None,
|
| 28 |
+
ultimo_movimiento_tipo: Optional[str] = None,
|
| 29 |
+
):
|
| 30 |
+
self.id = id
|
| 31 |
+
self.numero = numero
|
| 32 |
+
self.piso = piso
|
| 33 |
+
self.sector = sector
|
| 34 |
+
self.tipo = tipo
|
| 35 |
+
self.estado = estado
|
| 36 |
+
self.specialty_id = specialty_id
|
| 37 |
+
self.paciente_id = paciente_id
|
| 38 |
+
self.hospital_id = hospital_id
|
| 39 |
+
self.contraindicaciones_incompatibles = contraindicaciones_incompatibles or []
|
| 40 |
+
self.ultima_actualizacion = _parse_dt(ultima_actualizacion)
|
| 41 |
+
self.ultimo_movimiento_tipo = ultimo_movimiento_tipo
|
| 42 |
+
|
| 43 |
+
@classmethod
|
| 44 |
+
def from_dict(cls, d: dict) -> "Cama":
|
| 45 |
+
return cls(
|
| 46 |
+
id=d.get("id"),
|
| 47 |
+
numero=d.get("numero", 0),
|
| 48 |
+
piso=d.get("piso", 0),
|
| 49 |
+
sector=d.get("sector", ""),
|
| 50 |
+
tipo=d.get("tipo", ""),
|
| 51 |
+
estado=d.get("estado", "DISPONIBLE"),
|
| 52 |
+
specialty_id=d.get("specialty_id"),
|
| 53 |
+
paciente_id=d.get("paciente_id"),
|
| 54 |
+
hospital_id=d.get("hospital_id", ""),
|
| 55 |
+
contraindicaciones_incompatibles=d.get("contraindicaciones_incompatibles") or [],
|
| 56 |
+
ultima_actualizacion=d.get("ultima_actualizacion"),
|
| 57 |
+
ultimo_movimiento_tipo=d.get("ultimo_movimiento_tipo"),
|
| 58 |
+
)
|