Spaces:
Sleeping
Sleeping
refactor: move browser-bridge gates + signer helpers (require_desktop, require_local_network, fetch_signers_for_po, signer_from_lookup) into gates.py
Browse files- gates.py +184 -0
- streamlit_app.py +5 -168
gates.py
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
"""
|
| 3 |
+
gates — browser-bridge glue + the app's hard gates.
|
| 4 |
+
|
| 5 |
+
- require_desktop() : HARD-block phones/tablets (server-side st.stop()).
|
| 6 |
+
- require_local_network() : HARD gate — the one-time Local Network Access "Allow".
|
| 7 |
+
- fetch_signers_for_po(po) : signer/TOR fetch that runs in the USER'S browser.
|
| 8 |
+
- signer_from_lookup(role) : fast path — signer name from the loaded PO row.
|
| 9 |
+
|
| 10 |
+
Everything here shares one property: it needs the user's BROWSER (via
|
| 11 |
+
streamlit_js_eval), because the HF server cannot reach the PTT-internal
|
| 12 |
+
gateway — only the user's browser on the PTT network can.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
import os
|
| 16 |
+
import json
|
| 17 |
+
|
| 18 |
+
import streamlit as st
|
| 19 |
+
import streamlit.components.v1 as components
|
| 20 |
+
|
| 21 |
+
import signer_lookup
|
| 22 |
+
|
| 23 |
+
# Browser-side JS bridge: runs the signer fetch in the USER'S browser, which — unlike the HF
|
| 24 |
+
# server — can reach the PTT-internal gateway after the user grants the one-time Local Network
|
| 25 |
+
# Access "Allow" (Chrome/Edge 142+). Optional import so the app still runs (manual entry) if absent.
|
| 26 |
+
try:
|
| 27 |
+
from streamlit_js_eval import streamlit_js_eval
|
| 28 |
+
except Exception: # noqa: BLE001
|
| 29 |
+
streamlit_js_eval = None
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# =============================================================================
|
| 33 |
+
# ผู้ลงนาม: resolve a signer ROLE → the person's name.
|
| 34 |
+
# =============================================================================
|
| 35 |
+
# Two sources, in order: (1) the PO row from DATA_URL if it already carries
|
| 36 |
+
# approverName/chairmanName (fast, no fetch); else (2) a LIVE fetch in the USER'S browser to
|
| 37 |
+
# the SMP gateway — the HF server can't reach the private gateway, but the user's browser can
|
| 38 |
+
# after a one-time Local Network Access "Allow" (Chrome/Edge 142+; verified reachable 2026-06-30).
|
| 39 |
+
# Manual entry is always the fallback. See signer_lookup.build_signers_js for the browser chain.
|
| 40 |
+
SIGNER_COLUMN = signer_lookup.SIGNER_COLUMN
|
| 41 |
+
# no-reply read-only bearer (Space secret) — injected into the browser-side fetch.
|
| 42 |
+
SMP_TOKEN = os.environ.get("SMP_TOKEN", "").strip()
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def signer_from_lookup(role):
|
| 46 |
+
"""Fast path: signer name from the loaded PO row (lookup_data), or '' if the column is absent."""
|
| 47 |
+
if not role:
|
| 48 |
+
return ""
|
| 49 |
+
data = st.session_state.get("lookup_data") or {}
|
| 50 |
+
col = SIGNER_COLUMN.get(signer_lookup.role_kind(role), "")
|
| 51 |
+
return str(data.get(col) or "").strip()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def fetch_signers_for_po(po):
|
| 55 |
+
"""Resolve {approver, chairman, ...} via a fetch in the user's browser (streamlit_js_eval).
|
| 56 |
+
Returns a dict on success, {"error": ...} on failure, or None while still resolving
|
| 57 |
+
(incl. before the user clicks the one-time Local Network Access 'Allow')."""
|
| 58 |
+
po = (po or "").strip()
|
| 59 |
+
if not po:
|
| 60 |
+
return {"error": "no po"}
|
| 61 |
+
cache = st.session_state.setdefault("_signers_cache", {})
|
| 62 |
+
if po in cache:
|
| 63 |
+
return cache[po]
|
| 64 |
+
if not streamlit_js_eval:
|
| 65 |
+
return {"error": "browser bridge unavailable"}
|
| 66 |
+
if not SMP_TOKEN:
|
| 67 |
+
return {"error": "no SMP_TOKEN configured"}
|
| 68 |
+
js = signer_lookup.build_signers_js(po, SMP_TOKEN)
|
| 69 |
+
res = streamlit_js_eval(js_expressions=js, key=f"sig_{po}", want_output=True)
|
| 70 |
+
if isinstance(res, dict):
|
| 71 |
+
cache[po] = res
|
| 72 |
+
return res
|
| 73 |
+
if isinstance(res, str) and res.strip().startswith("{"):
|
| 74 |
+
try:
|
| 75 |
+
d = json.loads(res)
|
| 76 |
+
cache[po] = d
|
| 77 |
+
return d
|
| 78 |
+
except Exception: # noqa: BLE001
|
| 79 |
+
return {"error": "bad response"}
|
| 80 |
+
return None # still resolving
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def require_local_network():
|
| 84 |
+
"""Background reachability check: silently probe the SMP gateway on load — which auto-triggers
|
| 85 |
+
the one-time Local Network Access 'Allow' prompt. If granted → seamless, no UI. ONLY if the
|
| 86 |
+
user did NOT allow (blocked / off-network) do we surface a notification with recovery + retry.
|
| 87 |
+
App stays gated until allowed (no manual bypass, per product decision)."""
|
| 88 |
+
if st.session_state.get("lna_ok"):
|
| 89 |
+
return
|
| 90 |
+
if not streamlit_js_eval or not SMP_TOKEN:
|
| 91 |
+
st.error(":material/error: ระบบยังไม่พร้อมใช้งาน (ผู้ดูแลระบบยังไม่ได้ตั้งค่า SMP_TOKEN) — โปรดแจ้งผู้ดูแล")
|
| 92 |
+
st.stop()
|
| 93 |
+
|
| 94 |
+
# auto-fire the probe in the background (fresh key per attempt so 'ลองใหม่' re-fetches)
|
| 95 |
+
attempt = st.session_state.get("lna_attempt", 0)
|
| 96 |
+
res = streamlit_js_eval(js_expressions=signer_lookup.build_probe_js(SMP_TOKEN),
|
| 97 |
+
key=f"lna_probe_{attempt}", want_output=True)
|
| 98 |
+
ok = None
|
| 99 |
+
if isinstance(res, str) and res.strip().startswith("{"):
|
| 100 |
+
try:
|
| 101 |
+
ok = bool(json.loads(res).get("ok"))
|
| 102 |
+
except Exception: # noqa: BLE001
|
| 103 |
+
ok = False
|
| 104 |
+
|
| 105 |
+
if ok:
|
| 106 |
+
st.session_state["lna_ok"] = True
|
| 107 |
+
return # granted → seamless, no UI shown
|
| 108 |
+
|
| 109 |
+
if ok is False: # user did not allow / blocked / off-network
|
| 110 |
+
_assets = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets")
|
| 111 |
+
_, mid, _ = st.columns([1, 2, 1])
|
| 112 |
+
with mid:
|
| 113 |
+
st.warning(":material/lan: **ต้อง “อนุญาต” ให้แอปเชื่อมต่อ เพื่อดึงข้อมูลให้อัตโนมัติ**")
|
| 114 |
+
st.markdown("**วิธีที่ 1 — ถ้ามีกล่องเด้งขึ้น: กดปุ่ม “Allow / อนุญาต”**")
|
| 115 |
+
st.image(os.path.join(_assets, "lna_allow.png"), width=340)
|
| 116 |
+
st.markdown("**วิธีที่ 2 — ถ้าไม่เห็นกล่อง หรือเผลอกด “Block”: ทำตาม 3 ขั้นตอนนี้**")
|
| 117 |
+
st.image(os.path.join(_assets, "lna_unblock.png"), width=430)
|
| 118 |
+
if st.button(":material/refresh: โหลดหน้าใหม่", type="primary",
|
| 119 |
+
use_container_width=True):
|
| 120 |
+
components.html("<script>window.parent.location.reload()</script>", height=0)
|
| 121 |
+
st.caption("ℹ️ ต้องเชื่อมต่อเครือข่าย PTT หรือ VPN ด้วย")
|
| 122 |
+
st.stop()
|
| 123 |
+
|
| 124 |
+
# pending (the Allow prompt is open) → wait quietly
|
| 125 |
+
_, mid, _ = st.columns([1, 2, 1])
|
| 126 |
+
with mid:
|
| 127 |
+
st.info(":material/hourglass_top: กำลังเชื่อมต่อระบบ…")
|
| 128 |
+
st.stop()
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
# =============================================================================
|
| 132 |
+
# Desktop-only gate — the wide two-column form is not usable on a phone.
|
| 133 |
+
# =============================================================================
|
| 134 |
+
# Detect a real mobile device in the browser: mobile/tablet user-agent, OR a
|
| 135 |
+
# touch-only device (coarse pointer + no hover) on a narrow screen.
|
| 136 |
+
_MOBILE_DETECT_JS = r"""
|
| 137 |
+
(() => {
|
| 138 |
+
try {
|
| 139 |
+
const ua = navigator.userAgent || "";
|
| 140 |
+
const uaMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|Tablet|Silk/i.test(ua);
|
| 141 |
+
const mm = (q) => (window.matchMedia && window.matchMedia(q).matches) || false;
|
| 142 |
+
const coarse = mm('(pointer: coarse)'), noHover = mm('(hover: none)');
|
| 143 |
+
const narrow = Math.min((window.screen && window.screen.width) || 9999, window.innerWidth || 9999) <= 900;
|
| 144 |
+
return JSON.stringify({mobile: !!(uaMobile || (coarse && noHover && narrow))});
|
| 145 |
+
} catch (e) { return JSON.stringify({mobile: false}); }
|
| 146 |
+
})()
|
| 147 |
+
"""
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def _mobile_notice():
|
| 151 |
+
"""Always-visible fallback notice (the CSS .mobile-block overlay normally covers this)."""
|
| 152 |
+
st.markdown(
|
| 153 |
+
"<div style='max-width:360px;margin:16vh auto 0;text-align:center;background:#fff;"
|
| 154 |
+
"border:1px solid rgba(214,128,72,0.18);border-radius:24px;padding:2.2rem 1.7rem;"
|
| 155 |
+
"box-shadow:0 24px 60px rgba(214,120,60,0.22)'>"
|
| 156 |
+
"<div style='font-size:2.2rem;margin-bottom:0.7rem'>🖥️</div>"
|
| 157 |
+
"<div style='color:#3E2A1E;font-weight:800;font-size:1.3rem;margin-bottom:0.5rem'>"
|
| 158 |
+
"กรุณาใช้งานบนคอมพิวเตอร์</div>"
|
| 159 |
+
"<div style='color:#6E5848;font-weight:600;font-size:1rem;line-height:1.55'>"
|
| 160 |
+
"ปัจจุบัน ยังไม่รองรับการใช้งานในมือถือ<br>ขออภัยในความไม่สะดวก</div></div>",
|
| 161 |
+
unsafe_allow_html=True,
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def require_desktop():
|
| 166 |
+
"""HARD-block phones/tablets so a mobile user cannot progress (not even to login).
|
| 167 |
+
The CSS .mobile-block overlay covers narrow viewports instantly (no flash); this adds a
|
| 168 |
+
server-side st.stop() once the browser reports a mobile device. Detection runs once."""
|
| 169 |
+
if st.session_state.get("is_mobile") is True:
|
| 170 |
+
_mobile_notice()
|
| 171 |
+
st.stop()
|
| 172 |
+
if st.session_state.get("is_mobile") is False or not streamlit_js_eval:
|
| 173 |
+
return # known-desktop, or can't detect (CSS still covers)
|
| 174 |
+
res = streamlit_js_eval(js_expressions=_MOBILE_DETECT_JS, key="mobile_detect", want_output=True)
|
| 175 |
+
if isinstance(res, str) and res.strip().startswith("{"):
|
| 176 |
+
try:
|
| 177 |
+
mob = bool(json.loads(res).get("mobile"))
|
| 178 |
+
except Exception: # noqa: BLE001
|
| 179 |
+
mob = False
|
| 180 |
+
st.session_state["is_mobile"] = mob
|
| 181 |
+
if mob:
|
| 182 |
+
_mobile_notice()
|
| 183 |
+
st.stop()
|
| 184 |
+
# res is None (detection still resolving) → CSS overlay keeps mobile users covered meanwhile
|
streamlit_app.py
CHANGED
|
@@ -10,7 +10,6 @@
|
|
| 10 |
|
| 11 |
import os
|
| 12 |
import sys
|
| 13 |
-
import json
|
| 14 |
from io import BytesIO
|
| 15 |
from datetime import datetime
|
| 16 |
|
|
@@ -31,171 +30,9 @@ from auth_gate import require_login, render_topbar
|
|
| 31 |
import signer_lookup
|
| 32 |
import ai_clean
|
| 33 |
import data_source
|
|
|
|
| 34 |
import theme
|
| 35 |
|
| 36 |
-
# Browser-side JS bridge: runs the signer fetch in the USER'S browser, which — unlike the HF
|
| 37 |
-
# server — can reach the PTT-internal gateway after the user grants the one-time Local Network
|
| 38 |
-
# Access "Allow" (Chrome/Edge 142+). Optional import so the app still runs (manual entry) if absent.
|
| 39 |
-
try:
|
| 40 |
-
from streamlit_js_eval import streamlit_js_eval
|
| 41 |
-
except Exception: # noqa: BLE001
|
| 42 |
-
streamlit_js_eval = None
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
# =============================================================================
|
| 46 |
-
# ผู้ลงนาม: resolve a signer ROLE → the person's name.
|
| 47 |
-
# =============================================================================
|
| 48 |
-
# Two sources, in order: (1) the PO row from DATA_URL if it already carries
|
| 49 |
-
# approverName/chairmanName (fast, no fetch); else (2) a LIVE fetch in the USER'S browser to
|
| 50 |
-
# the SMP gateway — the HF server can't reach the private gateway, but the user's browser can
|
| 51 |
-
# after a one-time Local Network Access "Allow" (Chrome/Edge 142+; verified reachable 2026-06-30).
|
| 52 |
-
# Manual entry is always the fallback. See signer_lookup.build_signers_js for the browser chain.
|
| 53 |
-
SIGNER_COLUMN = signer_lookup.SIGNER_COLUMN
|
| 54 |
-
# no-reply read-only bearer (Space secret) — injected into the browser-side fetch.
|
| 55 |
-
SMP_TOKEN = os.environ.get("SMP_TOKEN", "").strip()
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def signer_from_lookup(role):
|
| 59 |
-
"""Fast path: signer name from the loaded PO row (lookup_data), or '' if the column is absent."""
|
| 60 |
-
if not role:
|
| 61 |
-
return ""
|
| 62 |
-
data = st.session_state.get("lookup_data") or {}
|
| 63 |
-
col = SIGNER_COLUMN.get(signer_lookup.role_kind(role), "")
|
| 64 |
-
return str(data.get(col) or "").strip()
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def fetch_signers_for_po(po):
|
| 68 |
-
"""Resolve {approver, chairman, ...} via a fetch in the user's browser (streamlit_js_eval).
|
| 69 |
-
Returns a dict on success, {"error": ...} on failure, or None while still resolving
|
| 70 |
-
(incl. before the user clicks the one-time Local Network Access 'Allow')."""
|
| 71 |
-
po = (po or "").strip()
|
| 72 |
-
if not po:
|
| 73 |
-
return {"error": "no po"}
|
| 74 |
-
cache = st.session_state.setdefault("_signers_cache", {})
|
| 75 |
-
if po in cache:
|
| 76 |
-
return cache[po]
|
| 77 |
-
if not streamlit_js_eval:
|
| 78 |
-
return {"error": "browser bridge unavailable"}
|
| 79 |
-
if not SMP_TOKEN:
|
| 80 |
-
return {"error": "no SMP_TOKEN configured"}
|
| 81 |
-
js = signer_lookup.build_signers_js(po, SMP_TOKEN)
|
| 82 |
-
res = streamlit_js_eval(js_expressions=js, key=f"sig_{po}", want_output=True)
|
| 83 |
-
if isinstance(res, dict):
|
| 84 |
-
cache[po] = res
|
| 85 |
-
return res
|
| 86 |
-
if isinstance(res, str) and res.strip().startswith("{"):
|
| 87 |
-
try:
|
| 88 |
-
d = json.loads(res)
|
| 89 |
-
cache[po] = d
|
| 90 |
-
return d
|
| 91 |
-
except Exception: # noqa: BLE001
|
| 92 |
-
return {"error": "bad response"}
|
| 93 |
-
return None # still resolving
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
def require_local_network():
|
| 97 |
-
"""Background reachability check: silently probe the SMP gateway on load — which auto-triggers
|
| 98 |
-
the one-time Local Network Access 'Allow' prompt. If granted → seamless, no UI. ONLY if the
|
| 99 |
-
user did NOT allow (blocked / off-network) do we surface a notification with recovery + retry.
|
| 100 |
-
App stays gated until allowed (no manual bypass, per product decision)."""
|
| 101 |
-
if st.session_state.get("lna_ok"):
|
| 102 |
-
return
|
| 103 |
-
if not streamlit_js_eval or not SMP_TOKEN:
|
| 104 |
-
st.error(":material/error: ระบบยังไม่พร้อมใช้งาน (ผู้ดูแลระบบยังไม่ได้ตั้งค่า SMP_TOKEN) — โปรดแจ้งผู้ดูแล")
|
| 105 |
-
st.stop()
|
| 106 |
-
|
| 107 |
-
# auto-fire the probe in the background (fresh key per attempt so 'ลองใหม่' re-fetches)
|
| 108 |
-
attempt = st.session_state.get("lna_attempt", 0)
|
| 109 |
-
res = streamlit_js_eval(js_expressions=signer_lookup.build_probe_js(SMP_TOKEN),
|
| 110 |
-
key=f"lna_probe_{attempt}", want_output=True)
|
| 111 |
-
ok = None
|
| 112 |
-
if isinstance(res, str) and res.strip().startswith("{"):
|
| 113 |
-
try:
|
| 114 |
-
ok = bool(json.loads(res).get("ok"))
|
| 115 |
-
except Exception: # noqa: BLE001
|
| 116 |
-
ok = False
|
| 117 |
-
|
| 118 |
-
if ok:
|
| 119 |
-
st.session_state["lna_ok"] = True
|
| 120 |
-
return # granted → seamless, no UI shown
|
| 121 |
-
|
| 122 |
-
if ok is False: # user did not allow / blocked / off-network
|
| 123 |
-
_assets = os.path.join(os.path.dirname(os.path.abspath(__file__)), "assets")
|
| 124 |
-
_, mid, _ = st.columns([1, 2, 1])
|
| 125 |
-
with mid:
|
| 126 |
-
st.warning(":material/lan: **ต้อง “อนุญาต” ให้แอปเชื่อมต่อ เพื่อดึงข้อมูลให้อัตโนมัติ**")
|
| 127 |
-
st.markdown("**วิธีที่ 1 — ถ้ามีกล่องเด้งขึ้น: กดปุ่ม “Allow / อนุญาต”**")
|
| 128 |
-
st.image(os.path.join(_assets, "lna_allow.png"), width=340)
|
| 129 |
-
st.markdown("**วิธีที่ 2 — ถ้าไม่เห็นกล่อง หรือเผลอกด “Block”: ทำตาม 3 ขั้นตอนนี้**")
|
| 130 |
-
st.image(os.path.join(_assets, "lna_unblock.png"), width=430)
|
| 131 |
-
if st.button(":material/refresh: โหลดหน้าใหม่", type="primary",
|
| 132 |
-
use_container_width=True):
|
| 133 |
-
components.html("<script>window.parent.location.reload()</script>", height=0)
|
| 134 |
-
st.caption("ℹ️ ต้องเชื่อมต่อเครือข่าย PTT หรือ VPN ด้วย")
|
| 135 |
-
st.stop()
|
| 136 |
-
|
| 137 |
-
# pending (the Allow prompt is open) → wait quietly
|
| 138 |
-
_, mid, _ = st.columns([1, 2, 1])
|
| 139 |
-
with mid:
|
| 140 |
-
st.info(":material/hourglass_top: กำลังเชื่อมต่อระบบ…")
|
| 141 |
-
st.stop()
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
# =============================================================================
|
| 145 |
-
# Desktop-only gate — the wide two-column form is not usable on a phone.
|
| 146 |
-
# =============================================================================
|
| 147 |
-
# Detect a real mobile device in the browser: mobile/tablet user-agent, OR a
|
| 148 |
-
# touch-only device (coarse pointer + no hover) on a narrow screen.
|
| 149 |
-
_MOBILE_DETECT_JS = r"""
|
| 150 |
-
(() => {
|
| 151 |
-
try {
|
| 152 |
-
const ua = navigator.userAgent || "";
|
| 153 |
-
const uaMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|Tablet|Silk/i.test(ua);
|
| 154 |
-
const mm = (q) => (window.matchMedia && window.matchMedia(q).matches) || false;
|
| 155 |
-
const coarse = mm('(pointer: coarse)'), noHover = mm('(hover: none)');
|
| 156 |
-
const narrow = Math.min((window.screen && window.screen.width) || 9999, window.innerWidth || 9999) <= 900;
|
| 157 |
-
return JSON.stringify({mobile: !!(uaMobile || (coarse && noHover && narrow))});
|
| 158 |
-
} catch (e) { return JSON.stringify({mobile: false}); }
|
| 159 |
-
})()
|
| 160 |
-
"""
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
def _mobile_notice():
|
| 164 |
-
"""Always-visible fallback notice (the CSS .mobile-block overlay normally covers this)."""
|
| 165 |
-
st.markdown(
|
| 166 |
-
"<div style='max-width:360px;margin:16vh auto 0;text-align:center;background:#fff;"
|
| 167 |
-
"border:1px solid rgba(214,128,72,0.18);border-radius:24px;padding:2.2rem 1.7rem;"
|
| 168 |
-
"box-shadow:0 24px 60px rgba(214,120,60,0.22)'>"
|
| 169 |
-
"<div style='font-size:2.2rem;margin-bottom:0.7rem'>🖥️</div>"
|
| 170 |
-
"<div style='color:#3E2A1E;font-weight:800;font-size:1.3rem;margin-bottom:0.5rem'>"
|
| 171 |
-
"กรุณาใช้งานบนคอมพิวเตอร์</div>"
|
| 172 |
-
"<div style='color:#6E5848;font-weight:600;font-size:1rem;line-height:1.55'>"
|
| 173 |
-
"ปัจจุบัน ยังไม่รองรับการใช้งานในมือถือ<br>ขออภัยในความไม่สะดวก</div></div>",
|
| 174 |
-
unsafe_allow_html=True,
|
| 175 |
-
)
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
def require_desktop():
|
| 179 |
-
"""HARD-block phones/tablets so a mobile user cannot progress (not even to login).
|
| 180 |
-
The CSS .mobile-block overlay covers narrow viewports instantly (no flash); this adds a
|
| 181 |
-
server-side st.stop() once the browser reports a mobile device. Detection runs once."""
|
| 182 |
-
if st.session_state.get("is_mobile") is True:
|
| 183 |
-
_mobile_notice()
|
| 184 |
-
st.stop()
|
| 185 |
-
if st.session_state.get("is_mobile") is False or not streamlit_js_eval:
|
| 186 |
-
return # known-desktop, or can't detect (CSS still covers)
|
| 187 |
-
res = streamlit_js_eval(js_expressions=_MOBILE_DETECT_JS, key="mobile_detect", want_output=True)
|
| 188 |
-
if isinstance(res, str) and res.strip().startswith("{"):
|
| 189 |
-
try:
|
| 190 |
-
mob = bool(json.loads(res).get("mobile"))
|
| 191 |
-
except Exception: # noqa: BLE001
|
| 192 |
-
mob = False
|
| 193 |
-
st.session_state["is_mobile"] = mob
|
| 194 |
-
if mob:
|
| 195 |
-
_mobile_notice()
|
| 196 |
-
st.stop()
|
| 197 |
-
# res is None (detection still resolving) → CSS overlay keeps mobile users covered meanwhile
|
| 198 |
-
|
| 199 |
|
| 200 |
# =============================================================================
|
| 201 |
# Page Config
|
|
@@ -219,11 +56,11 @@ st.markdown(theme.THEME_CSS, unsafe_allow_html=True)
|
|
| 219 |
# Renders the themed login screen and st.stop()s when not authenticated.
|
| 220 |
# =============================================================================
|
| 221 |
# HARD gate: phones/tablets are not supported — block before anything else (even login).
|
| 222 |
-
require_desktop()
|
| 223 |
require_login()
|
| 224 |
render_topbar()
|
| 225 |
# HARD gate: must grant Local Network Access (click "Allow") before the app is usable.
|
| 226 |
-
require_local_network()
|
| 227 |
|
| 228 |
|
| 229 |
# =============================================================================
|
|
@@ -338,7 +175,7 @@ st.markdown(f"""
|
|
| 338 |
# A single browser fetch feeds both the info card (left) and the signer field (right);
|
| 339 |
# calling fetch_signers_for_po in two places would duplicate the streamlit_js_eval key.
|
| 340 |
_po_for_smp = (st.session_state.get("last_po") or "").strip()
|
| 341 |
-
resolved_signers = (fetch_signers_for_po(_po_for_smp)
|
| 342 |
if (_po_for_smp and st.session_state.lookup_data) else None)
|
| 343 |
|
| 344 |
left_col, right_col = st.columns([1.05, 1], gap="large")
|
|
@@ -562,7 +399,7 @@ with right_col:
|
|
| 562 |
if signer_role:
|
| 563 |
po_val = po_number.strip()
|
| 564 |
signers = None
|
| 565 |
-
auto = signer_from_lookup(signer_role)
|
| 566 |
if not auto and po_val:
|
| 567 |
signers = resolved_signers # single fetch hoisted above the columns
|
| 568 |
if isinstance(signers, dict) and not signers.get("error"):
|
|
|
|
| 10 |
|
| 11 |
import os
|
| 12 |
import sys
|
|
|
|
| 13 |
from io import BytesIO
|
| 14 |
from datetime import datetime
|
| 15 |
|
|
|
|
| 30 |
import signer_lookup
|
| 31 |
import ai_clean
|
| 32 |
import data_source
|
| 33 |
+
import gates
|
| 34 |
import theme
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# =============================================================================
|
| 38 |
# Page Config
|
|
|
|
| 56 |
# Renders the themed login screen and st.stop()s when not authenticated.
|
| 57 |
# =============================================================================
|
| 58 |
# HARD gate: phones/tablets are not supported — block before anything else (even login).
|
| 59 |
+
gates.require_desktop()
|
| 60 |
require_login()
|
| 61 |
render_topbar()
|
| 62 |
# HARD gate: must grant Local Network Access (click "Allow") before the app is usable.
|
| 63 |
+
gates.require_local_network()
|
| 64 |
|
| 65 |
|
| 66 |
# =============================================================================
|
|
|
|
| 175 |
# A single browser fetch feeds both the info card (left) and the signer field (right);
|
| 176 |
# calling fetch_signers_for_po in two places would duplicate the streamlit_js_eval key.
|
| 177 |
_po_for_smp = (st.session_state.get("last_po") or "").strip()
|
| 178 |
+
resolved_signers = (gates.fetch_signers_for_po(_po_for_smp)
|
| 179 |
if (_po_for_smp and st.session_state.lookup_data) else None)
|
| 180 |
|
| 181 |
left_col, right_col = st.columns([1.05, 1], gap="large")
|
|
|
|
| 399 |
if signer_role:
|
| 400 |
po_val = po_number.strip()
|
| 401 |
signers = None
|
| 402 |
+
auto = gates.signer_from_lookup(signer_role)
|
| 403 |
if not auto and po_val:
|
| 404 |
signers = resolved_signers # single fetch hoisted above the columns
|
| 405 |
if isinstance(signers, dict) and not signers.get("error"):
|