This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
from django.http import JsonResponse
|
||||
from .actions import Admin
|
||||
import logging
|
||||
import json
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from django.utils import timezone
|
||||
from .models import Log
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -25,4 +31,65 @@ def status_view(request):
|
||||
|
||||
# BLOQUE 4: Log de cierre y retorno
|
||||
logger.info(f"FIN - Health Check completado. Status: {status_code}")
|
||||
return JsonResponse(response_data, status=status_code)
|
||||
return JsonResponse(response_data, status=status_code)
|
||||
|
||||
@csrf_exempt
|
||||
@staticmethod
|
||||
def api_token(request):
|
||||
"""
|
||||
Endpoint: api/token/
|
||||
Patrón: 4 bloques con persistencia en Log DB.
|
||||
"""
|
||||
# --- BLOQUE 1: LOG INITIATION ---
|
||||
logger.info("INICIO - Petición de JWT (api/token/)")
|
||||
|
||||
# Iniciamos el registro en la base de datos (Estándar compañeros)
|
||||
log_entry = Log.objects.create(
|
||||
user='anonimo',
|
||||
path='api/token/',
|
||||
method='POST',
|
||||
createdAt=timezone.now(),
|
||||
status_code='0'
|
||||
)
|
||||
|
||||
try:
|
||||
if request.method == 'POST':
|
||||
# --- BLOQUE 2: DATA CLEANING ---
|
||||
body_data = json.loads(request.body)
|
||||
log_entry.request = body_data # Guardamos lo que entró
|
||||
log_entry.save()
|
||||
|
||||
params = {
|
||||
'username': body_data.get('username'),
|
||||
'password': body_data.get('password')
|
||||
}
|
||||
|
||||
# --- BLOQUE 3: ACTION CALL ---
|
||||
admin_logic = Admin()
|
||||
resultado = admin_logic.obtener_token_action(params)
|
||||
|
||||
# --- BLOQUE 4: LOG CLOSURE & RESPONSE ---
|
||||
if resultado:
|
||||
status = 200
|
||||
log_entry.user = resultado['user']
|
||||
log_entry.response = resultado
|
||||
log_entry.status_code = str(status)
|
||||
log_entry.updatedAt = timezone.now()
|
||||
log_entry.save()
|
||||
|
||||
logger.info(f"FIN - JWT generado para: {log_entry.user}")
|
||||
return JsonResponse(resultado, status=status)
|
||||
else:
|
||||
status = 401
|
||||
response_error = {"error": "Credenciales inválidas"}
|
||||
log_entry.status_code = str(status)
|
||||
log_entry.response = response_error
|
||||
log_entry.save()
|
||||
return JsonResponse(response_error, status=status)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"ERROR CRÍTICO en api_token: {str(e)}")
|
||||
log_entry.status_code = '500'
|
||||
log_entry.response = {'error': str(e)}
|
||||
log_entry.save()
|
||||
return JsonResponse({'error': 'Error interno'}, status=500)
|
||||
Reference in New Issue
Block a user