Merge branch 'feature/BOMs-csv' into feature/templates-csv
This commit is contained in:
@@ -17,8 +17,13 @@ from api.v1.modules.public.reference_data.incoterms.models import Incoterm
|
||||
from api.v1.modules.public.reference_data.invoice_types.models import InvoiceType
|
||||
from api.v1.modules.public.reference_data.material_types.models import MaterialType
|
||||
from api.v1.modules.public.reference_data.payment_methods.models import PaymentMethod
|
||||
# Orden: PedimentoCode y RegimenPedimento antes de CodePedimentoRegimen para que
|
||||
# SQLAlchemy resuelva los nombres en relationship() al configurar el mapper
|
||||
from api.v1.modules.public.reference_data.pedimento_codes.models import PedimentoCode
|
||||
from api.v1.modules.public.reference_data.pedimento_regimens.models import RegimenPedimento
|
||||
from api.v1.modules.public.reference_data.code_pedimento_regimens.models import (
|
||||
CodePedimentoRegimen,
|
||||
)
|
||||
from api.v1.modules.public.reference_data.sectors.models import Sector
|
||||
from api.v1.modules.public.reference_data.states.models import State
|
||||
from api.v1.modules.public.reference_data.transport_modes.models import TransportMode
|
||||
@@ -134,6 +139,20 @@ logger = logging.getLogger(__name__)
|
||||
register_exception_handlers(app)
|
||||
|
||||
|
||||
def _cors_headers_for_request(request: Request):
|
||||
"""Return CORS headers if request Origin is allowed (so error responses don't get blocked by browser)."""
|
||||
origin = request.headers.get("origin")
|
||||
if not origin:
|
||||
return {}
|
||||
allowed = settings.cors_origins_list
|
||||
if origin in allowed:
|
||||
return {
|
||||
"Access-Control-Allow-Origin": origin,
|
||||
"Access-Control-Allow-Credentials": "true",
|
||||
}
|
||||
return {}
|
||||
|
||||
|
||||
# Add validation error handler
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def validation_exception_handler(request: Request, exc: RequestValidationError):
|
||||
@@ -141,12 +160,29 @@ async def validation_exception_handler(request: Request, exc: RequestValidationE
|
||||
f"Validation error for {request.method} {request.url.path}: {exc.errors()}"
|
||||
)
|
||||
logger.error(f"Request body: {await request.body()}")
|
||||
return JSONResponse(
|
||||
response = JSONResponse(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
content={"detail": exc.errors(), "body": exc.body},
|
||||
)
|
||||
for k, v in _cors_headers_for_request(request).items():
|
||||
response.headers[k] = v
|
||||
return response
|
||||
|
||||
|
||||
# Add HTTP exception handler
|
||||
@app.exception_handler(HTTPException)
|
||||
async def http_exception_handler(request: Request, exc: HTTPException):
|
||||
logger.error(
|
||||
f"HTTP {exc.status_code} for {request.method} {request.url.path}: {exc.detail}"
|
||||
)
|
||||
response = JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={"detail": exc.detail},
|
||||
)
|
||||
for k, v in _cors_headers_for_request(request).items():
|
||||
response.headers[k] = v
|
||||
return response
|
||||
|
||||
def run_migrations():
|
||||
subprocess.run(["alembic", "upgrade", "head"], check=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user