nueva api para reportes y update

parent bfedde89
...@@ -41,6 +41,8 @@ urlpatterns = [ ...@@ -41,6 +41,8 @@ urlpatterns = [
url(r'^settings/password$', views.settingsView, name='settings-password'), url(r'^settings/password$', views.settingsView, name='settings-password'),
url(r'^settings/searches$', views.settingsSearches, name='settings-search'), url(r'^settings/searches$', views.settingsSearches, name='settings-search'),
url(r'^settings/apikey$', views.settingsApiKey, name='api-key'), url(r'^settings/apikey$', views.settingsApiKey, name='api-key'),
#----------------------------------------------------------------------------
url(r'^api/run_update_and_report/', views.run_update_and_report, name='run_update_and_report'),
] ]
...@@ -54,6 +54,11 @@ from django.db.models import Value ...@@ -54,6 +54,11 @@ from django.db.models import Value
import html import html
import string import string
from django.contrib.auth import logout from django.contrib.auth import logout
from django.http import JsonResponse
from django.core.management import call_command
from django.views.decorators.csrf import csrf_exempt
import json
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
#------------------------------------------------------------------------------- #-------------------------------------------------------------------------------
...@@ -560,3 +565,51 @@ def audioPlayerTest(request, publisher, start, end): ...@@ -560,3 +565,51 @@ def audioPlayerTest(request, publisher, start, end):
filelist=[ f[:-5] for f in listAudioFiles(publisher, int(start), int(end))] filelist=[ f[:-5] for f in listAudioFiles(publisher, int(start), int(end))]
return render(request,'new/audioPlayerTest.html',{"filelist":filelist, "publisher":publisher}) return render(request,'new/audioPlayerTest.html',{"filelist":filelist, "publisher":publisher})
#----------------------------------|----------------------------------------------
def read_last_execution_date(log_file='log.txt'):
try:
with open(log_file, 'r') as file:
return file.read().strip()
except FileNotFoundError:
return None
def write_execution_date(log_file='log.txt'):
today = datetime.date.today()
with open(log_file, 'w') as file:
file.write(today.strftime('%Y-%m-%d'))
@csrf_exempt
def run_update_and_report(request):
if request.method == 'POST':
try:
# Leer parámetros del request
data = json.loads(request.body)
db_path = data.get('db_path', '/data/m3/news/')
json_output_path = data.get('output_path', 'catalog/static/js/data.js')
# Ejecutar los comandos
call_command('updateDB', db_path)
call_command('report', json_output_path)
# 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()
# Guardar nueva fecha de ejecución
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)
\ No newline at end of file
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