88 lines
2.9 KiB
Python
88 lines
2.9 KiB
Python
"""queries/pec.py — pec_comunicazioni"""
|
|
|
|
from .db import query, count_q, 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 + "WHERE date(p.created_at) >= date('now', '-10 days') ORDER BY p.created_at DESC LIMIT 200"
|
|
)
|
|
|
|
|
|
def get_stats(db_path: str, date_from: str = None, date_to: str = None) -> dict:
|
|
conds, params = [], []
|
|
if date_from:
|
|
conds.append("date(created_at) >= ?")
|
|
params.append(date_from)
|
|
if date_to:
|
|
conds.append("date(created_at) <= ?")
|
|
params.append(date_to)
|
|
where = (" WHERE " + " AND ".join(conds)) if conds else ""
|
|
and_pfx = (" AND" if conds else " WHERE")
|
|
t = tuple(params)
|
|
return {
|
|
'total_pec': count_q(
|
|
db_path,
|
|
f"SELECT COUNT(*) as n FROM pec_comunicazioni{where}",
|
|
t
|
|
),
|
|
'pec_inviato': count_q(
|
|
db_path,
|
|
f"SELECT COUNT(*) as n FROM pec_comunicazioni{where}{and_pfx} stato_invio='inviata'",
|
|
t
|
|
),
|
|
'pec_errore': count_q(
|
|
db_path,
|
|
f"SELECT COUNT(*) as n FROM pec_comunicazioni{where}{and_pfx} stato_invio='errore'",
|
|
t
|
|
),
|
|
'pec_pending': count_q(
|
|
db_path,
|
|
f"SELECT COUNT(*) as n FROM pec_comunicazioni{where}{and_pfx} stato_invio='pending'",
|
|
t
|
|
),
|
|
}
|
|
|
|
|
|
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>'
|
|
)
|