solucion de eventos por pais

This commit is contained in:
jlimolina 2025-11-24 00:45:10 +01:00
parent fc06566928
commit 68a5528f2f
7 changed files with 796 additions and 14 deletions

115
app.py
View file

@ -1291,6 +1291,121 @@ def restore_completo():
return redirect(url_for("dashboard"))
@app.route("/eventos_pais")
def eventos_pais():
pais_id = request.args.get("pais_id") or None
page = max(int(request.args.get("page", 1) or 1), 1)
per_page = 30
offset = (page - 1) * per_page
lang = (request.args.get("lang") or DEFAULT_TRANSLATION_LANG or DEFAULT_LANG).lower()[:5]
with get_conn() as conn:
conn.autocommit = True
paises = get_paises(conn)
eventos = []
total_eventos = 0
noticias_por_evento = {}
pais_nombre = None
if pais_id:
with conn.cursor(cursor_factory=extras.DictCursor) as cur:
# 1) Eventos que tienen al menos una traducción cuya noticia es de ese país
cur.execute(
"""
SELECT
e.id,
e.titulo,
e.fecha_inicio,
e.fecha_fin,
e.n_noticias,
MAX(p.nombre) AS pais_nombre
FROM eventos e
JOIN traducciones t ON t.evento_id = e.id
JOIN noticias n ON n.id = t.noticia_id
JOIN paises p ON p.id = n.pais_id
WHERE n.pais_id = %s
GROUP BY e.id, e.titulo, e.fecha_inicio, e.fecha_fin, e.n_noticias
ORDER BY e.fecha_inicio DESC NULLS LAST, e.id DESC
LIMIT %s OFFSET %s;
""",
(int(pais_id), per_page, offset),
)
eventos = cur.fetchall()
# 2) Total de eventos distintos para ese país
cur.execute(
"""
SELECT COUNT(DISTINCT e.id)
FROM eventos e
JOIN traducciones t ON t.evento_id = e.id
JOIN noticias n ON n.id = t.noticia_id
WHERE n.pais_id = %s;
""",
(int(pais_id),),
)
total_eventos = cur.fetchone()[0] if cur.rowcount else 0
# 3) Cargar noticias asociadas a esos eventos (desde traducciones + noticias)
if eventos:
evento_ids = [e["id"] for e in eventos]
cur.execute(
"""
SELECT
t.evento_id,
n.id AS noticia_id,
n.url,
n.fecha,
n.imagen_url,
n.fuente_nombre,
n.titulo AS titulo_orig,
n.resumen AS resumen_orig,
t.id AS traduccion_id,
t.titulo_trad AS titulo_trad,
t.resumen_trad AS resumen_trad,
p.nombre AS pais_nombre
FROM traducciones t
JOIN noticias n ON n.id = t.noticia_id
LEFT JOIN paises p ON p.id = n.pais_id
WHERE t.evento_id = ANY(%s)
AND t.status = 'done'
AND t.lang_to = %s
ORDER BY t.evento_id, n.fecha DESC;
""",
(evento_ids, lang),
)
rows = cur.fetchall()
noticias_por_evento = {e["id"]: [] for e in eventos}
for r in rows:
noticias_por_evento.setdefault(r["evento_id"], []).append(r)
# Nombre del país (todos los eventos en esta vista son del mismo país filtrado)
pais_nombre = eventos[0]["pais_nombre"]
else:
# Si no hay eventos, al menos sacamos el nombre del país desde la lista
for p in paises:
if p["id"] == int(pais_id):
pais_nombre = p["nombre"]
break
total_pages = (total_eventos // per_page) + (1 if total_eventos % per_page else 0)
return render_template(
"eventos_pais.html",
paises=paises,
eventos=eventos,
noticias_por_evento=noticias_por_evento,
pais_id=int(pais_id) if pais_id else None,
pais_nombre=pais_nombre,
total_eventos=total_eventos,
total_pages=total_pages,
page=page,
lang=lang,
)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8001, debug=True)