59 lines
2.2 KiB
Python
59 lines
2.2 KiB
Python
"""queries/pec.py — pec_comunicazioni"""
|
|
|
|
from .db import query, count, e, dt, badge
|
|
|
|
_PEC_JOIN = """
|
|
SELECT
|
|
p.id, p.sessione_id, p.tipo_comunicazione,
|
|
p.email_ats, p.stato_invio, p.data_invio_pec, p.created_at,
|
|
p.note,
|
|
s.articolo_cod, s.corso, s.azienda,
|
|
sl.data AS data_sessione, sl.indirizzo, sl.docenti
|
|
FROM pec_comunicazioni p
|
|
JOIN sessione s ON s.id = p.sessione_id
|
|
LEFT JOIN sessione_lezione sl ON sl.sessione_id = p.sessione_id
|
|
"""
|
|
|
|
|
|
def get_pec(db_path: str) -> list:
|
|
return query(db_path, _PEC_JOIN + "ORDER BY p.created_at DESC LIMIT 200")
|
|
|
|
|
|
def get_stats(db_path: str) -> dict:
|
|
return {
|
|
'total_pec': count(db_path, "SELECT COUNT(*) as n FROM pec_comunicazioni"),
|
|
'pec_inviato': count(db_path, "SELECT COUNT(*) as n FROM pec_comunicazioni WHERE stato_invio='inviata'"),
|
|
'pec_errore': count(db_path, "SELECT COUNT(*) as n FROM pec_comunicazioni WHERE stato_invio='errore'"),
|
|
'pec_pending': count(db_path, "SELECT COUNT(*) as n FROM pec_comunicazioni WHERE stato_invio='pending'"),
|
|
}
|
|
|
|
|
|
def render_table(rows: list) -> str:
|
|
if not rows:
|
|
return '<p class="empty">Nessuna comunicazione PEC trovata.</p>'
|
|
|
|
def _badge(stato):
|
|
m = {'inviata': ('ok', 'Inviata'), 'pending': ('warn', 'Pending'), 'errore': ('err', 'Errore')}
|
|
cls, lbl = m.get(stato, ('', ''))
|
|
return badge(cls, lbl) if cls else e(stato)
|
|
|
|
trs = ''.join(
|
|
f'<tr data-stato="{e(r.get("stato_invio", ""))}">'
|
|
f'<td>{e(r.get("articolo_cod"))}</td>'
|
|
f'<td>{e(r.get("sessione_id"))}</td>'
|
|
f'<td>{dt(r.get("data_sessione"))}</td>'
|
|
f'<td>{e(r.get("tipo_comunicazione"))}</td>'
|
|
f'<td>{e(r.get("email_ats"))}</td>'
|
|
f'<td>{dt(r.get("data_invio_pec"))}</td>'
|
|
f'<td>{_badge(r.get("stato_invio"))}</td>'
|
|
f'<td class="nc">{e(r.get("note"))}</td>'
|
|
f'</tr>'
|
|
for r in rows
|
|
)
|
|
return (
|
|
'<table><thead><tr>'
|
|
'<th>Corso</th><th>Sessione</th><th>Data Sessione</th><th>Tipo</th>'
|
|
'<th>Email ATS</th><th>Data Invio</th><th>Stato</th><th>Note</th>'
|
|
f'</tr></thead><tbody>{trs}</tbody></table>'
|
|
)
|