add import audit trail, live indicator recompute, reportes endpoints; production hardening
This commit is contained in:
@@ -7,7 +7,7 @@ from datetime import date, datetime
|
||||
from enum import Enum
|
||||
from typing import Annotated, Any, Optional, Type
|
||||
|
||||
from pydantic import BeforeValidator, StringConstraints
|
||||
from pydantic import BeforeValidator, StringConstraints, ValidationInfo
|
||||
|
||||
from core.columns import HOSPITAL_ID_LENGTH
|
||||
from extraction import ExtractionError
|
||||
@@ -18,9 +18,9 @@ from extraction.signals import EegRecording
|
||||
HospitalId = Annotated[str, StringConstraints(max_length=HOSPITAL_ID_LENGTH)]
|
||||
|
||||
|
||||
def required_str(v: Any, *, field_name: str) -> str:
|
||||
def required_str(v: Any, info: ValidationInfo) -> str:
|
||||
if v is None or str(v).strip() == "":
|
||||
raise ValueError(f"{field_name} es requerido")
|
||||
raise ValueError(f"{info.field_name} es requerido")
|
||||
return str(v).strip()
|
||||
|
||||
|
||||
@@ -34,10 +34,10 @@ def optional_str(v: Any) -> Any:
|
||||
_DAY_FIRST_FORMATS = ("%d/%m/%Y", "%d-%m-%Y", "%d.%m.%Y", "%Y/%m/%d")
|
||||
|
||||
|
||||
def required_date(v: Any, *, field_name: str) -> Any:
|
||||
def required_date(v: Any, info: ValidationInfo) -> Any:
|
||||
coerced = coerce_date(v)
|
||||
if coerced is None:
|
||||
raise ValueError(f"{field_name} es requerido")
|
||||
raise ValueError(f"{info.field_name} es requerido")
|
||||
return coerced
|
||||
|
||||
|
||||
@@ -67,16 +67,6 @@ def coerce_date(v: Any) -> Any:
|
||||
return v
|
||||
|
||||
|
||||
def coerce_datetime(v: Any) -> Any:
|
||||
if v is None or (isinstance(v, str) and not v.strip()):
|
||||
return None
|
||||
if isinstance(v, datetime):
|
||||
return v
|
||||
if isinstance(v, str):
|
||||
return datetime.fromisoformat(v.strip())
|
||||
return v
|
||||
|
||||
|
||||
class Catalog:
|
||||
"""A closed set of valid values for one field: an `Enum`'s values plus any
|
||||
aliases for them, matched case-insensitively. Built from the Enum because
|
||||
@@ -132,10 +122,6 @@ def CatalogValue(catalog: Catalog): # noqa: N802 (used in annotations, like a t
|
||||
return Annotated[Optional[str], BeforeValidator(catalog.coerce), catalog]
|
||||
|
||||
|
||||
def coerce_alias(v: Any, catalog: Catalog) -> Any:
|
||||
return catalog.coerce(v)
|
||||
|
||||
|
||||
def coerce_postal_code(v: Any) -> Any:
|
||||
"""Mexican postal codes are 5 digits. A numeric source (Excel cell, SQL
|
||||
INT column) has already dropped the leading zero, so restore it."""
|
||||
@@ -170,3 +156,15 @@ def edf_file(v: Any) -> Any:
|
||||
except ExtractionError as exc:
|
||||
raise ValueError(f"no es un archivo EDF válido: {exc}") from exc
|
||||
return bytes(v)
|
||||
|
||||
|
||||
# Field types. Blank cells become None; required ones say which field is missing.
|
||||
OptionalStr = Annotated[Optional[str], BeforeValidator(optional_str)]
|
||||
# A TEA record's own id as the source sends it (its primary key is String(36)).
|
||||
SourceId = Annotated[Optional[Annotated[str, StringConstraints(max_length=36)]], BeforeValidator(optional_str)]
|
||||
RequiredStr = Annotated[str, BeforeValidator(required_str)]
|
||||
RequiredHospitalId = Annotated[HospitalId, BeforeValidator(required_str)]
|
||||
FlexDate = Annotated[Optional[date], BeforeValidator(coerce_date)]
|
||||
RequiredDate = Annotated[date, BeforeValidator(required_date)]
|
||||
PostalCode = Annotated[Optional[CodigoPostal], BeforeValidator(coerce_postal_code)]
|
||||
EdfFile = Annotated[Optional[bytes], BeforeValidator(edf_file)]
|
||||
|
||||
Reference in New Issue
Block a user