Upload db.py with huggingface_hub
Browse files
db.py
CHANGED
|
@@ -109,6 +109,11 @@ def init_db():
|
|
| 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")
|
|
@@ -179,7 +184,8 @@ def get_donor_hla(chart_no):
|
|
| 179 |
|
| 180 |
|
| 181 |
def save_report(patient_id, report_date, pra_class, pra_percent, overall,
|
| 182 |
-
specificity, comment, sero_mfi_list, status='draft', submitted_by=''
|
|
|
|
| 183 |
backup_db()
|
| 184 |
conn = get_conn()
|
| 185 |
existing = conn.execute(
|
|
@@ -190,14 +196,14 @@ def save_report(patient_id, report_date, pra_class, pra_percent, overall,
|
|
| 190 |
if existing:
|
| 191 |
report_id = existing['id']
|
| 192 |
conn.execute('''UPDATE reports SET pra_percent=?, overall=?, specificity=?, comment=?,
|
| 193 |
-
status=?, submitted_by=?, updated_at=datetime('now','localtime') WHERE id=?''',
|
| 194 |
-
(pra_percent, overall, specificity, comment, status, submitted_by, report_id))
|
| 195 |
conn.execute('DELETE FROM antibody_strength WHERE report_id=?', (report_id,))
|
| 196 |
else:
|
| 197 |
cur = conn.execute(
|
| 198 |
'''INSERT INTO reports (patient_id, report_date, pra_class, pra_percent, overall,
|
| 199 |
-
specificity, comment, status, submitted_by) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)''',
|
| 200 |
-
(patient_id, report_date, pra_class, pra_percent, overall, specificity, comment, status, submitted_by))
|
| 201 |
report_id = cur.lastrowid
|
| 202 |
|
| 203 |
for m in sero_mfi_list:
|
|
@@ -348,6 +354,22 @@ def delete_report(report_id):
|
|
| 348 |
conn.close()
|
| 349 |
|
| 350 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
def register_user(username, password, display_name=''):
|
| 352 |
"""註冊新使用者"""
|
| 353 |
backup_db()
|
|
|
|
| 109 |
|
| 110 |
# migrate: soft delete columns
|
| 111 |
report_cols_all = [r[1] for r in conn.execute('PRAGMA table_info(reports)').fetchall()]
|
| 112 |
+
if 'upload_file' not in report_cols_all:
|
| 113 |
+
try:
|
| 114 |
+
conn.execute("ALTER TABLE reports ADD COLUMN upload_file TEXT DEFAULT ''")
|
| 115 |
+
except Exception:
|
| 116 |
+
pass
|
| 117 |
if 'is_deleted' not in report_cols_all:
|
| 118 |
try:
|
| 119 |
conn.execute("ALTER TABLE reports ADD COLUMN is_deleted INTEGER DEFAULT 0")
|
|
|
|
| 184 |
|
| 185 |
|
| 186 |
def save_report(patient_id, report_date, pra_class, pra_percent, overall,
|
| 187 |
+
specificity, comment, sero_mfi_list, status='draft', submitted_by='',
|
| 188 |
+
upload_file=''):
|
| 189 |
backup_db()
|
| 190 |
conn = get_conn()
|
| 191 |
existing = conn.execute(
|
|
|
|
| 196 |
if existing:
|
| 197 |
report_id = existing['id']
|
| 198 |
conn.execute('''UPDATE reports SET pra_percent=?, overall=?, specificity=?, comment=?,
|
| 199 |
+
status=?, submitted_by=?, upload_file=?, updated_at=datetime('now','localtime') WHERE id=?''',
|
| 200 |
+
(pra_percent, overall, specificity, comment, status, submitted_by, upload_file or '', report_id))
|
| 201 |
conn.execute('DELETE FROM antibody_strength WHERE report_id=?', (report_id,))
|
| 202 |
else:
|
| 203 |
cur = conn.execute(
|
| 204 |
'''INSERT INTO reports (patient_id, report_date, pra_class, pra_percent, overall,
|
| 205 |
+
specificity, comment, status, submitted_by, upload_file) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''',
|
| 206 |
+
(patient_id, report_date, pra_class, pra_percent, overall, specificity, comment, status, submitted_by, upload_file or ''))
|
| 207 |
report_id = cur.lastrowid
|
| 208 |
|
| 209 |
for m in sero_mfi_list:
|
|
|
|
| 354 |
conn.close()
|
| 355 |
|
| 356 |
|
| 357 |
+
UPLOAD_DIR = DB_PATH.parent / 'uploads'
|
| 358 |
+
|
| 359 |
+
|
| 360 |
+
def get_upload_dir():
|
| 361 |
+
UPLOAD_DIR.mkdir(exist_ok=True)
|
| 362 |
+
return UPLOAD_DIR
|
| 363 |
+
|
| 364 |
+
|
| 365 |
+
def get_storage_stats():
|
| 366 |
+
"""取得 uploads 資料夾使用量"""
|
| 367 |
+
d = get_upload_dir()
|
| 368 |
+
files = list(d.glob('*.xls')) + list(d.glob('*.xlsx')) + list(d.glob('*.csv'))
|
| 369 |
+
total_size = sum(f.stat().st_size for f in files)
|
| 370 |
+
return {'count': len(files), 'size_mb': round(total_size / 1024 / 1024, 2)}
|
| 371 |
+
|
| 372 |
+
|
| 373 |
def register_user(username, password, display_name=''):
|
| 374 |
"""註冊新使用者"""
|
| 375 |
backup_db()
|