Compare commits

..

3 Commits

Author SHA1 Message Date
3f95e92318 Merge pull request 'dev' (#39) from dev into master
All checks were successful
DEPLOY_MULTI_BRACH/pipeline/head This commit looks good
Reviewed-on: #39
2026-04-16 15:11:49 +00:00
aed8661331 Merge pull request 'fix: corregir ruta de data/ y hacer LogService resiliente a fallos de BD' (#38) from pre-dev into dev
All checks were successful
DEPLOY_MULTI_BRACH/pipeline/head This commit looks good
Reviewed-on: #38
2026-04-16 15:11:27 +00:00
juanjo
e908125d31 fix: corregir ruta de data/ y hacer LogService resiliente a fallos de BD
All checks were successful
DEPLOY_MULTI_BRACH/pipeline/head This commit looks good
- Mover data/.gitkeep a app/data/.gitkeep (Django busca app/data/db.sqlite3,
  no data/db.sqlite3 en la raíz del repo, porque BASE_DIR apunta a app/)
- Actualizar .gitignore para ignorar también app/data/*
- LogService.gestionar_log ahora captura excepciones internamente para que
  un fallo de BD no rompa la petición (log omitido con WARNING, no 500)
- Mover import de Log dentro del método para evitar problemas de arranque

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-16 17:07:23 +02:00
3 changed files with 35 additions and 17 deletions

2
.gitignore vendored
View File

@@ -13,6 +13,8 @@ local_postgres_data/
# Carpeta de datos (BD SQLite y similares), pero se mantiene la carpeta
data/*
!data/.gitkeep
app/data/*
!app/data/.gitkeep
*.pyc
# Bloquear todos los .env en cualquier carpeta
.env

0
app/data/.gitkeep Normal file
View File

View File

@@ -1,7 +1,9 @@
from backend_admin.models import Log
import logging
from django.utils import timezone
from . import utils
logger = logging.getLogger(__name__)
class LogService:
"""
@@ -11,6 +13,9 @@ class LogService:
Uso en vistas:
log_id = LogService.gestionar_log(self, request, path='/mi/path/')
LogService.gestionar_log(self, request, log_id=log_id, body_request=data, status_code=200)
Si la BD no está disponible, el log falla silenciosamente para no
interrumpir la petición del usuario.
"""
def gestionar_log(
@@ -23,8 +28,21 @@ class LogService:
body_response=None,
status_code=None,
):
# Determinar la app que llama a partir del módulo de la vista
modulo = self.__class__.__module__
try:
return LogService._ejecutar_log(
self, request, log_id, path, user, body_request, body_response, status_code
)
except Exception as e:
logger.warning('LogService.gestionar_log falló (log omitido): %s', str(e))
return log_id # Devuelve el log_id existente o None si era creación
@staticmethod
def _ejecutar_log(caller, request, log_id, path, user, body_request, body_response, status_code):
# Importación aquí para evitar problemas de arranque si la BD no está lista
from backend_admin.models import Log
# Determinar la app llamante a partir del módulo de la vista
modulo = caller.__class__.__module__
app_nombre = modulo.split('.')[0]
tag_header = request.headers.get('tag')
@@ -44,20 +62,18 @@ class LogService:
# --- CREACIÓN: primer registro del ciclo de vida de la petición ---
path_final = path if path else request.path
data_log = {
'user_id': 0,
'user': usuario_final,
'app_id': 0,
'remote_address': utils.get_client_ip(request),
'request': '',
'response': '',
'status_code': status_code if status_code else '0',
'path': path_final,
'method': request.method,
'createdAt': timezone.now(),
}
nuevo_log = Log.objects.create(**data_log)
nuevo_log = Log.objects.create(
user_id=0,
user=usuario_final,
app_id=0,
remote_address=utils.get_client_ip(request),
request='',
response='',
status_code=status_code if status_code else '0',
path=path_final,
method=request.method,
createdAt=timezone.now(),
)
return nuevo_log.pk
else: