feat: añadir app general con LogService centralizado
- Crear app/general con estructura estándar del proyecto: · utilidades/acciones.py → LogService.gestionar_log() (única fuente de logs) · utilidades/utils.py → get_client_ip() · utilidades/custom_errors.py → ValidationError, ExternalServiceError, NotFoundError · exception.py, request.py, serializers.py, validaciones/ - Registrar 'general' en INSTALLED_APPS y añadir general/ a urls.py - Refactorizar promociones/views.py para usar LogService en lugar de Log directo Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -2,45 +2,45 @@ from rest_framework.views import APIView
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework_simplejwt.authentication import JWTAuthentication
|
||||
from django.http import JsonResponse
|
||||
from backend_admin.models import Log
|
||||
|
||||
from general.utilidades.acciones import LogService
|
||||
from .acciones import getData
|
||||
from django.utils import timezone # Esta es la forma correcta
|
||||
|
||||
|
||||
class PromocionObtener(APIView):
|
||||
authentication_classes = [JWTAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def post(self, request):
|
||||
# --- BLOQUE 1: LOG INITIATION ---
|
||||
log_entry = Log.objects.create(
|
||||
user=request.user.username,
|
||||
path='promociones/obtener/',
|
||||
method='POST',
|
||||
status_code='0'
|
||||
)
|
||||
path = '/promociones/obtener/'
|
||||
|
||||
print('llega a despues de log entry')
|
||||
# --- LOG: inicio de la petición ---
|
||||
log_id = LogService.gestionar_log(self, request, path=path)
|
||||
|
||||
try:
|
||||
# --- BLOQUE 2: DATA CLEANING ---
|
||||
# --- BLOQUE 2: limpieza y validación del body ---
|
||||
data = request.data
|
||||
log_entry.request = data
|
||||
log_entry.save()
|
||||
status = 100
|
||||
LogService.gestionar_log(self, request, log_id=log_id, path=path, body_request=data, status_code=status)
|
||||
|
||||
params = {'id': data.get('id'), 'activo': data.get('activo')}
|
||||
|
||||
# --- BLOQUE 3: ACTION CALL ---
|
||||
# Aquí ya recibimos las fechas como strings gracias al paso 1
|
||||
except Exception as error:
|
||||
response = {'body': {'data': [], 'error': str(error)}, 'mensaje': str(error)}
|
||||
status = 400
|
||||
LogService.gestionar_log(self, request, log_id=log_id, path=path, body_response=response, status_code=status)
|
||||
return JsonResponse(response, status=status, safe=False)
|
||||
|
||||
try:
|
||||
# --- BLOQUE 3: llamada a la acción ---
|
||||
resultado = getData(params)
|
||||
response = resultado
|
||||
status = 200
|
||||
LogService.gestionar_log(self, request, log_id=log_id, path=path, body_response=response, status_code=status)
|
||||
return JsonResponse(response, safe=False, status=status)
|
||||
|
||||
# --- BLOQUE 4: LOG CLOSURE & RESPONSE ---
|
||||
log_entry.response = {"count": len(resultado)} # No guardes todo el JSON si es muy grande
|
||||
log_entry.status_code = '200'
|
||||
log_entry.save()
|
||||
|
||||
return JsonResponse(resultado, safe=False, status=200)
|
||||
|
||||
except Exception as e:
|
||||
log_entry.status_code = '500'
|
||||
log_entry.response = {'error': str(e)}
|
||||
log_entry.save()
|
||||
return JsonResponse({'error': str(e)}, status=500)
|
||||
except Exception as error:
|
||||
response = {'body': {'data': [], 'error': str(error)}, 'error': str(error)}
|
||||
status = 500
|
||||
LogService.gestionar_log(self, request, log_id=log_id, path=path, body_response=response, status_code=status)
|
||||
return JsonResponse(response, status=status, safe=False)
|
||||
|
||||
Reference in New Issue
Block a user