manejo del get

parent d5ad079b
......@@ -579,46 +579,70 @@ def write_execution_date(log_file='log.txt'):
with open(log_file, 'w') as file:
file.write(today.strftime('%Y-%m-%d'))
from django.http import JsonResponse, HttpResponseNotAllowed
import json
import datetime
import traceback
import requests
from django.http import JsonResponse, HttpResponseNotAllowed
from django.core.management import call_command
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
def run_update_and_report(request):
if request.method == 'POST':
if request.method != 'POST':
return HttpResponseNotAllowed(['POST'])
try:
# Leer y validar JSON del request
try:
# Leer parámetros del request
data = json.loads(request.body)
except json.JSONDecodeError:
return JsonResponse({'status': 'error', 'message': 'JSON inválido'}, status=400)
db_path = data.get('db_path', '/data/m3/news/')
json_output_path = data.get('output_path', 'catalog/static/js/data.js')
# Ejecutar los comandos
# Ejecutar los comandos de Django con manejo de errores
try:
call_command('updateDB', db_path)
except Exception as e:
return JsonResponse({'status': 'error', 'message': f'Error en updateDB: {str(e)}'}, status=500)
try:
call_command('report', json_output_path)
except Exception as e:
return JsonResponse({'status': 'error', 'message': f'Error en report: {str(e)}'}, status=500)
# Obtener la última fecha de ejecución
try:
last_execution_date = read_last_execution_date() or datetime.date.today().strftime('%Y-%m-%d')
except Exception as e:
return JsonResponse({'status': 'error', 'message': f'Error al leer fecha de ejecución: {str(e)}'}, status=500)
# Obtener fechas
last_execution_date = read_last_execution_date()
if not last_execution_date:
last_execution_date = datetime.date.today().strftime('%Y-%m-%d')
fecha_inicio = last_execution_date
fecha_final = (datetime.date.today() - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
# Enviar POST a la URL
payload = {
"fecha_inicio": fecha_inicio,
"fecha_final": fecha_final
}
response = requests.post("https://em.geoint.mx/m3/processnews", json=payload)
response.raise_for_status()
# Enviar solicitud POST a API externa
payload = {"fecha_inicio": fecha_inicio, "fecha_final": fecha_final}
try:
response = requests.post("https://em.geoint.mx/m3/processnews", json=payload, timeout=10)
response.raise_for_status() # Lanza error si la respuesta no es 200
except requests.exceptions.RequestException as e:
return JsonResponse({'status': 'error', 'message': f'Error en API externa: {str(e)}'}, status=500)
# Guardar nueva fecha de ejecución
try:
write_execution_date()
return JsonResponse({'status': 'success', 'message': f'Report generated at {json_output_path}', 'api_response': response.json()})
except Exception as e:
return JsonResponse({'status': 'error', 'message': str(e)}, status=500)
return JsonResponse({'status': 'error', 'message': f'Error al escribir fecha de ejecución: {str(e)}'}, status=500)
return HttpResponseNotAllowed(['POST'])
return JsonResponse({
'status': 'success',
'message': f'Reporte generado en {json_output_path}',
'api_response': response.json()
})
except Exception as e:
print(traceback.format_exc()) # Muestra error completo en consola
return JsonResponse({'status': 'error', 'message': f'Error inesperado: {str(e)}'}, status=500)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment