Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
from fastapi import FastAPI, Query
|
| 2 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
-
import sqlite3
|
| 4 |
-
import os
|
| 5 |
from pathlib import Path
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
|
|
@@ -12,108 +11,112 @@ DB_DIR = Path("/tmp/dbs")
|
|
| 12 |
DB_DIR.mkdir(exist_ok=True)
|
| 13 |
|
| 14 |
PROVINCES = [
|
| 15 |
-
{"key": "baghdad", "name": "بغداد",
|
| 16 |
-
{"key": "nineveh", "name": "نينوى",
|
| 17 |
-
{"key": "basra", "name": "البصرة",
|
| 18 |
-
{"key": "dhiqar", "name": "ذي قار",
|
| 19 |
-
{"key": "babylon", "name": "بابل",
|
| 20 |
-
{"key": "sulaymaniyah", "name": "السليمانية",
|
| 21 |
-
{"key": "erbil", "name": "أربيل",
|
| 22 |
-
{"key": "najaf", "name": "النجف",
|
| 23 |
-
{"key": "alanbar", "name": "الأنبار",
|
| 24 |
-
{"key": "diyala", "name": "ديالى",
|
| 25 |
-
{"key": "kirkuk", "name": "كركوك",
|
| 26 |
-
{"key": "salahaldeen", "name": "صلاح الدين",
|
| 27 |
-
{"key": "muthana", "name": "المثنى",
|
| 28 |
-
{"key": "mesan", "name": "ميسان",
|
| 29 |
-
{"key": "karbalaa", "name": "كربلاء",
|
| 30 |
-
{"key": "qadisiya", "name": "القادسية",
|
| 31 |
-
{"key": "duhok", "name": "دهوك",
|
| 32 |
-
{"key": "wasit", "name": "واسط",
|
| 33 |
-
{"key": "balad", "name": "بلد (يثرب)",
|
| 34 |
]
|
| 35 |
-
|
| 36 |
PROVINCE_MAP = {p["key"]: p for p in PROVINCES}
|
| 37 |
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def get_db_path(province_key: str):
|
| 42 |
-
prov = PROVINCE_MAP.get(province_key)
|
| 43 |
-
if not prov:
|
| 44 |
-
return None
|
| 45 |
-
local = DB_DIR / prov["file"]
|
| 46 |
-
if not local.exists():
|
| 47 |
-
token = os.environ.get("HF_TOKEN")
|
| 48 |
-
hf_hub_download(
|
| 49 |
-
repo_id=REPO_ID,
|
| 50 |
-
filename=prov["file"],
|
| 51 |
-
repo_type="dataset",
|
| 52 |
-
local_dir=str(DB_DIR),
|
| 53 |
-
token=token,
|
| 54 |
-
)
|
| 55 |
-
return str(local)
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def get_table(conn):
|
| 59 |
-
cur = conn.execute("SELECT name FROM sqlite_master WHERE type='table' LIMIT 1")
|
| 60 |
-
row = cur.fetchone()
|
| 61 |
-
return row[0] if row else None
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def normalize(text: str) -> str:
|
| 65 |
-
"""
|
| 66 |
if not text:
|
| 67 |
return ""
|
| 68 |
-
text = text.strip()
|
| 69 |
-
text = text.replace("أ",
|
| 70 |
-
text = text.replace("ة",
|
| 71 |
-
text = text.replace("ى", "ي")
|
| 72 |
return text
|
| 73 |
|
| 74 |
-
|
| 75 |
def fmt_name(raw) -> str:
|
| 76 |
-
"""
|
| 77 |
if not raw:
|
| 78 |
return ""
|
| 79 |
-
raw = str(raw).strip()
|
| 80 |
-
prefixes = ["عبد",
|
| 81 |
for p in prefixes:
|
| 82 |
if raw.startswith(p) and len(raw) > len(p):
|
| 83 |
return raw[:len(p)] + " " + raw[len(p):]
|
| 84 |
return raw
|
| 85 |
|
| 86 |
-
|
| 87 |
def fmt_birth(raw) -> str:
|
| 88 |
-
"""Format birth
|
| 89 |
if raw is None:
|
| 90 |
return ""
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
| 98 |
|
| 99 |
def row_to_dict(row) -> dict:
|
| 100 |
first = fmt_name(row["p_first"])
|
| 101 |
father = fmt_name(row["p_father"])
|
| 102 |
grand = fmt_name(row["p_grand"])
|
| 103 |
-
birth = fmt_birth(row["p_birth"])
|
| 104 |
-
fam = str(row["fam_no"] or "").strip()
|
| 105 |
return {
|
| 106 |
-
"fam_no":
|
| 107 |
"p_first": first,
|
| 108 |
"p_father": father,
|
| 109 |
"p_grand": grand,
|
| 110 |
-
"p_birth":
|
| 111 |
"full_name": f"{first} {father} {grand}".strip(),
|
| 112 |
}
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
def name_combos(words):
|
| 116 |
-
"""Split N words into all (first, father, grand) combinations."""
|
| 117 |
n = len(words)
|
| 118 |
if n < 3 or n > 7:
|
| 119 |
return []
|
|
@@ -121,16 +124,12 @@ def name_combos(words):
|
|
| 121 |
seen = set()
|
| 122 |
for i in range(1, n - 1):
|
| 123 |
for j in range(i + 1, n):
|
| 124 |
-
|
| 125 |
-
g2 = "".join(words[i:j])
|
| 126 |
-
g3 = "".join(words[j:])
|
| 127 |
-
key = (g1, g2, g3)
|
| 128 |
if key not in seen:
|
| 129 |
seen.add(key)
|
| 130 |
combos.append(key)
|
| 131 |
return combos
|
| 132 |
|
| 133 |
-
|
| 134 |
# ─── API ──────────────────────────────────────────────────────────────────────
|
| 135 |
|
| 136 |
@app.get("/api/debug")
|
|
@@ -160,8 +159,7 @@ def search(p: str = Query(...), name: str = Query(None), fam: str = Query(None))
|
|
| 160 |
if not db_path:
|
| 161 |
return JSONResponse({"error": "محافظة غير موجودة"}, 404)
|
| 162 |
|
| 163 |
-
conn =
|
| 164 |
-
conn.row_factory = sqlite3.Row
|
| 165 |
table = get_table(conn)
|
| 166 |
if not table:
|
| 167 |
conn.close()
|
|
@@ -170,15 +168,14 @@ def search(p: str = Query(...), name: str = Query(None), fam: str = Query(None))
|
|
| 170 |
results = []
|
| 171 |
seen = set()
|
| 172 |
|
| 173 |
-
# ──
|
| 174 |
if fam:
|
| 175 |
fam_clean = fam.strip()
|
| 176 |
-
# Try as-is, as integer, and zero-padded variants
|
| 177 |
candidates = {fam_clean}
|
| 178 |
try:
|
| 179 |
n = int(fam_clean)
|
| 180 |
candidates.add(str(n))
|
| 181 |
-
candidates.add(f"{n:07d}")
|
| 182 |
except:
|
| 183 |
pass
|
| 184 |
for candidate in candidates:
|
|
@@ -193,27 +190,22 @@ def search(p: str = Query(...), name: str = Query(None), fam: str = Query(None))
|
|
| 193 |
seen.add(key)
|
| 194 |
results.append(d)
|
| 195 |
|
| 196 |
-
# ──
|
| 197 |
elif name:
|
| 198 |
words = name.strip().split()
|
| 199 |
if len(words) < 3:
|
| 200 |
conn.close()
|
| 201 |
return JSONResponse({"error": "يرجى إدخال الاسم الثلاثي كاملاً"})
|
| 202 |
|
| 203 |
-
# Generate combos with both original and normalized versions
|
| 204 |
norm_words = [normalize(w) for w in words]
|
| 205 |
-
|
| 206 |
all_combos = set()
|
| 207 |
-
for combo in name_combos(words):
|
| 208 |
-
all_combos.add(combo)
|
| 209 |
-
all_combos.add(tuple(normalize(x) for x in combo))
|
| 210 |
for combo in name_combos(norm_words):
|
| 211 |
all_combos.add(combo)
|
| 212 |
|
| 213 |
for first, father, grand in all_combos:
|
| 214 |
cur = conn.execute(
|
| 215 |
f"SELECT fam_no,p_first,p_father,p_grand,p_birth FROM [{table}] "
|
| 216 |
-
f"WHERE p_first=? AND p_father=? AND p_grand=? LIMIT 300",
|
| 217 |
(first, father, grand)
|
| 218 |
)
|
| 219 |
for row in cur.fetchall():
|
|
@@ -224,10 +216,10 @@ def search(p: str = Query(...), name: str = Query(None), fam: str = Query(None))
|
|
| 224 |
results.append(d)
|
| 225 |
|
| 226 |
conn.close()
|
| 227 |
-
return JSONResponse({"results": results, "count": len(results)})
|
| 228 |
|
| 229 |
except Exception as e:
|
| 230 |
-
return JSONResponse({"error": f"خطأ: {str(e)}"}, 500)
|
| 231 |
|
| 232 |
|
| 233 |
@app.get("/api/family")
|
|
@@ -236,11 +228,8 @@ def family(p: str = Query(...), fam: str = Query(...)):
|
|
| 236 |
db_path = get_db_path(p)
|
| 237 |
if not db_path:
|
| 238 |
return JSONResponse({"error": "محافظة غير موجودة"}, 404)
|
| 239 |
-
|
| 240 |
-
conn = sqlite3.connect(db_path)
|
| 241 |
-
conn.row_factory = sqlite3.Row
|
| 242 |
table = get_table(conn)
|
| 243 |
-
|
| 244 |
fam_clean = fam.strip()
|
| 245 |
candidates = {fam_clean}
|
| 246 |
try:
|
|
@@ -249,7 +238,6 @@ def family(p: str = Query(...), fam: str = Query(...)):
|
|
| 249 |
candidates.add(f"{n:07d}")
|
| 250 |
except:
|
| 251 |
pass
|
| 252 |
-
|
| 253 |
members = []
|
| 254 |
seen = set()
|
| 255 |
for candidate in candidates:
|
|
@@ -263,10 +251,8 @@ def family(p: str = Query(...), fam: str = Query(...)):
|
|
| 263 |
if key not in seen:
|
| 264 |
seen.add(key)
|
| 265 |
members.append(d)
|
| 266 |
-
|
| 267 |
conn.close()
|
| 268 |
return JSONResponse({"members": members, "count": len(members)})
|
| 269 |
-
|
| 270 |
except Exception as e:
|
| 271 |
return JSONResponse({"error": f"خطأ: {str(e)}"}, 500)
|
| 272 |
|
|
@@ -276,294 +262,289 @@ def family(p: str = Query(...), fam: str = Query(...)):
|
|
| 276 |
INDEX_HTML = """<!DOCTYPE html>
|
| 277 |
<html lang="ar" dir="rtl">
|
| 278 |
<head>
|
| 279 |
-
<meta charset="UTF-8">
|
| 280 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 281 |
<title>نظام البحث</title>
|
| 282 |
<style>
|
| 283 |
-
:root
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
}
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
}
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
.
|
| 299 |
-
.
|
| 300 |
-
.
|
| 301 |
-
.hero { text-align: center; padding: 40px 24px 32px; }
|
| 302 |
-
.hero h1 { font-size: 28px; font-weight: 600; margin-bottom: 8px; }
|
| 303 |
-
.hero p { font-size: 15px; color: var(--text3); }
|
| 304 |
-
.grid-wrap { max-width: 760px; margin: 0 auto; padding: 0 20px 60px; }
|
| 305 |
-
.grid-label { font-size: 12px; color: var(--text3); margin-bottom: 14px; text-align: center; letter-spacing: 0.05em; }
|
| 306 |
-
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
|
| 307 |
-
.prov-card {
|
| 308 |
-
display: block; text-decoration: none;
|
| 309 |
-
background: var(--surface); border: 1px solid var(--border);
|
| 310 |
-
border-radius: var(--radius); padding: 20px 14px;
|
| 311 |
-
text-align: center; cursor: pointer; transition: all 0.18s; position: relative; overflow: hidden;
|
| 312 |
-
}
|
| 313 |
-
.prov-card:hover { border-color: var(--accent); transform: translateY(-2px); box-shadow: 0 6px 24px rgba(79,123,255,0.12); }
|
| 314 |
-
.prov-name { font-size: 15px; font-weight: 500; color: var(--text); }
|
| 315 |
-
.prov-arrow { font-size: 16px; color: var(--accent); margin-top: 8px; opacity: 0; transition: opacity 0.18s; }
|
| 316 |
-
.prov-card:hover .prov-arrow { opacity: 1; }
|
| 317 |
</style>
|
| 318 |
</head>
|
| 319 |
<body>
|
| 320 |
-
<div class="
|
| 321 |
-
|
| 322 |
-
<
|
| 323 |
-
<
|
| 324 |
-
<h1>نظام البحث في السجلات</h1>
|
| 325 |
-
<p>اختر المحافظة للبدء بالبحث</p>
|
| 326 |
-
</div>
|
| 327 |
-
<div class="grid-wrap">
|
| 328 |
-
<p class="grid-label">19 محافظة وقضاء</p>
|
| 329 |
<div class="grid">
|
| 330 |
-
<a class="
|
| 331 |
-
<a class="
|
| 332 |
-
<a class="
|
| 333 |
-
<a class="
|
| 334 |
-
<a class="
|
| 335 |
-
<a class="
|
| 336 |
-
<a class="
|
| 337 |
-
<a class="
|
| 338 |
-
<a class="
|
| 339 |
-
<a class="
|
| 340 |
-
<a class="
|
| 341 |
-
<a class="
|
| 342 |
-
<a class="
|
| 343 |
-
<a class="
|
| 344 |
-
<a class="
|
| 345 |
-
<a class="
|
| 346 |
-
<a class="
|
| 347 |
-
<a class="
|
| 348 |
-
<a class="
|
| 349 |
</div>
|
| 350 |
</div>
|
| 351 |
<script>
|
| 352 |
-
const
|
| 353 |
-
if
|
| 354 |
-
function toggleTheme()
|
| 355 |
-
const
|
| 356 |
-
const
|
| 357 |
-
document.documentElement.setAttribute('data-theme',
|
| 358 |
-
localStorage.setItem('theme',
|
| 359 |
}
|
| 360 |
</script>
|
| 361 |
-
</body>
|
| 362 |
-
</html>"""
|
| 363 |
|
| 364 |
|
| 365 |
SEARCH_HTML = """<!DOCTYPE html>
|
| 366 |
<html lang="ar" dir="rtl">
|
| 367 |
<head>
|
| 368 |
-
<meta charset="UTF-8">
|
| 369 |
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 370 |
<title>بحث — {prov_name}</title>
|
| 371 |
<style>
|
| 372 |
-
:root
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
}}
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
}}
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
.
|
| 390 |
-
.
|
| 391 |
-
.
|
| 392 |
-
.
|
| 393 |
-
.
|
| 394 |
-
.
|
| 395 |
-
.
|
| 396 |
-
.
|
| 397 |
-
.
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
.
|
| 401 |
-
.
|
| 402 |
-
.
|
| 403 |
-
.
|
| 404 |
-
.
|
| 405 |
-
.
|
| 406 |
-
.
|
| 407 |
-
.
|
| 408 |
-
.
|
| 409 |
-
.
|
| 410 |
-
.
|
| 411 |
-
.
|
| 412 |
-
.
|
| 413 |
-
.
|
| 414 |
-
.
|
| 415 |
-
.
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
.
|
| 419 |
-
.fam-
|
| 420 |
-
.
|
| 421 |
-
.
|
| 422 |
-
.
|
| 423 |
-
.
|
| 424 |
-
.fam-
|
| 425 |
-
.fam-title
|
| 426 |
-
.fam-
|
| 427 |
-
|
| 428 |
-
.member
|
| 429 |
-
.member-
|
| 430 |
-
.
|
| 431 |
-
.
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
.
|
|
|
|
|
|
|
| 435 |
</style>
|
| 436 |
</head>
|
| 437 |
<body>
|
| 438 |
<div class="topbar">
|
| 439 |
-
<a class="back
|
| 440 |
-
<span class="
|
| 441 |
-
<button class="
|
| 442 |
</div>
|
| 443 |
<div class="wrap">
|
| 444 |
-
<div class="
|
| 445 |
-
<div class="
|
| 446 |
-
<button class="
|
| 447 |
-
<button class="
|
| 448 |
</div>
|
| 449 |
-
<div class="
|
| 450 |
-
<input class="
|
| 451 |
-
<button class="
|
| 452 |
</div>
|
| 453 |
-
<p class="hint" id="hint">أدخل الاسم الثلاثي
|
| 454 |
-
<p class="
|
| 455 |
</div>
|
| 456 |
<div id="results-area"></div>
|
| 457 |
</div>
|
| 458 |
<script>
|
| 459 |
-
const PROV
|
| 460 |
-
let mode
|
| 461 |
-
const
|
| 462 |
-
if
|
| 463 |
-
function toggleTheme()
|
| 464 |
-
const
|
| 465 |
-
const
|
| 466 |
-
document.documentElement.setAttribute('data-theme',
|
| 467 |
-
localStorage.setItem('theme',
|
| 468 |
}}
|
| 469 |
-
function setMode(m)
|
| 470 |
-
mode
|
| 471 |
-
document.getElementById('
|
| 472 |
-
document.getElementById('
|
| 473 |
-
const inp
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
}}
|
| 482 |
-
inp.value = '';
|
| 483 |
-
document.getElementById('error-msg').style.display = 'none';
|
| 484 |
-
document.getElementById('results-area').innerHTML = '';
|
| 485 |
}}
|
| 486 |
-
function
|
| 487 |
-
|
| 488 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 489 |
}}
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
if (mode === 'name' && val.split(/\s+/).filter(Boolean).length < 3) {{
|
| 496 |
-
showError('يرجى إدخال الاسم الثلاثي كاملاً — على الأقل 3 أجزاء');
|
| 497 |
return;
|
| 498 |
}}
|
| 499 |
-
const
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
|
|
|
|
|
|
| 512 |
}}
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
|
|
|
|
|
|
| 516 |
return;
|
| 517 |
}}
|
| 518 |
-
let html
|
| 519 |
-
results.forEach((r,
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
<div class="
|
| 524 |
-
|
| 525 |
-
<div class="
|
| 526 |
-
|
| 527 |
-
<span class="
|
| 528 |
-
<span class="fam-tag">عائلة: ${{r.fam_no}}</span>
|
| 529 |
</div>
|
| 530 |
</div>
|
| 531 |
-
<button class="
|
| 532 |
</div>
|
| 533 |
-
<div class="
|
| 534 |
-
<div class="
|
| 535 |
-
<p style="font-size:13px;color:var(--
|
| 536 |
</div>
|
| 537 |
</div>`;
|
| 538 |
}});
|
| 539 |
-
document.getElementById('results-area').innerHTML
|
| 540 |
}}
|
| 541 |
-
|
| 542 |
-
|
| 543 |
-
const
|
| 544 |
-
|
| 545 |
-
sec.classList.
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
const
|
| 550 |
-
const
|
| 551 |
-
const
|
| 552 |
-
|
| 553 |
-
|
|
|
|
|
|
|
| 554 |
}}
|
| 555 |
}}
|
| 556 |
-
|
|
|
|
| 557 |
</script>
|
| 558 |
-
</body>
|
| 559 |
-
</html>"""
|
| 560 |
|
| 561 |
|
| 562 |
@app.get("/", response_class=HTMLResponse)
|
| 563 |
def index():
|
| 564 |
return HTMLResponse(INDEX_HTML)
|
| 565 |
|
| 566 |
-
|
| 567 |
@app.get("/search", response_class=HTMLResponse)
|
| 568 |
def search_page(p: str = Query(...)):
|
| 569 |
prov = PROVINCE_MAP.get(p)
|
|
|
|
| 1 |
from fastapi import FastAPI, Query
|
| 2 |
from fastapi.responses import HTMLResponse, JSONResponse
|
| 3 |
+
import sqlite3, os, re
|
|
|
|
| 4 |
from pathlib import Path
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
|
|
|
|
| 11 |
DB_DIR.mkdir(exist_ok=True)
|
| 12 |
|
| 13 |
PROVINCES = [
|
| 14 |
+
{"key": "baghdad", "name": "بغداد", "file": "baghdad.sqlite"},
|
| 15 |
+
{"key": "nineveh", "name": "نينوى", "file": "nineveh.sqlite"},
|
| 16 |
+
{"key": "basra", "name": "البصرة", "file": "basra.sqlite"},
|
| 17 |
+
{"key": "dhiqar", "name": "ذي قار", "file": "dhiqar.sqlite"},
|
| 18 |
+
{"key": "babylon", "name": "بابل", "file": "babylon.sqlite"},
|
| 19 |
+
{"key": "sulaymaniyah", "name": "السليمانية", "file": "sulaymaniyah.sqlite"},
|
| 20 |
+
{"key": "erbil", "name": "أربيل", "file": "erbil.sqlite"},
|
| 21 |
+
{"key": "najaf", "name": "النجف", "file": "najaf.sqlite"},
|
| 22 |
+
{"key": "alanbar", "name": "الأنبار", "file": "al-anbar.sqlite"},
|
| 23 |
+
{"key": "diyala", "name": "ديالى", "file": "diyala.sqlite"},
|
| 24 |
+
{"key": "kirkuk", "name": "كركوك", "file": "kirkuk.sqlite"},
|
| 25 |
+
{"key": "salahaldeen", "name": "صلاح الدين", "file": "salah-aldeen.sqlite"},
|
| 26 |
+
{"key": "muthana", "name": "المثنى", "file": "muthana.sqlite"},
|
| 27 |
+
{"key": "mesan", "name": "ميسان", "file": "mesan.sqlite"},
|
| 28 |
+
{"key": "karbalaa", "name": "كربلاء", "file": "karbalaa.sqlite"},
|
| 29 |
+
{"key": "qadisiya", "name": "القادسية", "file": "qadisiya.sqlite"},
|
| 30 |
+
{"key": "duhok", "name": "دهوك", "file": "duhok.sqlite"},
|
| 31 |
+
{"key": "wasit", "name": "واسط", "file": "wasit.sqlite"},
|
| 32 |
+
{"key": "balad", "name": "بلد (يثرب)", "file": "balad.sqlite"},
|
| 33 |
]
|
|
|
|
| 34 |
PROVINCE_MAP = {p["key"]: p for p in PROVINCES}
|
| 35 |
|
| 36 |
+
# ─── TEXT HELPERS ─────────────────────────────────────────────────────────────
|
| 37 |
|
| 38 |
+
DIACRITICS = re.compile(r'[\u064B-\u065F\u0610-\u061A\u06D6-\u06DC\u06DF-\u06E4\u06E7\u06E8\u06EA-\u06ED]')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
def strip_diacritics(text):
|
| 41 |
+
"""Remove Arabic diacritics (tashkeel) from text."""
|
| 42 |
+
if not text:
|
| 43 |
+
return text
|
| 44 |
+
return DIACRITICS.sub('', str(text))
|
| 45 |
|
| 46 |
def normalize(text: str) -> str:
|
| 47 |
+
"""Strip diacritics + normalize hamza + ta marbuta."""
|
| 48 |
if not text:
|
| 49 |
return ""
|
| 50 |
+
text = strip_diacritics(text).strip()
|
| 51 |
+
text = text.replace("أ","ا").replace("إ","ا").replace("آ","ا")
|
| 52 |
+
text = text.replace("ة","ه").replace("ى","ي")
|
|
|
|
| 53 |
return text
|
| 54 |
|
|
|
|
| 55 |
def fmt_name(raw) -> str:
|
| 56 |
+
"""Strip diacritics and restore spaces in compound names."""
|
| 57 |
if not raw:
|
| 58 |
return ""
|
| 59 |
+
raw = strip_diacritics(str(raw)).strip()
|
| 60 |
+
prefixes = ["عبد","ابو","أبو","زين","بدر","نور","سيف","عز","علاء","ضياء","بهاء","ذو"]
|
| 61 |
for p in prefixes:
|
| 62 |
if raw.startswith(p) and len(raw) > len(p):
|
| 63 |
return raw[:len(p)] + " " + raw[len(p):]
|
| 64 |
return raw
|
| 65 |
|
|
|
|
| 66 |
def fmt_birth(raw) -> str:
|
| 67 |
+
"""Format integer birth YYYYMM → YYYY or YYYY/MM."""
|
| 68 |
if raw is None:
|
| 69 |
return ""
|
| 70 |
+
try:
|
| 71 |
+
s = str(int(str(raw).split("+")[0].strip()))
|
| 72 |
+
if len(s) == 6:
|
| 73 |
+
year, month = s[:4], s[4:]
|
| 74 |
+
return year if month == "00" else f"{year}/{month}"
|
| 75 |
+
return s
|
| 76 |
+
except:
|
| 77 |
+
return str(raw)
|
| 78 |
|
| 79 |
def row_to_dict(row) -> dict:
|
| 80 |
first = fmt_name(row["p_first"])
|
| 81 |
father = fmt_name(row["p_father"])
|
| 82 |
grand = fmt_name(row["p_grand"])
|
|
|
|
|
|
|
| 83 |
return {
|
| 84 |
+
"fam_no": str(row["fam_no"] or "").strip(),
|
| 85 |
"p_first": first,
|
| 86 |
"p_father": father,
|
| 87 |
"p_grand": grand,
|
| 88 |
+
"p_birth": fmt_birth(row["p_birth"]),
|
| 89 |
"full_name": f"{first} {father} {grand}".strip(),
|
| 90 |
}
|
| 91 |
|
| 92 |
+
# ─── DB HELPERS ───────────────────────────────────────────────────────────────
|
| 93 |
+
|
| 94 |
+
def get_db_path(province_key: str):
|
| 95 |
+
prov = PROVINCE_MAP.get(province_key)
|
| 96 |
+
if not prov:
|
| 97 |
+
return None
|
| 98 |
+
local = DB_DIR / prov["file"]
|
| 99 |
+
if not local.exists():
|
| 100 |
+
hf_hub_download(
|
| 101 |
+
repo_id=REPO_ID, filename=prov["file"],
|
| 102 |
+
repo_type="dataset", local_dir=str(DB_DIR),
|
| 103 |
+
token=os.environ.get("HF_TOKEN"),
|
| 104 |
+
)
|
| 105 |
+
return str(local)
|
| 106 |
+
|
| 107 |
+
def open_db(db_path: str):
|
| 108 |
+
conn = sqlite3.connect(db_path)
|
| 109 |
+
conn.row_factory = sqlite3.Row
|
| 110 |
+
# Register custom SQLite function to strip diacritics
|
| 111 |
+
conn.create_function("STRIP", 1, strip_diacritics)
|
| 112 |
+
conn.create_function("NORM", 1, normalize)
|
| 113 |
+
return conn
|
| 114 |
+
|
| 115 |
+
def get_table(conn) -> str:
|
| 116 |
+
row = conn.execute("SELECT name FROM sqlite_master WHERE type='table' LIMIT 1").fetchone()
|
| 117 |
+
return row[0] if row else None
|
| 118 |
|
| 119 |
def name_combos(words):
|
|
|
|
| 120 |
n = len(words)
|
| 121 |
if n < 3 or n > 7:
|
| 122 |
return []
|
|
|
|
| 124 |
seen = set()
|
| 125 |
for i in range(1, n - 1):
|
| 126 |
for j in range(i + 1, n):
|
| 127 |
+
key = ("".join(words[:i]), "".join(words[i:j]), "".join(words[j:]))
|
|
|
|
|
|
|
|
|
|
| 128 |
if key not in seen:
|
| 129 |
seen.add(key)
|
| 130 |
combos.append(key)
|
| 131 |
return combos
|
| 132 |
|
|
|
|
| 133 |
# ─── API ──────────────────────────────────────────────────────────────────────
|
| 134 |
|
| 135 |
@app.get("/api/debug")
|
|
|
|
| 159 |
if not db_path:
|
| 160 |
return JSONResponse({"error": "محافظة غير موجودة"}, 404)
|
| 161 |
|
| 162 |
+
conn = open_db(db_path)
|
|
|
|
| 163 |
table = get_table(conn)
|
| 164 |
if not table:
|
| 165 |
conn.close()
|
|
|
|
| 168 |
results = []
|
| 169 |
seen = set()
|
| 170 |
|
| 171 |
+
# ── By family number → return all members directly ──
|
| 172 |
if fam:
|
| 173 |
fam_clean = fam.strip()
|
|
|
|
| 174 |
candidates = {fam_clean}
|
| 175 |
try:
|
| 176 |
n = int(fam_clean)
|
| 177 |
candidates.add(str(n))
|
| 178 |
+
candidates.add(f"{n:07d}")
|
| 179 |
except:
|
| 180 |
pass
|
| 181 |
for candidate in candidates:
|
|
|
|
| 190 |
seen.add(key)
|
| 191 |
results.append(d)
|
| 192 |
|
| 193 |
+
# ── By name → use NORM() SQLite function to ignore diacritics ──
|
| 194 |
elif name:
|
| 195 |
words = name.strip().split()
|
| 196 |
if len(words) < 3:
|
| 197 |
conn.close()
|
| 198 |
return JSONResponse({"error": "يرجى إدخال الاسم الثلاثي كاملاً"})
|
| 199 |
|
|
|
|
| 200 |
norm_words = [normalize(w) for w in words]
|
|
|
|
| 201 |
all_combos = set()
|
|
|
|
|
|
|
|
|
|
| 202 |
for combo in name_combos(norm_words):
|
| 203 |
all_combos.add(combo)
|
| 204 |
|
| 205 |
for first, father, grand in all_combos:
|
| 206 |
cur = conn.execute(
|
| 207 |
f"SELECT fam_no,p_first,p_father,p_grand,p_birth FROM [{table}] "
|
| 208 |
+
f"WHERE NORM(p_first)=? AND NORM(p_father)=? AND NORM(p_grand)=? LIMIT 300",
|
| 209 |
(first, father, grand)
|
| 210 |
)
|
| 211 |
for row in cur.fetchall():
|
|
|
|
| 216 |
results.append(d)
|
| 217 |
|
| 218 |
conn.close()
|
| 219 |
+
return JSONResponse({"results": results, "count": len(results), "mode": "fam" if fam else "name"})
|
| 220 |
|
| 221 |
except Exception as e:
|
| 222 |
+
return JSONResponse({"error": f"خطأ داخلي: {str(e)}"}, 500)
|
| 223 |
|
| 224 |
|
| 225 |
@app.get("/api/family")
|
|
|
|
| 228 |
db_path = get_db_path(p)
|
| 229 |
if not db_path:
|
| 230 |
return JSONResponse({"error": "محافظة غير موجودة"}, 404)
|
| 231 |
+
conn = open_db(db_path)
|
|
|
|
|
|
|
| 232 |
table = get_table(conn)
|
|
|
|
| 233 |
fam_clean = fam.strip()
|
| 234 |
candidates = {fam_clean}
|
| 235 |
try:
|
|
|
|
| 238 |
candidates.add(f"{n:07d}")
|
| 239 |
except:
|
| 240 |
pass
|
|
|
|
| 241 |
members = []
|
| 242 |
seen = set()
|
| 243 |
for candidate in candidates:
|
|
|
|
| 251 |
if key not in seen:
|
| 252 |
seen.add(key)
|
| 253 |
members.append(d)
|
|
|
|
| 254 |
conn.close()
|
| 255 |
return JSONResponse({"members": members, "count": len(members)})
|
|
|
|
| 256 |
except Exception as e:
|
| 257 |
return JSONResponse({"error": f"خطأ: {str(e)}"}, 500)
|
| 258 |
|
|
|
|
| 262 |
INDEX_HTML = """<!DOCTYPE html>
|
| 263 |
<html lang="ar" dir="rtl">
|
| 264 |
<head>
|
| 265 |
+
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
| 266 |
<title>نظام البحث</title>
|
| 267 |
<style>
|
| 268 |
+
:root{--bg:#0f1117;--surface:#181c27;--surface2:#1e2336;--border:rgba(255,255,255,0.07);--border2:rgba(255,255,255,0.13);--accent:#4f7bff;--accent-dim:rgba(79,123,255,0.12);--text:#eef0f7;--text2:#8b90a8;--text3:#555d7a;--r:12px}
|
| 269 |
+
[data-theme=light]{--bg:#f5f6fa;--surface:#fff;--surface2:#f0f1f7;--border:rgba(0,0,0,0.08);--border2:rgba(0,0,0,0.15);--accent:#3b6bff;--accent-dim:rgba(59,107,255,0.1);--text:#111827;--text2:#4b5563;--text3:#9ca3af}
|
| 270 |
+
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
|
| 271 |
+
body{background:var(--bg);color:var(--text);font-family:'Segoe UI',Tahoma,Arial,sans-serif;min-height:100vh;transition:background .2s,color .2s}
|
| 272 |
+
.topbar{display:flex;justify-content:flex-end;padding:16px 24px}
|
| 273 |
+
.theme-btn{background:var(--surface);border:1px solid var(--border2);border-radius:20px;padding:6px 14px;color:var(--text2);font-size:13px;cursor:pointer}
|
| 274 |
+
.theme-btn:hover{color:var(--text);border-color:var(--accent)}
|
| 275 |
+
.hero{text-align:center;padding:40px 24px 32px}
|
| 276 |
+
.hero h1{font-size:26px;font-weight:600;margin-bottom:8px}
|
| 277 |
+
.hero p{font-size:14px;color:var(--text3)}
|
| 278 |
+
.wrap{max-width:780px;margin:0 auto;padding:0 20px 60px}
|
| 279 |
+
.lbl{font-size:12px;color:var(--text3);margin-bottom:12px;text-align:center;letter-spacing:.05em}
|
| 280 |
+
.grid{display:grid;grid-template-columns:repeat(auto-fill,minmax(148px,1fr));gap:10px}
|
| 281 |
+
.card{display:block;text-decoration:none;background:var(--surface);border:1px solid var(--border);border-radius:var(--r);padding:20px 14px;text-align:center;transition:all .18s;position:relative;overflow:hidden}
|
| 282 |
+
.card:hover{border-color:var(--accent);transform:translateY(-2px);box-shadow:0 6px 24px rgba(79,123,255,.12)}
|
| 283 |
+
.card-name{font-size:15px;font-weight:500;color:var(--text)}
|
| 284 |
+
.card-arrow{font-size:16px;color:var(--accent);margin-top:8px;opacity:0;transition:opacity .18s}
|
| 285 |
+
.card:hover .card-arrow{opacity:1}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
</style>
|
| 287 |
</head>
|
| 288 |
<body>
|
| 289 |
+
<div class="topbar"><button class="theme-btn" onclick="toggleTheme()">🌙 داكن / فاتح</button></div>
|
| 290 |
+
<div class="hero"><h1>نظام البحث في السجلات</h1><p>اختر المحافظة للبدء بالبحث</p></div>
|
| 291 |
+
<div class="wrap">
|
| 292 |
+
<p class="lbl">19 محافظة وقضاء</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
<div class="grid">
|
| 294 |
+
<a class="card" href="/search?p=baghdad"><div class="card-name">بغداد</div><div class="card-arrow">←</div></a>
|
| 295 |
+
<a class="card" href="/search?p=nineveh"><div class="card-name">نينوى</div><div class="card-arrow">←</div></a>
|
| 296 |
+
<a class="card" href="/search?p=basra"><div class="card-name">البصرة</div><div class="card-arrow">←</div></a>
|
| 297 |
+
<a class="card" href="/search?p=dhiqar"><div class="card-name">ذي قار</div><div class="card-arrow">←</div></a>
|
| 298 |
+
<a class="card" href="/search?p=babylon"><div class="card-name">بابل</div><div class="card-arrow">←</div></a>
|
| 299 |
+
<a class="card" href="/search?p=sulaymaniyah"><div class="card-name">السليمانية</div><div class="card-arrow">←</div></a>
|
| 300 |
+
<a class="card" href="/search?p=erbil"><div class="card-name">أربيل</div><div class="card-arrow">←</div></a>
|
| 301 |
+
<a class="card" href="/search?p=najaf"><div class="card-name">النجف</div><div class="card-arrow">←</div></a>
|
| 302 |
+
<a class="card" href="/search?p=alanbar"><div class="card-name">الأنبار</div><div class="card-arrow">←</div></a>
|
| 303 |
+
<a class="card" href="/search?p=diyala"><div class="card-name">ديالى</div><div class="card-arrow">←</div></a>
|
| 304 |
+
<a class="card" href="/search?p=kirkuk"><div class="card-name">كركوك</div><div class="card-arrow">←</div></a>
|
| 305 |
+
<a class="card" href="/search?p=salahaldeen"><div class="card-name">صلاح الدين</div><div class="card-arrow">←</div></a>
|
| 306 |
+
<a class="card" href="/search?p=muthana"><div class="card-name">المثنى</div><div class="card-arrow">←</div></a>
|
| 307 |
+
<a class="card" href="/search?p=mesan"><div class="card-name">ميسان</div><div class="card-arrow">←</div></a>
|
| 308 |
+
<a class="card" href="/search?p=karbalaa"><div class="card-name">كربلاء</div><div class="card-arrow">←</div></a>
|
| 309 |
+
<a class="card" href="/search?p=qadisiya"><div class="card-name">القادسية</div><div class="card-arrow">←</div></a>
|
| 310 |
+
<a class="card" href="/search?p=duhok"><div class="card-name">دهوك</div><div class="card-arrow">←</div></a>
|
| 311 |
+
<a class="card" href="/search?p=wasit"><div class="card-name">واسط</div><div class="card-arrow">←</div></a>
|
| 312 |
+
<a class="card" href="/search?p=balad"><div class="card-name">بلد (يثرب)</div><div class="card-arrow">←</div></a>
|
| 313 |
</div>
|
| 314 |
</div>
|
| 315 |
<script>
|
| 316 |
+
const s=localStorage.getItem('theme')||'dark';
|
| 317 |
+
if(s==='light')document.documentElement.setAttribute('data-theme','light');
|
| 318 |
+
function toggleTheme(){
|
| 319 |
+
const c=document.documentElement.getAttribute('data-theme');
|
| 320 |
+
const n=c==='light'?'dark':'light';
|
| 321 |
+
document.documentElement.setAttribute('data-theme',n==='dark'?'':'light');
|
| 322 |
+
localStorage.setItem('theme',n);
|
| 323 |
}
|
| 324 |
</script>
|
| 325 |
+
</body></html>"""
|
|
|
|
| 326 |
|
| 327 |
|
| 328 |
SEARCH_HTML = """<!DOCTYPE html>
|
| 329 |
<html lang="ar" dir="rtl">
|
| 330 |
<head>
|
| 331 |
+
<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
| 332 |
<title>بحث — {prov_name}</title>
|
| 333 |
<style>
|
| 334 |
+
:root{{--bg:#0f1117;--sf:#181c27;--sf2:#1e2336;--bd:rgba(255,255,255,0.07);--bd2:rgba(255,255,255,0.13);--ac:#4f7bff;--acd:rgba(79,123,255,0.12);--acg:rgba(79,123,255,0.22);--tx:#eef0f7;--tx2:#8b90a8;--tx3:#555d7a;--gr:#2ecc8a;--grd:rgba(46,204,138,0.1);--rd:#ff5f5f;--r:12px}}
|
| 335 |
+
[data-theme=light]{{--bg:#f5f6fa;--sf:#fff;--sf2:#f0f1f7;--bd:rgba(0,0,0,0.08);--bd2:rgba(0,0,0,0.15);--ac:#3b6bff;--acd:rgba(59,107,255,0.1);--acg:rgba(59,107,255,0.18);--tx:#111827;--tx2:#4b5563;--tx3:#9ca3af;--gr:#059669;--grd:rgba(5,150,105,0.08)}}
|
| 336 |
+
*,*::before,*::after{{box-sizing:border-box;margin:0;padding:0}}
|
| 337 |
+
body{{background:var(--bg);color:var(--tx);font-family:'Segoe UI',Tahoma,Arial,sans-serif;min-height:100vh}}
|
| 338 |
+
.topbar{{display:flex;align-items:center;justify-content:space-between;padding:13px 20px;border-bottom:1px solid var(--bd);background:var(--sf);position:sticky;top:0;z-index:100}}
|
| 339 |
+
.back{{color:var(--tx2);text-decoration:none;font-size:14px;padding:6px 12px;border-radius:8px;border:1px solid transparent;transition:all .15s}}
|
| 340 |
+
.back:hover{{color:var(--tx);background:var(--sf2);border-color:var(--bd2)}}
|
| 341 |
+
.prov-title{{font-size:15px;font-weight:500}}
|
| 342 |
+
.tbtn{{background:transparent;border:1px solid var(--bd2);border-radius:20px;padding:5px 12px;color:var(--tx3);font-size:12px;cursor:pointer}}
|
| 343 |
+
.wrap{{max-width:700px;margin:0 auto;padding:24px 18px 60px}}
|
| 344 |
+
.scard{{background:var(--sf);border:1px solid var(--bd);border-radius:16px;padding:22px;margin-bottom:22px}}
|
| 345 |
+
.mtoggle{{display:flex;gap:8px;margin-bottom:16px}}
|
| 346 |
+
.mbtn{{flex:1;padding:9px;border:1px solid var(--bd2);border-radius:8px;background:transparent;color:var(--tx2);font-family:inherit;font-size:14px;cursor:pointer;transition:all .15s}}
|
| 347 |
+
.mbtn.active{{background:var(--acd);border-color:var(--ac);color:var(--ac);font-weight:500}}
|
| 348 |
+
.irow{{display:flex;gap:8px}}
|
| 349 |
+
.sinput{{flex:1;padding:11px 15px;background:var(--sf2);border:1px solid var(--bd2);border-radius:var(--r);color:var(--tx);font-family:inherit;font-size:15px;outline:none;transition:border .15s;direction:rtl}}
|
| 350 |
+
.sinput::placeholder{{color:var(--tx3)}}
|
| 351 |
+
.sinput:focus{{border-color:var(--ac);box-shadow:0 0 0 3px var(--acg)}}
|
| 352 |
+
.sbtn{{padding:11px 22px;background:var(--ac);color:#fff;border:none;border-radius:var(--r);font-family:inherit;font-size:14px;font-weight:500;cursor:pointer;white-space:nowrap;transition:opacity .15s}}
|
| 353 |
+
.sbtn:hover{{opacity:.88}}
|
| 354 |
+
.sbtn:disabled{{opacity:.4;cursor:not-allowed}}
|
| 355 |
+
.hint{{font-size:12px;color:var(--tx3);margin-top:9px}}
|
| 356 |
+
.errmsg{{font-size:13px;color:var(--rd);margin-top:9px;display:none}}
|
| 357 |
+
.rh{{display:flex;align-items:center;gap:10px;margin-bottom:12px}}
|
| 358 |
+
.rlabel{{font-size:13px;color:var(--tx2)}}
|
| 359 |
+
.rbadge{{background:var(--acd);color:var(--ac);border-radius:20px;padding:2px 10px;font-size:12px;font-weight:500}}
|
| 360 |
+
|
| 361 |
+
/* Name search result cards */
|
| 362 |
+
.rcard{{background:var(--sf);border:1px solid var(--bd);border-radius:16px;margin-bottom:10px;overflow:hidden;transition:border .15s}}
|
| 363 |
+
.rcard:hover{{border-color:var(--bd2)}}
|
| 364 |
+
.rmain{{display:flex;align-items:center;gap:13px;padding:15px 18px}}
|
| 365 |
+
.av{{width:40px;height:40px;border-radius:50%;background:var(--acd);border:1px solid rgba(79,123,255,.2);display:flex;align-items:center;justify-content:center;font-size:14px;font-weight:600;color:var(--ac);flex-shrink:0}}
|
| 366 |
+
.rinfo{{flex:1}}
|
| 367 |
+
.rname{{font-size:15px;font-weight:500;margin-bottom:4px}}
|
| 368 |
+
.rmeta{{display:flex;flex-wrap:wrap;gap:10px}}
|
| 369 |
+
.mitem{{font-size:12px;color:var(--tx2)}}
|
| 370 |
+
.ftag{{font-size:11px;padding:2px 10px;border-radius:20px;background:var(--grd);color:var(--gr);border:1px solid rgba(46,204,138,.2);font-weight:500}}
|
| 371 |
+
.expbtn{{padding:7px 13px;background:transparent;border:1px solid var(--bd2);border-radius:8px;color:var(--tx2);font-family:inherit;font-size:13px;cursor:pointer;white-space:nowrap;transition:all .15s;flex-shrink:0}}
|
| 372 |
+
.expbtn:hover,.expbtn.open{{border-color:var(--ac);color:var(--ac);background:var(--acd)}}
|
| 373 |
+
.famsec{{display:none;border-top:1px solid var(--bd);background:var(--bg);padding:14px 18px}}
|
| 374 |
+
.famsec.open{{display:block}}
|
| 375 |
+
.famhdr{{display:flex;align-items:center;justify-content:space-between;margin-bottom:11px}}
|
| 376 |
+
.famtitle{{font-size:13px;color:var(--tx3);font-weight:500}}
|
| 377 |
+
.famno{{font-size:11px;color:var(--tx3);background:var(--sf);border:1px solid var(--bd);border-radius:20px;padding:3px 10px}}
|
| 378 |
+
|
| 379 |
+
/* Family search — table style */
|
| 380 |
+
.fam-table{{width:100%;border-collapse:collapse;margin-top:4px}}
|
| 381 |
+
.fam-table th{{font-size:12px;color:var(--tx3);font-weight:500;padding:8px 10px;border-bottom:1px solid var(--bd);text-align:right}}
|
| 382 |
+
.fam-table td{{font-size:14px;color:var(--tx);padding:10px 10px;border-bottom:1px solid var(--bd)}}
|
| 383 |
+
.fam-table tr:last-child td{{border-bottom:none}}
|
| 384 |
+
.fam-table tr:hover td{{background:var(--sf2)}}
|
| 385 |
+
.fam-card{{background:var(--sf);border:1px solid var(--bd);border-radius:16px;overflow:hidden}}
|
| 386 |
+
.fam-card-hdr{{padding:14px 18px;border-bottom:1px solid var(--bd);display:flex;align-items:center;justify-content:space-between}}
|
| 387 |
+
.fam-card-title{{font-size:14px;font-weight:500}}
|
| 388 |
+
.fam-count{{font-size:12px;color:var(--tx3);background:var(--sf2);border:1px solid var(--bd);border-radius:20px;padding:2px 10px}}
|
| 389 |
+
|
| 390 |
+
.member{{display:flex;align-items:center;justify-content:space-between;padding:10px 18px;border-bottom:1px solid var(--bd)}}
|
| 391 |
+
.member:last-child{{border-bottom:none}}
|
| 392 |
+
.mname{{font-size:14px}}
|
| 393 |
+
.mbirth{{font-size:12px;color:var(--tx3)}}
|
| 394 |
+
|
| 395 |
+
.empty{{text-align:center;padding:50px 20px;color:var(--tx3)}}
|
| 396 |
+
.spin{{display:inline-block;width:20px;height:20px;border:2px solid var(--bd2);border-top-color:var(--ac);border-radius:50%;animation:sp .7s linear infinite}}
|
| 397 |
+
@keyframes sp{{to{{transform:rotate(360deg)}}}}
|
| 398 |
+
.loadwrap{{text-align:center;padding:30px;color:var(--tx3);font-size:14px;display:flex;flex-direction:column;align-items:center;gap:12px}}
|
| 399 |
</style>
|
| 400 |
</head>
|
| 401 |
<body>
|
| 402 |
<div class="topbar">
|
| 403 |
+
<a class="back" href="/">→ الرئيسية</a>
|
| 404 |
+
<span class="prov-title">{prov_name}</span>
|
| 405 |
+
<button class="tbtn" onclick="toggleTheme()">🌙</button>
|
| 406 |
</div>
|
| 407 |
<div class="wrap">
|
| 408 |
+
<div class="scard">
|
| 409 |
+
<div class="mtoggle">
|
| 410 |
+
<button class="mbtn active" id="m-name" onclick="setMode('name')">بحث بالاسم الثلاثي</button>
|
| 411 |
+
<button class="mbtn" id="m-fam" onclick="setMode('fam')">بحث برقم العائلة</button>
|
| 412 |
</div>
|
| 413 |
+
<div class="irow">
|
| 414 |
+
<input class="sinput" id="sinput" type="text" placeholder="أحمد محمد علي" autocomplete="off"/>
|
| 415 |
+
<button class="sbtn" id="sbtn" onclick="doSearch()">بحث</button>
|
| 416 |
</div>
|
| 417 |
+
<p class="hint" id="hint">أدخل الاسم الثلاثي — الاسم واسم الأب واسم الجد</p>
|
| 418 |
+
<p class="errmsg" id="errmsg"></p>
|
| 419 |
</div>
|
| 420 |
<div id="results-area"></div>
|
| 421 |
</div>
|
| 422 |
<script>
|
| 423 |
+
const PROV="{prov_key}";
|
| 424 |
+
let mode="name";
|
| 425 |
+
const sv=localStorage.getItem('theme')||'dark';
|
| 426 |
+
if(sv==='light')document.documentElement.setAttribute('data-theme','light');
|
| 427 |
+
function toggleTheme(){{
|
| 428 |
+
const c=document.documentElement.getAttribute('data-theme');
|
| 429 |
+
const n=c==='light'?'dark':'light';
|
| 430 |
+
document.documentElement.setAttribute('data-theme',n==='dark'?'':'light');
|
| 431 |
+
localStorage.setItem('theme',n);
|
| 432 |
}}
|
| 433 |
+
function setMode(m){{
|
| 434 |
+
mode=m;
|
| 435 |
+
document.getElementById('m-name').classList.toggle('active',m==='name');
|
| 436 |
+
document.getElementById('m-fam').classList.toggle('active',m==='fam');
|
| 437 |
+
const inp=document.getElementById('sinput');
|
| 438 |
+
document.getElementById('hint').textContent=m==='name'
|
| 439 |
+
?'أدخل الاسم الثلاثي — الاسم واسم الأب واسم الجد'
|
| 440 |
+
:'أدخل رقم العائلة لعرض جميع أفرادها';
|
| 441 |
+
inp.placeholder=m==='name'?'أحمد محمد علي':'34323';
|
| 442 |
+
inp.value='';
|
| 443 |
+
document.getElementById('errmsg').style.display='none';
|
| 444 |
+
document.getElementById('results-area').innerHTML='';
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
}}
|
| 446 |
+
function showErr(msg){{const e=document.getElementById('errmsg');e.textContent=msg;e.style.display='block';}}
|
| 447 |
+
function hideErr(){{document.getElementById('errmsg').style.display='none';}}
|
| 448 |
+
|
| 449 |
+
async function doSearch(){{
|
| 450 |
+
hideErr();
|
| 451 |
+
const val=document.getElementById('sinput').value.trim();
|
| 452 |
+
if(!val){{showErr('يرجى إدخال قيمة للبحث');return;}}
|
| 453 |
+
if(mode==='name'&&val.split(/\s+/).filter(Boolean).length<3){{
|
| 454 |
+
showErr('يرجى إدخال الاسم الثلاثي كاملاً — الاسم والأب والجد');return;
|
| 455 |
+
}}
|
| 456 |
+
const btn=document.getElementById('sbtn');
|
| 457 |
+
btn.disabled=true;
|
| 458 |
+
document.getElementById('results-area').innerHTML='<div class="loadwrap"><div class="spin"></div><span>جاري البحث...</span></div>';
|
| 459 |
+
const param=mode==='name'?`name=${{encodeURIComponent(val)}}`:`fam=${{encodeURIComponent(val)}}`;
|
| 460 |
+
try{{
|
| 461 |
+
const res=await fetch(`/api/search?p=${{PROV}}&${{param}}`);
|
| 462 |
+
const data=await res.json();
|
| 463 |
+
if(data.error){{showErr(data.error);document.getElementById('results-area').innerHTML='';}}
|
| 464 |
+
else if(data.mode==='fam') renderFamResults(data.results,data.count);
|
| 465 |
+
else renderNameResults(data.results,data.count);
|
| 466 |
+
}}catch(e){{showErr('حدث خطأ، حاول مجدداً');document.getElementById('results-area').innerHTML='';}}
|
| 467 |
+
finally{{btn.disabled=false;}}
|
| 468 |
}}
|
| 469 |
+
|
| 470 |
+
/* Family search → show all members directly as a table */
|
| 471 |
+
function renderFamResults(results,count){{
|
| 472 |
+
if(!results||results.length===0){{
|
| 473 |
+
document.getElementById('results-area').innerHTML='<div class="empty"><p style="font-size:32px;margin-bottom:10px">🔍</p><p>لا توجد نتائج</p></div>';
|
|
|
|
|
|
|
| 474 |
return;
|
| 475 |
}}
|
| 476 |
+
const famNo=results[0]?.fam_no||'';
|
| 477 |
+
let rows=results.map(r=>`
|
| 478 |
+
<div class="member">
|
| 479 |
+
<span class="mname">${{r.full_name}}</span>
|
| 480 |
+
<span class="mbirth">${{r.p_birth||''}}</span>
|
| 481 |
+
</div>`).join('');
|
| 482 |
+
document.getElementById('results-area').innerHTML=`
|
| 483 |
+
<div class="rh"><span class="rlabel">أفراد العائلة</span><span class="rbadge">${{count}} فرد</span></div>
|
| 484 |
+
<div class="fam-card">
|
| 485 |
+
<div class="fam-card-hdr">
|
| 486 |
+
<span class="fam-card-title">رقم العائلة: ${{famNo}}</span>
|
| 487 |
+
<span class="fam-count">${{count}} فرد</span>
|
| 488 |
+
</div>
|
| 489 |
+
${{rows}}
|
| 490 |
+
</div>`;
|
| 491 |
}}
|
| 492 |
+
|
| 493 |
+
/* Name search → show person cards with expand button */
|
| 494 |
+
function renderNameResults(results,count){{
|
| 495 |
+
if(!results||results.length===0){{
|
| 496 |
+
document.getElementById('results-area').innerHTML='<div class="empty"><p style="font-size:32px;margin-bottom:10px">🔍</p><p>لا توجد نتائج مطابقة للبحث</p></div>';
|
| 497 |
return;
|
| 498 |
}}
|
| 499 |
+
let html=`<div class="rh"><span class="rlabel">نتائج البحث</span><span class="rbadge">${{count}} نتيجة</span></div>`;
|
| 500 |
+
results.forEach((r,i)=>{{
|
| 501 |
+
html+=`<div class="rcard">
|
| 502 |
+
<div class="rmain">
|
| 503 |
+
<div class="av">${{(r.p_first||'?')[0]}}</div>
|
| 504 |
+
<div class="rinfo">
|
| 505 |
+
<div class="rname">${{r.full_name}}</div>
|
| 506 |
+
<div class="rmeta">
|
| 507 |
+
<span class="mitem">📅 ${{r.p_birth||'غير محدد'}}</span>
|
| 508 |
+
<span class="ftag">عائلة: ${{r.fam_no}}</span>
|
|
|
|
| 509 |
</div>
|
| 510 |
</div>
|
| 511 |
+
<button class="expbtn" onclick="toggleFam(this,'fs-${{i}}','${{r.fam_no}}')">عرض العائلة</button>
|
| 512 |
</div>
|
| 513 |
+
<div class="famsec" id="fs-${{i}}">
|
| 514 |
+
<div class="famhdr"><span class="famtitle">أفراد العائلة</span><span class="famno">رقم ${{r.fam_no}}</span></div>
|
| 515 |
+
<p style="font-size:13px;color:var(--tx3);padding:6px 0">جاري التحميل...</p>
|
| 516 |
</div>
|
| 517 |
</div>`;
|
| 518 |
}});
|
| 519 |
+
document.getElementById('results-area').innerHTML=html;
|
| 520 |
}}
|
| 521 |
+
|
| 522 |
+
async function toggleFam(btn,id,famNo){{
|
| 523 |
+
const sec=document.getElementById(id);
|
| 524 |
+
const open=sec.classList.contains('open');
|
| 525 |
+
if(open){{sec.classList.remove('open');btn.classList.remove('open');btn.textContent='عرض العائلة';return;}}
|
| 526 |
+
sec.classList.add('open');btn.classList.add('open');btn.textContent='إخفاء العائلة';
|
| 527 |
+
if(sec.querySelector('p')){{
|
| 528 |
+
try{{
|
| 529 |
+
const res=await fetch(`/api/family?p=${{PROV}}&fam=${{famNo}}`);
|
| 530 |
+
const data=await res.json();
|
| 531 |
+
const hdr=sec.querySelector('.famhdr').outerHTML;
|
| 532 |
+
const rows=(data.members||[]).map(m=>`
|
| 533 |
+
<div class="member"><span class="mname">${{m.full_name}}</span><span class="mbirth">${{m.p_birth||''}}</span></div>`).join('');
|
| 534 |
+
sec.innerHTML=hdr+rows;
|
| 535 |
+
}}catch(e){{sec.querySelector('p').textContent='فشل تحميل بيانات العائلة';}}
|
| 536 |
}}
|
| 537 |
}}
|
| 538 |
+
|
| 539 |
+
document.getElementById('sinput').addEventListener('keydown',e=>{{if(e.key==='Enter')doSearch();}});
|
| 540 |
</script>
|
| 541 |
+
</body></html>"""
|
|
|
|
| 542 |
|
| 543 |
|
| 544 |
@app.get("/", response_class=HTMLResponse)
|
| 545 |
def index():
|
| 546 |
return HTMLResponse(INDEX_HTML)
|
| 547 |
|
|
|
|
| 548 |
@app.get("/search", response_class=HTMLResponse)
|
| 549 |
def search_page(p: str = Query(...)):
|
| 550 |
prov = PROVINCE_MAP.get(p)
|