iSoFe commited on
Commit
8680f1b
ยท
verified ยท
1 Parent(s): dc40ebe

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +208 -29
app.py CHANGED
@@ -951,7 +951,7 @@ function renderName(results, fuzzy){{
951
  <div class="rinfo">
952
  <div class="rname">${{r.full_name}}</div>
953
  <div class="rmeta">
954
- <span class="mitem"><i class="ti ti-calendar"></i> ${{r.p_birth||'ุบูŠุฑ ู…ุญุฏุฏ'}}</span>
955
  <span class="ftag"><i class="ti ti-id"></i> ุนุงุฆู„ุฉ ${{r.fam_no}}</span>
956
  </div>
957
  </div>
@@ -997,6 +997,175 @@ document.getElementById('sinput').addEventListener('keydown',e=>{{if(e.key==='En
997
  </body></html>"""
998
 
999
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1000
  # โ”€โ”€โ”€ ADMIN PAGE โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1001
 
1002
  ADMIN_HTML = """<!DOCTYPE html>
@@ -1114,47 +1283,47 @@ ADMIN_HTML = """<!DOCTYPE html>
1114
  .ibtn.ac:hover{background:var(--acd);color:var(--ac);border-color:var(--ac)}
1115
 
1116
  /* โ”€โ”€ SAVING BAR MODIFIED โ”€โ”€ */
1117
- .savebar{
1118
  position:fixed;bottom:0;right:0;left:0;
1119
  background:var(--sf);border-top:1.5px solid var(--bd);
1120
  padding:16px 24px;display:flex;align-items:center;justify-content:space-between;
1121
  z-index:200;box-shadow:0 -4px 20px rgba(0,0,0,0.2);
1122
  animation: slideUp 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
1123
- }
1124
- @keyframes slideUp {
1125
- from { transform: translateY(100%); }
1126
- to { transform: translateY(0); }
1127
- }
1128
- .savebar.hidden{display:none}
1129
- .savebar-msg{font-size:14px;font-weight:700;color:var(--yl);display:flex;align-items:center;gap:8px}
1130
- .savebar-prov{font-size:12px;font-weight:700;color:var(--ac2);background:var(--acd);border:1px solid var(--acg);border-radius:30px;padding:4px 12px}
1131
 
1132
  /* pulsating dot indicator for unsaved changes */
1133
- .pulse-dot {
1134
  width: 8px; height: 8px; background-color: var(--yl); border-radius: 50%;
1135
  box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.7);
1136
  animation: pulse 1.4s infinite;
1137
- }
1138
- @keyframes pulse {
1139
- 0% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.7); }
1140
- 70% { transform: scale(1); box-shadow: 0 0 0 6px rgba(245, 158, 11, 0); }
1141
- 100% { transform: scale(0.95); box-shadow: 0 0 0 0 rgba(245, 158, 11, 0); }
1142
- }
1143
 
1144
- .divider{height:1px;background:var(--bd);margin:18px 0}
1145
- .note{font-size:12px;color:var(--tx3);margin-top:10px;display:flex;align-items:center;gap:6px}
1146
- .empty{text-align:center;padding:40px;color:var(--tx3);font-size:14px}
1147
 
1148
- .toast{position:fixed;top:80px;left:50%;transform:translateX(-50%);background:var(--gr);color:#fff;padding:12px 24px;border-radius:var(--r);font-size:14px;font-weight:700;z-index:300;display:none;box-shadow:0 8px 24px rgba(0,0,0,0.2)}
1149
- .toast.err{background:var(--rd)}
1150
 
1151
- .modal-bg{display:none;position:fixed;inset:0;background:rgba(0,0,0,.6);z-index:400;align-items:center;justify-content:center;backdrop-filter:blur(4px)}
1152
- .modal-bg.on{display:flex}
1153
- .modal{background:var(--sf);border:1px solid var(--bd2);border-radius:20px;padding:28px;width:min(480px,90vw);box-shadow:0 12px 36px rgba(0,0,0,0.25)}
1154
- .modal h3{font-size:17px;font-weight:700;margin-bottom:18px;display:flex;align-items:center;gap:8px}
1155
 
1156
- .spin{display:inline-block;width:16px;height:16px;border:2px solid rgba(255,255,255,.3);border-top-color:#fff;border-radius:50%;animation:sp .7s linear infinite;vertical-align:middle;margin-left:6px}
1157
- @keyframes sp{to{transform:rotate(360deg)}}
1158
  </style>
1159
  </head>
1160
  <body>
@@ -1442,12 +1611,22 @@ ADMIN_HTML = """<!DOCTYPE html>
1442
  document.getElementById('savebar').classList.add('hidden');
1443
  }else toast(d.error||'ูุดู„ ู…ุฒุงู…ู†ุฉ ุงู„ู…ู„ู ุนู„ู‰ ุงู„ุฎุงุฏู…',true);
1444
  }catch{toast('ุญุฏุซ ุฎุทุฃ ุบูŠุฑ ู…ุชูˆู‚ุน ุฃุซู†ุงุก ุงู„ุงุชุตุงู„ ุจุงู„ุฎุงุฏู…',true);}
1445
- finally{btn.disabled=false;btn.innerHTML='<i class="ti ti-cloud-upload"></i> ู…ุฒุงู…ู†ุฉ ูˆุญูุธ ุงู„ุชุนุฏูŠู„ุงุช ุงู„ุขู†';}
1446
  }
1447
  </script>
1448
  </body></html>"""
1449
 
1450
 
 
 
 
 
 
 
 
 
 
 
1451
  @app.get("/admin", response_class=HTMLResponse)
1452
  def admin_page():
1453
  return HTMLResponse(ADMIN_HTML)
 
951
  <div class="rinfo">
952
  <div class="rname">${{r.full_name}}</div>
953
  <div class="rmeta">
954
+ <span class="mitem"><i class="ti ti-calendar"></i> ${{r.p_birth||'ุบูŠุฑ ู…ุญุญุฏุฏ'}}</span>
955
  <span class="ftag"><i class="ti ti-id"></i> ุนุงุฆู„ุฉ ${{r.fam_no}}</span>
956
  </div>
957
  </div>
 
997
  </body></html>"""
998
 
999
 
1000
+ # โ”€โ”€โ”€ ADMIN API โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1001
+
1002
+ @app.post("/api/admin/add")
1003
+ def admin_add(data: dict = Body(...)):
1004
+ try:
1005
+ p = data.get("province")
1006
+ first = data.get("p_first","").strip()
1007
+ father = data.get("p_father","").strip()
1008
+ grand = data.get("p_grand","").strip()
1009
+ birth = data.get("p_birth","").strip()
1010
+ fam_no = data.get("fam_no","").strip()
1011
+
1012
+ if not all([p, first, father, grand, fam_no]):
1013
+ return JSONResponse({"error": "ุฌู…ูŠุน ุงู„ุญู‚ูˆู„ ู…ุทู„ูˆุจุฉ"}, 400)
1014
+
1015
+ db_path = get_db_path(p)
1016
+ if not db_path:
1017
+ return JSONResponse({"error": "ู…ุญุงูุธุฉ ุบูŠุฑ ู…ูˆุฌูˆุฏุฉ"}, 404)
1018
+
1019
+ conn = sqlite3.connect(db_path)
1020
+ table = get_table(conn)
1021
+
1022
+ # Convert birth to integer format YYYYMM
1023
+ birth_int = None
1024
+ if birth:
1025
+ birth_clean = birth.replace("/","").strip()
1026
+ if len(birth_clean) == 4:
1027
+ birth_int = int(birth_clean + "00")
1028
+ elif len(birth_clean) == 6:
1029
+ birth_int = int(birth_clean)
1030
+
1031
+ conn.execute(
1032
+ f"INSERT INTO [{table}] (fam_no, p_first, p_father, p_grand, p_birth) VALUES (?,?,?,?,?)",
1033
+ (fam_no, first, father, grand, birth_int)
1034
+ )
1035
+ conn.commit()
1036
+ conn.close()
1037
+ return JSONResponse({"ok": True})
1038
+ except Exception as e:
1039
+ return JSONResponse({"error": str(e)}, 500)
1040
+
1041
+
1042
+ @app.post("/api/admin/edit")
1043
+ def admin_edit(data: dict = Body(...)):
1044
+ try:
1045
+ p = data.get("province")
1046
+ old_first = data.get("old_first","").strip()
1047
+ old_father = data.get("old_father","").strip()
1048
+ old_grand = data.get("old_grand","").strip()
1049
+ old_fam = data.get("old_fam","").strip()
1050
+ new_first = data.get("p_first","").strip()
1051
+ new_father = data.get("p_father","").strip()
1052
+ new_grand = data.get("p_grand","").strip()
1053
+ new_birth = data.get("p_birth","").strip()
1054
+ new_fam = data.get("fam_no","").strip()
1055
+
1056
+ db_path = get_db_path(p)
1057
+ if not db_path:
1058
+ return JSONResponse({"error": "ู…ุญุงูุธุฉ ุบูŠุฑ ู…ูˆุฌูˆุฏุฉ"}, 404)
1059
+
1060
+ conn = sqlite3.connect(db_path)
1061
+ table = get_table(conn)
1062
+
1063
+ birth_int = None
1064
+ if new_birth:
1065
+ b = new_birth.replace("/","").strip()
1066
+ if len(b) == 4:
1067
+ birth_int = int(b + "00")
1068
+ elif len(b) == 6:
1069
+ birth_int = int(b)
1070
+
1071
+ conn.execute(
1072
+ f"UPDATE [{table}] SET p_first=?, p_father=?, p_grand=?, p_birth=?, fam_no=? "
1073
+ f"WHERE fam_no=? AND p_first=? AND p_father=? AND p_grand=?",
1074
+ (new_first, new_father, new_grand, birth_int, new_fam,
1075
+ old_fam, old_first, old_father, old_grand)
1076
+ )
1077
+ conn.commit()
1078
+ conn.close()
1079
+ return JSONResponse({"ok": True})
1080
+ except Exception as e:
1081
+ return JSONResponse({"error": str(e)}, 500)
1082
+
1083
+
1084
+ @app.post("/api/admin/delete-person")
1085
+ def admin_delete_person(data: dict = Body(...)):
1086
+ try:
1087
+ p = data.get("province")
1088
+ first = data.get("p_first","").strip()
1089
+ father = data.get("p_father","").strip()
1090
+ grand = data.get("p_grand","").strip()
1091
+ fam_no = data.get("fam_no","").strip()
1092
+
1093
+ db_path = get_db_path(p)
1094
+ if not db_path:
1095
+ return JSONResponse({"error": "ู…ุญุงูุธุฉ ุบูŠุฑ ู…ูˆุฌูˆุฏุฉ"}, 404)
1096
+
1097
+ conn = sqlite3.connect(db_path)
1098
+ table = get_table(conn)
1099
+ conn.execute(
1100
+ f"DELETE FROM [{table}] WHERE fam_no=? AND p_first=? AND p_father=? AND p_grand=?",
1101
+ (fam_no, first, father, grand)
1102
+ )
1103
+ conn.commit()
1104
+ conn.close()
1105
+ return JSONResponse({"ok": True})
1106
+ except Exception as e:
1107
+ return JSONResponse({"error": str(e)}, 500)
1108
+
1109
+
1110
+ @app.post("/api/admin/delete-family")
1111
+ def admin_delete_family(data: dict = Body(...)):
1112
+ try:
1113
+ p = data.get("province")
1114
+ fam_no = data.get("fam_no","").strip()
1115
+
1116
+ db_path = get_db_path(p)
1117
+ if not db_path:
1118
+ return JSONResponse({"error": "ู…ุญุงูุธุฉ ุบูŠุฑ ู…ูˆุฌูˆุฏุฉ"}, 404)
1119
+
1120
+ conn = sqlite3.connect(db_path)
1121
+ table = get_table(conn)
1122
+
1123
+ candidates = {fam_no}
1124
+ try:
1125
+ n = int(fam_no)
1126
+ candidates.add(str(n))
1127
+ candidates.add(f"{n:07d}")
1128
+ except:
1129
+ pass
1130
+
1131
+ deleted = 0
1132
+ for candidate in candidates:
1133
+ cur = conn.execute(f"DELETE FROM [{table}] WHERE fam_no=?", (candidate,))
1134
+ deleted += cur.rowcount
1135
+
1136
+ conn.commit()
1137
+ conn.close()
1138
+ return JSONResponse({"ok": True, "deleted": deleted})
1139
+ except Exception as e:
1140
+ return JSONResponse({"error": str(e)}, 500)
1141
+
1142
+
1143
+ @app.post("/api/admin/save")
1144
+ def admin_save(data: dict = Body(...)):
1145
+ try:
1146
+ p = data.get("province")
1147
+ prov = PROVINCE_MAP.get(p)
1148
+ if not prov:
1149
+ return JSONResponse({"error": "ู…ุญุงูุธุฉ ุบูŠุฑ ู…ูˆุฌูˆุฏุฉ"}, 404)
1150
+
1151
+ db_path = get_db_path(p)
1152
+ token = os.environ.get("HF_TOKEN")
1153
+ if not token:
1154
+ return JSONResponse({"error": "HF_TOKEN ุบูŠุฑ ู…ูˆุฌูˆุฏ"}, 500)
1155
+
1156
+ api = HfApi()
1157
+ api.upload_file(
1158
+ path_or_fileobj=db_path,
1159
+ path_in_repo=prov["file"],
1160
+ repo_id=REPO_ID,
1161
+ repo_type="dataset",
1162
+ token=token,
1163
+ )
1164
+ return JSONResponse({"ok": True})
1165
+ except Exception as e:
1166
+ return JSONResponse({"error": str(e)}, 500)
1167
+
1168
+
1169
  # โ”€โ”€โ”€ ADMIN PAGE โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
1170
 
1171
  ADMIN_HTML = """<!DOCTYPE html>
 
1283
  .ibtn.ac:hover{background:var(--acd);color:var(--ac);border-color:var(--ac)}
1284
 
1285
  /* โ”€โ”€ SAVING BAR MODIFIED โ”€โ”€ */
1286
+ .savebar{{
1287
  position:fixed;bottom:0;right:0;left:0;
1288
  background:var(--sf);border-top:1.5px solid var(--bd);
1289
  padding:16px 24px;display:flex;align-items:center;justify-content:space-between;
1290
  z-index:200;box-shadow:0 -4px 20px rgba(0,0,0,0.2);
1291
  animation: slideUp 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
1292
+ }}
1293
+ @keyframes slideUp {{
1294
+ from {{ transform: translateY(100%); }}
1295
+ to {{ transform: translateY(0); }}
1296
+ }}
1297
+ .savebar.hidden{{display:none}}
1298
+ .savebar-msg{{font-size:14px;font-weight:700;color:var(--yl);display:flex;align-items:center;gap:8px}}
1299
+ .savebar-prov{{font-size:12px;font-weight:700;color:var(--ac2);background:var(--acd);border:1px solid var(--acg);border-radius:30px;padding:4px 12px}}
1300
 
1301
  /* pulsating dot indicator for unsaved changes */
1302
+ .pulse-dot {{
1303
  width: 8px; height: 8px; background-color: var(--yl); border-radius: 50%;
1304
  box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.7);
1305
  animation: pulse 1.4s infinite;
1306
+ }}
1307
+ @keyframes pulse {{
1308
+ 0% {{ transform: scale(0.95); box-shadow: 0 0 0 0 rgba(245, 158, 11, 0.7); }}
1309
+ 70% {{ transform: scale(1); box-shadow: 0 0 0 6px rgba(245, 158, 11, 0); }}
1310
+ 100% {{ transform: scale(0.95); box-shadow: 0 0 0 0 rgba(245, 158, 11, 0); }}
1311
+ }}
1312
 
1313
+ .divider{{height:1px;background:var(--bd);margin:18px 0}}
1314
+ .note{{font-size:12px;color:var(--tx3);margin-top:10px;display:flex;align-items:center;gap:6px}}
1315
+ .empty{{text-align:center;padding:40px;color:var(--tx3);font-size:14px}}
1316
 
1317
+ .toast{{position:fixed;top:80px;left:50%;transform:translateX(-50%);background:var(--gr);color:#fff;padding:12px 24px;border-radius:var(--r);font-size:14px;font-weight:700;z-index:300;display:none;box-shadow:0 8px 24px rgba(0,0,0,0.2)}}
1318
+ .toast.err{{background:var(--rd)}}
1319
 
1320
+ .modal-bg{{display:none;position:fixed;inset:0;background:rgba(0,0,0,.6);z-index:400;align-items:center;justify-content:center;backdrop-filter:blur(4px)}}
1321
+ .modal-bg.on{{display:flex}}
1322
+ .modal{{background:var(--sf);border:1px solid var(--bd2);border-radius:20px;padding:28px;width:min(480px,90vw);box-shadow:0 12px 36px rgba(0,0,0,0.25)}}
1323
+ .modal h3{{font-size:17px;font-weight:700;margin-bottom:18px;display:flex;align-items:center;gap:8px}}
1324
 
1325
+ .spin{{display:inline-block;width:16px;height:16px;border:2px solid rgba(255,255,255,.3);border-top-color:#fff;border-radius:50%;animation:sp .7s linear infinite;vertical-align:middle;margin-left:6px}}
1326
+ @keyframes sp{{to{{transform:rotate(360deg)}}}}
1327
  </style>
1328
  </head>
1329
  <body>
 
1611
  document.getElementById('savebar').classList.add('hidden');
1612
  }else toast(d.error||'ูุดู„ ู…ุฒุงู…ู†ุฉ ุงู„ู…ู„ู ุนู„ู‰ ุงู„ุฎุงุฏู…',true);
1613
  }catch{toast('ุญุฏุซ ุฎุทุฃ ุบูŠุฑ ู…ุชูˆู‚ุน ุฃุซู†ุงุก ุงู„ุงุชุตุงู„ ุจุงู„ุฎุงุฏู…',true);}
1614
+ finally{{btn.disabled=false;btn.innerHTML='<i class="ti ti-cloud-upload"></i> ู…ุฒุงู…ู†ุฉ ูˆุญูุธ ุงู„ุชุนุฏูŠู„ุงุช ุงู„ุขู†';}}
1615
  }
1616
  </script>
1617
  </body></html>"""
1618
 
1619
 
1620
+ @app.get("/", response_class=HTMLResponse)
1621
+ def index():
1622
+ return HTMLResponse(INDEX_HTML)
1623
+
1624
+ @app.get("/search", response_class=HTMLResponse)
1625
+ def search_page(p: str = Query(...)):
1626
+ prov = PROVINCE_MAP.get(p)
1627
+ if not prov: return HTMLResponse("<h1>ู…ุญุงูุธุฉ ุบูŠุฑ ู…ูˆุฌูˆุฏุฉ</h1>", 404)
1628
+ return HTMLResponse(SEARCH_HTML.format(prov_name=prov["name"], prov_key=p))
1629
+
1630
  @app.get("/admin", response_class=HTMLResponse)
1631
  def admin_page():
1632
  return HTMLResponse(ADMIN_HTML)