Upload db.py with huggingface_hub
Browse files
db.py
CHANGED
|
@@ -107,6 +107,15 @@ def init_db():
|
|
| 107 |
if 'donor_hla' not in pat_cols:
|
| 108 |
conn.execute("ALTER TABLE patients ADD COLUMN donor_hla TEXT DEFAULT ''")
|
| 109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
# migrate users table
|
| 111 |
user_cols = [r[1] for r in conn.execute('PRAGMA table_info(users)').fetchall()]
|
| 112 |
if 'role' not in user_cols:
|
|
@@ -205,12 +214,13 @@ def save_report(patient_id, report_date, pra_class, pra_percent, overall,
|
|
| 205 |
|
| 206 |
|
| 207 |
def get_all_reports(limit=100):
|
| 208 |
-
"""取得所有報告 (含病人資訊)"""
|
| 209 |
conn = get_conn()
|
| 210 |
rows = conn.execute('''
|
| 211 |
SELECT r.*, p.patient_name, p.chart_no
|
| 212 |
FROM reports r
|
| 213 |
JOIN patients p ON p.id = r.patient_id
|
|
|
|
| 214 |
ORDER BY r.report_date DESC, p.chart_no, r.pra_class
|
| 215 |
LIMIT ?
|
| 216 |
''', (limit,)).fetchall()
|
|
@@ -226,7 +236,7 @@ def get_all_patients():
|
|
| 226 |
MAX(r.report_date) as latest_date,
|
| 227 |
GROUP_CONCAT(DISTINCT r.pra_class) as classes
|
| 228 |
FROM patients p
|
| 229 |
-
LEFT JOIN reports r ON r.patient_id = p.id
|
| 230 |
GROUP BY p.id
|
| 231 |
ORDER BY latest_date DESC
|
| 232 |
''').fetchall()
|
|
@@ -243,7 +253,8 @@ def get_patient_reports(chart_no):
|
|
| 243 |
return None, []
|
| 244 |
|
| 245 |
reports = conn.execute('''
|
| 246 |
-
SELECT * FROM reports WHERE patient_id=?
|
|
|
|
| 247 |
''', (patient['id'],)).fetchall()
|
| 248 |
|
| 249 |
result = []
|
|
@@ -275,7 +286,7 @@ def get_mfi_comparison(chart_no, pra_class=None):
|
|
| 275 |
a.antigen, a.allele, a.max_mfi, a.mean_mfi, a.no_of_beads
|
| 276 |
FROM reports r
|
| 277 |
JOIN antibody_strength a ON a.report_id = r.id
|
| 278 |
-
WHERE r.patient_id = ?
|
| 279 |
'''
|
| 280 |
params = [patient['id']]
|
| 281 |
if pra_class:
|
|
@@ -328,10 +339,11 @@ def get_mfi_comparison(chart_no, pra_class=None):
|
|
| 328 |
|
| 329 |
|
| 330 |
def delete_report(report_id):
|
|
|
|
| 331 |
backup_db()
|
| 332 |
conn = get_conn()
|
| 333 |
-
conn.execute(
|
| 334 |
-
|
| 335 |
conn.commit()
|
| 336 |
conn.close()
|
| 337 |
|
|
|
|
| 107 |
if 'donor_hla' not in pat_cols:
|
| 108 |
conn.execute("ALTER TABLE patients ADD COLUMN donor_hla TEXT DEFAULT ''")
|
| 109 |
|
| 110 |
+
# migrate: soft delete columns
|
| 111 |
+
report_cols_all = [r[1] for r in conn.execute('PRAGMA table_info(reports)').fetchall()]
|
| 112 |
+
if 'is_deleted' not in report_cols_all:
|
| 113 |
+
try:
|
| 114 |
+
conn.execute("ALTER TABLE reports ADD COLUMN is_deleted INTEGER DEFAULT 0")
|
| 115 |
+
conn.execute("ALTER TABLE reports ADD COLUMN deleted_at TEXT")
|
| 116 |
+
except Exception:
|
| 117 |
+
pass
|
| 118 |
+
|
| 119 |
# migrate users table
|
| 120 |
user_cols = [r[1] for r in conn.execute('PRAGMA table_info(users)').fetchall()]
|
| 121 |
if 'role' not in user_cols:
|
|
|
|
| 214 |
|
| 215 |
|
| 216 |
def get_all_reports(limit=100):
|
| 217 |
+
"""取得所有報告 (含病人資訊,排除已刪除)"""
|
| 218 |
conn = get_conn()
|
| 219 |
rows = conn.execute('''
|
| 220 |
SELECT r.*, p.patient_name, p.chart_no
|
| 221 |
FROM reports r
|
| 222 |
JOIN patients p ON p.id = r.patient_id
|
| 223 |
+
WHERE COALESCE(r.is_deleted, 0) = 0
|
| 224 |
ORDER BY r.report_date DESC, p.chart_no, r.pra_class
|
| 225 |
LIMIT ?
|
| 226 |
''', (limit,)).fetchall()
|
|
|
|
| 236 |
MAX(r.report_date) as latest_date,
|
| 237 |
GROUP_CONCAT(DISTINCT r.pra_class) as classes
|
| 238 |
FROM patients p
|
| 239 |
+
LEFT JOIN reports r ON r.patient_id = p.id AND COALESCE(r.is_deleted, 0) = 0
|
| 240 |
GROUP BY p.id
|
| 241 |
ORDER BY latest_date DESC
|
| 242 |
''').fetchall()
|
|
|
|
| 253 |
return None, []
|
| 254 |
|
| 255 |
reports = conn.execute('''
|
| 256 |
+
SELECT * FROM reports WHERE patient_id=? AND COALESCE(is_deleted, 0) = 0
|
| 257 |
+
ORDER BY report_date DESC, pra_class
|
| 258 |
''', (patient['id'],)).fetchall()
|
| 259 |
|
| 260 |
result = []
|
|
|
|
| 286 |
a.antigen, a.allele, a.max_mfi, a.mean_mfi, a.no_of_beads
|
| 287 |
FROM reports r
|
| 288 |
JOIN antibody_strength a ON a.report_id = r.id
|
| 289 |
+
WHERE r.patient_id = ? AND COALESCE(r.is_deleted, 0) = 0
|
| 290 |
'''
|
| 291 |
params = [patient['id']]
|
| 292 |
if pra_class:
|
|
|
|
| 339 |
|
| 340 |
|
| 341 |
def delete_report(report_id):
|
| 342 |
+
"""軟刪除報告(標記 is_deleted=1,不真的刪)"""
|
| 343 |
backup_db()
|
| 344 |
conn = get_conn()
|
| 345 |
+
conn.execute("""UPDATE reports SET is_deleted=1, deleted_at=datetime('now','localtime'),
|
| 346 |
+
updated_at=datetime('now','localtime') WHERE id=?""", (report_id,))
|
| 347 |
conn.commit()
|
| 348 |
conn.close()
|
| 349 |
|