Merge pull request 'fix: serializar dates/Decimal en LogService con DjangoJSONEncoder' (#40) from pre-dev into dev
All checks were successful
DEPLOY_MULTI_BRACH/pipeline/head This commit looks good
All checks were successful
DEPLOY_MULTI_BRACH/pipeline/head This commit looks good
Reviewed-on: #40
This commit was merged in pull request #40.
This commit is contained in:
@@ -1,10 +1,25 @@
|
|||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
from django.core.serializers.json import DjangoJSONEncoder
|
||||||
from . import utils
|
from . import utils
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def _json_safe(value):
|
||||||
|
"""
|
||||||
|
Convierte cualquier valor a un tipo seguro para JSONField.
|
||||||
|
Usa DjangoJSONEncoder para manejar date, datetime, Decimal, UUID, etc.
|
||||||
|
"""
|
||||||
|
if value is None or value == '':
|
||||||
|
return value
|
||||||
|
try:
|
||||||
|
return json.loads(json.dumps(value, cls=DjangoJSONEncoder))
|
||||||
|
except Exception:
|
||||||
|
return str(value)
|
||||||
|
|
||||||
|
|
||||||
class LogService:
|
class LogService:
|
||||||
"""
|
"""
|
||||||
Servicio centralizado de gestión de logs.
|
Servicio centralizado de gestión de logs.
|
||||||
@@ -38,7 +53,7 @@ class LogService:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _ejecutar_log(caller, request, log_id, path, user, body_request, body_response, status_code):
|
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
|
# Importación diferida para evitar problemas de arranque si la BD no está lista
|
||||||
from backend_admin.models import Log
|
from backend_admin.models import Log
|
||||||
|
|
||||||
# Determinar la app llamante a partir del módulo de la vista
|
# Determinar la app llamante a partir del módulo de la vista
|
||||||
@@ -87,10 +102,11 @@ class LogService:
|
|||||||
if path:
|
if path:
|
||||||
datos_a_actualizar['path'] = path
|
datos_a_actualizar['path'] = path
|
||||||
if body_request is not None:
|
if body_request is not None:
|
||||||
datos_a_actualizar['request'] = body_request
|
# _json_safe convierte dates, Decimals, etc. a tipos JSON válidos
|
||||||
|
datos_a_actualizar['request'] = _json_safe(body_request)
|
||||||
if body_response is not None:
|
if body_response is not None:
|
||||||
datos_a_actualizar['response'] = body_response
|
datos_a_actualizar['response'] = _json_safe(body_response)
|
||||||
if status_code:
|
if status_code is not None:
|
||||||
datos_a_actualizar['status_code'] = str(status_code)
|
datos_a_actualizar['status_code'] = str(status_code)
|
||||||
|
|
||||||
Log.objects.filter(pk=log_id).update(**datos_a_actualizar)
|
Log.objects.filter(pk=log_id).update(**datos_a_actualizar)
|
||||||
|
|||||||
Reference in New Issue
Block a user