Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
m3_webInterface
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
m3
m3_webInterface
Commits
e31273c7
Commit
e31273c7
authored
Mar 31, 2025
by
Ulises Morales Ramírez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
manejo del get
parent
d282e884
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
48 additions
and
83 deletions
+48
-83
views.py
catalog/views.py
+48
-83
No files found.
catalog/views.py
View file @
e31273c7
...
@@ -586,93 +586,58 @@ import requests
...
@@ -586,93 +586,58 @@ import requests
from
django.http
import
JsonResponse
,
HttpResponseNotAllowed
from
django.http
import
JsonResponse
,
HttpResponseNotAllowed
from
django.core.management
import
call_command
from
django.core.management
import
call_command
from
django.views.decorators.csrf
import
csrf_exempt
from
django.views.decorators.csrf
import
csrf_exempt
import
os
from
django.http
import
JsonResponse
from
django.views.decorators.csrf
import
csrf_exempt
from
django.conf
import
settings
import
subprocess
@
csrf_exempt
@
csrf_exempt
def
run_update_and_report
(
request
):
def
run_update_and_report
(
request
):
if
request
.
method
!=
'POST'
:
if
request
.
method
!=
'POST'
:
return
HttpResponseNotAllowed
([
'POST'
])
return
JsonResponse
({
'status'
:
'error'
,
'message'
:
'Only POST requests are allowed'
},
status
=
405
)
# Inicializar variables para recopilar resultados y errores
results
=
{
'status'
:
'success'
,
'messages'
:
[],
'errors'
:
[],
'api_response'
:
None
}
status_code
=
200
try
:
# Leer y validar JSON del request
try
:
data
=
json
.
loads
(
request
.
body
)
except
json
.
JSONDecodeError
as
e
:
results
[
'status'
]
=
'partial_success'
results
[
'errors'
]
.
append
(
'JSON inválido'
)
status_code
=
400
data
=
{}
# Usar valores por defecto
db_path
=
data
.
get
(
'db_path'
,
'/data/m3/news/'
)
# Get the base directory (where manage.py is located)
json_output_path
=
data
.
get
(
'output_path'
,
'catalog/static/js/data.js'
)
base_dir
=
settings
.
BASE_DIR
script_path
=
os
.
path
.
join
(
base_dir
,
'update.sh'
)
# Ejecutar updateDB
# Check if the script exists
try
:
if
not
os
.
path
.
exists
(
script_path
):
call_command
(
'updateDB'
,
db_path
)
return
JsonResponse
({
'status'
:
'error'
,
'message'
:
'update.sh not found'
},
status
=
404
)
results
[
'messages'
]
.
append
(
'updateDB ejecutado correctamente'
)
except
Exception
as
e
:
results
[
'status'
]
=
'partial_success'
results
[
'errors'
]
.
append
(
f
'Error en updateDB: {str(e)}'
)
# No hacemos return, continuamos con el siguiente comando
# Ejecutar report
# Check if the script is executable
if
not
os
.
access
(
script_path
,
os
.
X_OK
):
try
:
try
:
call_command
(
'report'
,
json_output_path
)
os
.
chmod
(
script_path
,
0o755
)
# Make the script executable
results
[
'messages'
]
.
append
(
f
'Reporte generado en {json_output_path}'
)
except
Exception
as
e
:
except
Exception
as
e
:
results
[
'status'
]
=
'partial_success'
return
JsonResponse
({
'status'
:
'error'
,
'message'
:
f
'Cannot make script executable: {str(e)}'
},
status
=
500
)
results
[
'errors'
]
.
append
(
f
'Error en report: {str(e)}'
)
# Obtener la última fecha de ejecución
try
:
try
:
last_execution_date
=
read_last_execution_date
()
or
datetime
.
date
.
today
()
.
strftime
(
'
%
Y-
%
m-
%
d'
)
# Run the script and capture output
fecha_inicio
=
last_execution_date
result
=
subprocess
.
run
(
fecha_final
=
(
datetime
.
date
.
today
()
-
datetime
.
timedelta
(
days
=
1
))
.
strftime
(
'
%
Y-
%
m-
%
d'
)
[
script_path
],
except
Exception
as
e
:
check
=
True
,
results
[
'status'
]
=
'partial_success'
stdout
=
subprocess
.
PIPE
,
results
[
'errors'
]
.
append
(
f
'Error al leer fecha de ejecución: {str(e)}'
)
stderr
=
subprocess
.
PIPE
,
# Usar fechas por defecto si hay error
text
=
True
fecha_inicio
=
datetime
.
date
.
today
()
.
strftime
(
'
%
Y-
%
m-
%
d'
)
)
fecha_final
=
(
datetime
.
date
.
today
()
-
datetime
.
timedelta
(
days
=
1
))
.
strftime
(
'
%
Y-
%
m-
%
d'
)
output
=
{
'status'
:
'success'
,
'return_code'
:
result
.
returncode
,
'stdout'
:
result
.
stdout
,
'stderr'
:
result
.
stderr
}
return
JsonResponse
(
output
)
# Enviar solicitud POST a API externa
except
subprocess
.
CalledProcessError
as
e
:
try
:
return
JsonResponse
({
payload
=
{
"fecha_inicio"
:
fecha_inicio
,
"fecha_final"
:
fecha_final
}
'status'
:
'error'
,
response
=
requests
.
post
(
"https://em.geoint.mx/m3/processnews"
,
json
=
payload
,
timeout
=
10
)
'message'
:
'Script execution failed'
,
response
.
raise_for_status
()
'return_code'
:
e
.
returncode
,
results
[
'api_response'
]
=
response
.
json
()
'stdout'
:
e
.
stdout
,
results
[
'messages'
]
.
append
(
'API externa llamada correctamente'
)
'stderr'
:
e
.
stderr
except
requests
.
exceptions
.
RequestException
as
e
:
},
status
=
500
)
results
[
'status'
]
=
'partial_success'
results
[
'errors'
]
.
append
(
f
'Error en API externa: {str(e)}'
)
# Guardar nueva fecha de ejecución
try
:
write_execution_date
()
results
[
'messages'
]
.
append
(
'Fecha de ejecución actualizada'
)
except
Exception
as
e
:
results
[
'status'
]
=
'partial_success'
results
[
'errors'
]
.
append
(
f
'Error al escribir fecha de ejecución: {str(e)}'
)
except
Exception
as
e
:
except
Exception
as
e
:
# Este bloque captura errores inesperados no manejados en los bloques try internos
return
JsonResponse
({
'status'
:
'error'
,
'message'
:
str
(
e
)},
status
=
500
)
print
(
traceback
.
format_exc
())
\ No newline at end of file
results
[
'status'
]
=
'error'
results
[
'errors'
]
.
append
(
f
'Error inesperado: {str(e)}'
)
status_code
=
500
# Determinar el código de estado final
if
results
[
'status'
]
==
'error'
:
status_code
=
500
elif
results
[
'status'
]
==
'partial_success'
and
status_code
==
200
:
status_code
=
207
# Multi-Status, si todo fue parcial
return
JsonResponse
(
results
,
status
=
status_code
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment