This commit is contained in:
2026-06-01 16:49:38 +02:00
parent bed30c985b
commit d017203644
595 changed files with 233 additions and 13 deletions
+78 -2
View File
@@ -320,6 +320,77 @@ def insert_visits(cursor, import_id, study, subject, visits):
))
# ── notifications ─────────────────────────────────────────────────────────────
def find_notification_json_files(study):
"""Najde všechny .json soubory notifikací pro danou studii."""
out_dir = os.path.join(DETAILS_DIR, study)
return sorted(glob.glob(os.path.join(out_dir, "*.json")))
def import_notifications(conn, study):
import json as json_lib
json_files = find_notification_json_files(study)
if not json_files:
print(f" Žádné notifikace k importu pro {study}")
return 0
sql = """
INSERT INTO iwrs_notifications
(study, subject, pk, title, label, event, actual_date, text, pdf, source_file)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
ON DUPLICATE KEY UPDATE
label = VALUES(label),
text = VALUES(text),
pdf = VALUES(pdf),
source_file = VALUES(source_file)
"""
done_dir = os.path.join(os.path.join(DETAILS_DIR, study), "Zpracováno")
os.makedirs(done_dir, exist_ok=True)
cursor = conn.cursor()
count = 0
for json_path in json_files:
try:
with open(json_path, "r", encoding="utf-8") as f:
meta = json_lib.load(f)
pdf_path = json_path.replace(".json", ".pdf")
pdf_data = None
if os.path.exists(pdf_path):
with open(pdf_path, "rb") as f:
pdf_data = f.read()
cursor.execute(sql, (
meta.get("study", study),
meta.get("subject"),
meta.get("pk"),
meta.get("title"),
meta.get("label"),
meta.get("event"),
to_date(meta.get("actual_date")),
meta.get("text"),
pdf_data,
os.path.basename(json_path),
))
count += 1
# Přesun do Zpracováno
import shutil
shutil.move(json_path, os.path.join(done_dir, os.path.basename(json_path)))
if os.path.exists(pdf_path):
shutil.move(pdf_path, os.path.join(done_dir, os.path.basename(pdf_path)))
except Exception as e:
print(f" CHYBA při importu {os.path.basename(json_path)}: {e}")
conn.commit()
cursor.close()
print(f" Notifikací uloženo/přesunuto: {count}")
return count
# ── main ──────────────────────────────────────────────────────────────────────
def import_study(conn, study):
@@ -366,9 +437,14 @@ def main():
print(f"[{study}]")
try:
import_id = import_study(conn, study)
print(f" OK — import_id {import_id}\n")
print(f" OK — import_id {import_id}")
except Exception as e:
print(f" CHYBA: {e}\n")
print(f" CHYBA: {e}")
try:
import_notifications(conn, study)
except Exception as e:
print(f" CHYBA notifikace: {e}")
print()
conn.close()
print("Hotovo.")