From 56a34d7357bd3074ab8c52089a39bd6ef70a061e Mon Sep 17 00:00:00 2001 From: Guillermo Valdes Date: Fri, 21 Feb 2025 05:55:18 -0600 Subject: [PATCH 1/2] Nueva variable de entorno estado clave --- README.md | 4 ++ pjecz_carina_api_key/routers/exh_exhortos.py | 39 +++++++++++--------- pjecz_carina_api_key/settings.py | 3 ++ tests/README.md | 1 + tests/__init__.py | 1 + tests/test_000_consultar_estados.py | 8 ++-- tests/test_000_consultar_municipios.py | 8 ++-- tests/test_010_consultar_materias.py | 6 --- tests/test_020_enviar_exhorto.py | 8 ---- tests/test_030_enviar_exhorto_archivos.py | 6 --- tests/test_040_consultar_exhorto.py | 7 ---- tests/test_051_enviar_respuesta.py | 9 ----- tests/test_052_enviar_respuesta_archivos.py | 8 ---- tests/test_060_enviar_actualizacion.py | 5 --- tests/test_071_enviar_promocion.py | 2 - tests/test_072_enviar_promocion_archivos.py | 2 - 16 files changed, 39 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index a928936..64957b7 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,9 @@ DB_PASS=XXXXXXXXXXXX # Google Cloud Storage CLOUD_STORAGE_DEPOSITO=XXXXXXXXXXXX +# Clave INEGI del Estado, 05 = Coahuila de Zaragoza +ESTADO_CLAVE=05 + # Origins ORIGINS=http://127.0.0.1:3000 @@ -102,6 +105,7 @@ then echo " DB_NAME: ${DB_NAME}" echo " DB_USER: ${DB_USER}" echo " DB_PASS: ${DB_PASS}" + echo " ESTADO_CLAVE: ${ESTADO_CLAVE}" echo " ORIGINS: ${ORIGINS}" echo " SALT: ${SALT}" echo diff --git a/pjecz_carina_api_key/routers/exh_exhortos.py b/pjecz_carina_api_key/routers/exh_exhortos.py index 05c2424..7452c79 100644 --- a/pjecz_carina_api_key/routers/exh_exhortos.py +++ b/pjecz_carina_api_key/routers/exh_exhortos.py @@ -34,12 +34,10 @@ ) from ..schemas.exh_exhortos_archivos import ExhExhortoArchivoItem from ..schemas.exh_exhortos_partes import ExhExhortoParteItem +from ..settings import Settings, get_settings exh_exhortos = APIRouter(prefix="/api/v5/exh_exhortos") -ESTADO_DESTINO_NOMBRE = "COAHUILA DE ZARAGOZA" -ESTADO_DESTINO_ID = 5 - def get_exhorto_with_exhorto_origen_id(database: Annotated[Session, Depends(get_db)], exhorto_origen_id: str) -> ExhExhorto: """Consultar un exhorto con su exhorto_origen_id""" @@ -77,15 +75,21 @@ def get_exhorto_with_folio_seguimiento(database: Annotated[Session, Depends(get_ return exh_exhorto -def get_municipio_destino(database: Annotated[Session, Depends(get_db)], municipio_num: int) -> Municipio: +def get_municipio_destino( + database: Annotated[Session, Depends(get_db)], + municipio_num: int, + settings: Annotated[Settings, Depends(get_settings)], +) -> Municipio: """Obtener el municipio de destino a partir de la clave INEGI""" municipio_destino_clave = str(municipio_num).zfill(3) try: municipio_destino = ( - database.query(Municipio).filter_by(estado_id=ESTADO_DESTINO_ID).filter_by(clave=municipio_destino_clave).one() + database.query(Municipio).filter_by(estado_id=settings.estado_clave).filter_by(clave=municipio_destino_clave).one() ) except (MultipleResultsFound, NoResultFound) as error: - raise MyNotExistsError(f"No existe el municipio {municipio_destino_clave} en {ESTADO_DESTINO_NOMBRE}") from error + raise MyNotExistsError( + f"No existe el municipio {municipio_destino_clave} en el estado {settings.estado_clave}" + ) from error return municipio_destino @@ -102,7 +106,7 @@ def get_municipio_origen(database: Annotated[Session, Depends(get_db)], estado_n database.query(Municipio).filter_by(estado_id=estado_origen.id).filter_by(clave=municipio_origen_clave).one() ) except (MultipleResultsFound, NoResultFound) as error: - raise MyNotExistsError(f"No existe el municipio {municipio_origen_clave} en {ESTADO_DESTINO_NOMBRE}") from error + raise MyNotExistsError(f"No existe el municipio {municipio_origen_clave} en {estado_origen_clave}") from error return municipio_origen @@ -322,6 +326,7 @@ async def consultar_exhorto_request( async def recibir_exhorto_request( current_user: Annotated[UsuarioInDB, Depends(get_current_active_user)], database: Annotated[Session, Depends(get_db)], + settings: Annotated[Settings, Depends(get_settings)], exh_exhorto_in: ExhExhortoIn, ): """Recepción de datos de un exhorto""" @@ -337,9 +342,9 @@ async def recibir_exhorto_request( errores.append("No es válido exhortoOrigenId") # Consultar nuestro estado - estado_destino = database.query(Estado).get(ESTADO_DESTINO_ID) + estado_destino = database.query(Estado).get(settings.estado_clave) if estado_destino is None: - errores.append(f"No existe el estado de destino {ESTADO_DESTINO_NOMBRE}") + errores.append(f"No existe el estado de destino {settings.estado_clave}") # Validar municipioDestinoId, obligatorio y es un identificador INEGI try: @@ -350,19 +355,19 @@ async def recibir_exhorto_request( # Consultar ExhExterno de nuestro estado estado_destino_exh_externo = database.query(ExhExterno).filter_by(estado_id=estado_destino.id).first() if estado_destino_exh_externo is None: - errores.append(f"No existe el registro de {ESTADO_DESTINO_NOMBRE} en exh_externos") + errores.append(f"No existe el registro del estado {settings.estado_clave} en exh_externos") # Tomar las materias de nuestro estado materias = estado_destino_exh_externo.materias - if materias is None: - errores.append(f"No hay materias para {ESTADO_DESTINO_NOMBRE}") + if materias is None or len(materias) == 0: + errores.append(f"No hay materias en el estado {settings.estado_clave} en exh_externos") - # Validar materiaClave, obligatorio + # Validar la materia materia_clave = safe_clave(exh_exhorto_in.materiaClave) - materia = next((materia for materia in materias if materia["clave"] == materia_clave), None) - if materia is None: - errores.append(f"No tiene la materia {materia_clave} en {ESTADO_DESTINO_NOMBRE}") - materia_nombre = materia["nombre"] + try: + materia_nombre = materias[materia_clave] + except KeyError as error: + errores.append(f"No tiene la materia {materia_clave} el estado {settings.estado_clave} en exh_externos") # Validar estadoOrigenId y municipioOrigenId, enteros obligatorios y son identificadores INEGI try: diff --git a/pjecz_carina_api_key/settings.py b/pjecz_carina_api_key/settings.py index 04ec051..edfb384 100644 --- a/pjecz_carina_api_key/settings.py +++ b/pjecz_carina_api_key/settings.py @@ -15,6 +15,7 @@ - DB_NAME - DB_USER - DB_PASS +- ESTADO_CLAVE - ORIGINS - SALT @@ -28,6 +29,7 @@ - pjecz_carina_api_key_db_name - pjecz_carina_api_key_db_user - pjecz_carina_api_key_db_pass +- pjecz_carina_api_key_estado_clave - pjecz_carina_api_key_origins - pjecz_carina_api_key_salt @@ -77,6 +79,7 @@ class Settings(BaseSettings): db_name: str = get_secret("db_name") db_pass: str = get_secret("db_pass") db_user: str = get_secret("db_user") + estado_clave: str = get_secret("estado_clave") origins: str = get_secret("origins") salt: str = get_secret("salt") tz: str = "America/Mexico_City" diff --git a/tests/README.md b/tests/README.md index 1edda6b..dfde523 100644 --- a/tests/README.md +++ b/tests/README.md @@ -12,6 +12,7 @@ Crear un archivo .env con las siguientes variables API_KEY=XXXXXXXX.XXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXXX API_BASE_URL=http://127.0.0.1:8000/v5 TIMEOUT=10 +ESTADO_CLAVE=05 FOLIO_SEGUIMIENTO=XXXXXXXXXXXXXXXX ARCHIVO_PDF_HASHSHA1= ARCHIVO_PDF_HASHSHA256= diff --git a/tests/__init__.py b/tests/__init__.py index a97819e..a5b492e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -11,6 +11,7 @@ "api_key": os.getenv("API_KEY", ""), "api_base_url": os.getenv("API_BASE_URL", "http://127.0.0.1:8000/v4"), "timeout": int(os.getenv("TIMEOUT", "10")), + "estado_clave": os.getenv("ESTADO_CLAVE", "05"), "folio_seguimiento": os.getenv("FOLIO_SEGUIMIENTO", ""), "archivo_pdf_hashsha1": os.getenv("ARCHIVO_PDF_HASHSHA1", ""), "archivo_pdf_hashsha256": os.getenv("ARCHIVO_PDF_HASHSHA256", ""), diff --git a/tests/test_000_consultar_estados.py b/tests/test_000_consultar_estados.py index 8513685..d0200e0 100644 --- a/tests/test_000_consultar_estados.py +++ b/tests/test_000_consultar_estados.py @@ -44,13 +44,13 @@ def test_get_estados(self): self.assertEqual("clave" in item, True) self.assertEqual("nombre" in item, True) - def test_get_estado_clave_05(self): - """GET method for estado with clave 05 nombre COAHUILA DE ZARAGOZA""" + def test_get_estado_clave(self): + """GET method for estado with clave""" - # Consultar el estado aguascalientes + # Consultar el estado definido en la variable de entorno ESTADO_CLAVE try: response = requests.get( - url=f"{config['api_base_url']}/estados/05", + url=f"{config['api_base_url']}/estados/{config['estado_clave']}", headers={"X-Api-Key": config["api_key"]}, timeout=config["timeout"], ) diff --git a/tests/test_000_consultar_municipios.py b/tests/test_000_consultar_municipios.py index a4dcff8..f9675c4 100644 --- a/tests/test_000_consultar_municipios.py +++ b/tests/test_000_consultar_municipios.py @@ -12,13 +12,13 @@ class TestsConsultarMunicipios(unittest.TestCase): """Tests Consultar Municipios""" - def test_get_municipios_estado_clave_05(self): - """GET method for municipios for estado with clave 05""" + def test_get_municipios_estado_clave(self): + """GET method for municipios for estado with clave""" - # Consultar los municipios + # Consultar los municipios del estado definido en la variable de entorno ESTADO_CLAVE try: response = requests.get( - url=f"{config['api_base_url']}/municipios/05", + url=f"{config['api_base_url']}/municipios/{config['estado_clave']}", headers={"X-Api-Key": config["api_key"]}, timeout=config["timeout"], ) diff --git a/tests/test_010_consultar_materias.py b/tests/test_010_consultar_materias.py index c061b7d..dc80436 100644 --- a/tests/test_010_consultar_materias.py +++ b/tests/test_010_consultar_materias.py @@ -1,11 +1,5 @@ """ Unit test - Consultar Materias - -Listado de materias del PJ exhortado. - -- GET /materias -- GET /materias/{MATERIA_CLAVE} - """ import unittest diff --git a/tests/test_020_enviar_exhorto.py b/tests/test_020_enviar_exhorto.py index d2d0dec..9b676f6 100644 --- a/tests/test_020_enviar_exhorto.py +++ b/tests/test_020_enviar_exhorto.py @@ -1,16 +1,8 @@ """ Unit test - Enviar Exhorto - -Se manda el esquema ExhExhortoIn. - -- POST /exh_exhortos - -Se recibe el esquema OneExhExhortoConfirmacionDatosExhortoRecibidoOut. """ import random -import string -import time import unittest from datetime import datetime diff --git a/tests/test_030_enviar_exhorto_archivos.py b/tests/test_030_enviar_exhorto_archivos.py index a79d289..e697552 100644 --- a/tests/test_030_enviar_exhorto_archivos.py +++ b/tests/test_030_enviar_exhorto_archivos.py @@ -1,11 +1,5 @@ """ Unit test - Enviar los Archivos del Exhorto - -Se manda exhortoOrigenId y el archivo - -- POST /exh_exhortos_archivos/upload - -Se recibe el esquema OneExhExhortoArchivoFileOut. """ import time diff --git a/tests/test_040_consultar_exhorto.py b/tests/test_040_consultar_exhorto.py index 9f69495..f1d63b4 100644 --- a/tests/test_040_consultar_exhorto.py +++ b/tests/test_040_consultar_exhorto.py @@ -1,12 +1,5 @@ """ Unit test - Consultar Exhorto - -Consultar exhorto enviado al PJ exhortado para ver su información. - -- DEBE CONFIGURAR en las variables de entorno FOLIO_SEGUIMIENTO -- GET /exh_exhortos/{FOLIO_SEGUIMIENTO} -- Se recibe el esquema OneExhExhortoOut. - """ import unittest diff --git a/tests/test_051_enviar_respuesta.py b/tests/test_051_enviar_respuesta.py index a6201b2..c32c10b 100644 --- a/tests/test_051_enviar_respuesta.py +++ b/tests/test_051_enviar_respuesta.py @@ -1,18 +1,9 @@ """ Unit test - Enviar Respuesta - -Se envían los datos que conforman la respuesta del exhorto. - -- DEBE CONFIGURAR en las variables de entorno FOLIO_SEGUIMIENTO -- Se manda el esquema ExhExhortoRecibirRespuestaIn que contiene archivos (ExhExhortoArchivoIn) y videos (ExhExhortoVideoIn). -- POST /exh_exhortos/responder -- Se recibe el esquema OneExhExhortoRecibirRespuestaOut. - """ import random import string -import time import unittest from datetime import datetime diff --git a/tests/test_052_enviar_respuesta_archivos.py b/tests/test_052_enviar_respuesta_archivos.py index be386bb..788ff62 100644 --- a/tests/test_052_enviar_respuesta_archivos.py +++ b/tests/test_052_enviar_respuesta_archivos.py @@ -1,13 +1,5 @@ """ Unit test - Enviar los Archivos de la Respuesta - -Se envían los documentos que conforman la respuesta del exhorto. - -- DEBE CONFIGURAR en las variables de entorno FOLIO_SEGUIMIENTO -- Se envía exhortoOrigenId, respuestaOrigenId y el archivo -- POST /exh_exhortos_archivos/responder_upload -- Se recibe el esquema OneExhExhortoArchivoRecibirRespuestaExhortoDataOut. - """ import time diff --git a/tests/test_060_enviar_actualizacion.py b/tests/test_060_enviar_actualizacion.py index 1ffced0..0764429 100644 --- a/tests/test_060_enviar_actualizacion.py +++ b/tests/test_060_enviar_actualizacion.py @@ -1,10 +1,5 @@ """ Unit test - Enviar Actualización - -Se puede realizar este proceso cuando el exhorto llega a una Oficialía y se turna a un Juzgado. - -Se manda el esquema ExhExhortoActualizarIn. - """ import random diff --git a/tests/test_071_enviar_promocion.py b/tests/test_071_enviar_promocion.py index 03906a7..008d840 100644 --- a/tests/test_071_enviar_promocion.py +++ b/tests/test_071_enviar_promocion.py @@ -1,7 +1,5 @@ """ Unit test - Enviar Promoción - -Se pueden enviar o recibir promociones sobre exhortos radicados. """ import random diff --git a/tests/test_072_enviar_promocion_archivos.py b/tests/test_072_enviar_promocion_archivos.py index 189a3ec..e8cde24 100644 --- a/tests/test_072_enviar_promocion_archivos.py +++ b/tests/test_072_enviar_promocion_archivos.py @@ -1,7 +1,5 @@ """ Unit test - Enviar Archivos de la Promoción - -Se pueden enviar o recibir promociones sobre exhortos radicados. """ import time From 381af6e28ac6c13907455fe4f20e986ba259d7f1 Mon Sep 17 00:00:00 2001 From: Guillermo Valdes Date: Fri, 21 Feb 2025 06:21:43 -0600 Subject: [PATCH 2/2] Correcciones al validar materia en exh exhortos --- pjecz_carina_api_key/routers/exh_exhortos.py | 28 ++++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/pjecz_carina_api_key/routers/exh_exhortos.py b/pjecz_carina_api_key/routers/exh_exhortos.py index 7452c79..93ad3a1 100644 --- a/pjecz_carina_api_key/routers/exh_exhortos.py +++ b/pjecz_carina_api_key/routers/exh_exhortos.py @@ -77,8 +77,8 @@ def get_exhorto_with_folio_seguimiento(database: Annotated[Session, Depends(get_ def get_municipio_destino( database: Annotated[Session, Depends(get_db)], - municipio_num: int, settings: Annotated[Settings, Depends(get_settings)], + municipio_num: int, ) -> Municipio: """Obtener el municipio de destino a partir de la clave INEGI""" municipio_destino_clave = str(municipio_num).zfill(3) @@ -348,26 +348,26 @@ async def recibir_exhorto_request( # Validar municipioDestinoId, obligatorio y es un identificador INEGI try: - municipio_destino = get_municipio_destino(database, exh_exhorto_in.municipioDestinoId) + municipio_destino = get_municipio_destino(database, settings, exh_exhorto_in.municipioDestinoId) except MyAnyError as error: errores.append(str(error)) - # Consultar ExhExterno de nuestro estado + # Consultar nuestro estado en exh_externos estado_destino_exh_externo = database.query(ExhExterno).filter_by(estado_id=estado_destino.id).first() if estado_destino_exh_externo is None: errores.append(f"No existe el registro del estado {settings.estado_clave} en exh_externos") - # Tomar las materias de nuestro estado - materias = estado_destino_exh_externo.materias - if materias is None or len(materias) == 0: - errores.append(f"No hay materias en el estado {settings.estado_clave} en exh_externos") - - # Validar la materia - materia_clave = safe_clave(exh_exhorto_in.materiaClave) - try: - materia_nombre = materias[materia_clave] - except KeyError as error: - errores.append(f"No tiene la materia {materia_clave} el estado {settings.estado_clave} en exh_externos") + # Validar materiaClave, que la tenga nuestro estado + if estado_destino_exh_externo: + materias = estado_destino_exh_externo.materias + if materias: + materia_clave = safe_clave(exh_exhorto_in.materiaClave) + try: + materia_nombre = materias[materia_clave] + except KeyError: + errores.append(f"No tiene la materia '{materia_clave}' el estado {settings.estado_clave} en exh_externos") + else: + errores.append(f"No hay materias en el estado {settings.estado_clave} en exh_externos") # Validar estadoOrigenId y municipioOrigenId, enteros obligatorios y son identificadores INEGI try: