Merge pull request #9 from jasonMPM/v7

Update app.py
This commit was merged in pull request #9.
This commit is contained in:
jasonMPM
2026-03-04 22:17:11 -06:00
committed by GitHub

64
app.py
View File

@@ -30,21 +30,25 @@ def get_db():
def init_db(): def init_db():
with get_db() as db: with get_db() as db:
db.execute(""" db.execute(
"""
CREATE TABLE IF NOT EXISTS badge_events ( CREATE TABLE IF NOT EXISTS badge_events (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
actor_id TEXT NOT NULL, actor_id TEXT NOT NULL,
ts TEXT NOT NULL, ts TEXT NOT NULL,
date TEXT NOT NULL date TEXT NOT NULL
) )
""") """
db.execute(""" )
db.execute(
"""
CREATE TABLE IF NOT EXISTS user_cache ( CREATE TABLE IF NOT EXISTS user_cache (
actor_id TEXT PRIMARY KEY, actor_id TEXT PRIMARY KEY,
full_name TEXT NOT NULL, full_name TEXT NOT NULL,
updated_at TEXT NOT NULL updated_at TEXT NOT NULL
) )
""") """
)
db.commit() db.commit()
@@ -62,35 +66,33 @@ def sync_unifi_users():
users = r.json().get("data", []) users = r.json().get("data", [])
with get_db() as db: with get_db() as db:
for u in users: for u in users:
# Prefer the same ID used in webhooks (identity ID) # Prefer the same ID used in webhooks (identity ID / id)
actor_id = ( actor_id = (
u.get("id") or u.get("id")
u.get("identity_id") or or u.get("identity_id")
u.get("user_id") or u.get("user_id")
) )
if not actor_id:
continue # skip malformed entries
if not actor_id: full_name = (u.get("full_name") or "").strip()
continue # skip malformed entries if not full_name:
full_name = f"{u.get('first_name','')} {u.get('last_name','')}".strip()
full_name = (u.get("full_name") or "").strip()
if not full_name:
full_name = f"{u.get('first_name','')} {u.get('last_name','')}".strip()
db.execute(
"""
INSERT INTO user_cache (actor_id, full_name, updated_at)
VALUES (?, ?, ?)
ON CONFLICT(actor_id) DO UPDATE SET
full_name = excluded.full_name,
updated_at = excluded.updated_at
""",
(
actor_id,
full_name or f"User {actor_id[:8]}",
datetime.utcnow().isoformat(),
),
)
db.execute(
"""
INSERT INTO user_cache (actor_id, full_name, updated_at)
VALUES (?, ?, ?)
ON CONFLICT(actor_id) DO UPDATE SET
full_name = excluded.full_name,
updated_at = excluded.updated_at
""",
(
actor_id,
full_name or f"User {actor_id[:8]}",
datetime.utcnow().isoformat(),
),
)
db.commit() db.commit()
log.info("Synced %d users from UniFi Access", len(users)) log.info("Synced %d users from UniFi Access", len(users))
except Exception as e: except Exception as e: