Update app.py

This commit is contained in:
jasonMPM
2026-03-04 22:13:39 -06:00
committed by GitHub
parent 2cb050334a
commit dd0bd79c36

46
app.py
View File

@@ -62,23 +62,35 @@ 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:
full_name = (u.get("full_name") or "").strip() # Prefer the same ID used in webhooks (identity ID)
if not full_name: actor_id = (
full_name = f"{u.get('first_name','')} {u.get('last_name','')}".strip() u.get("id") or
db.execute( u.get("identity_id") or
""" u.get("user_id")
INSERT INTO user_cache (actor_id, full_name, updated_at) )
VALUES (?, ?, ?)
ON CONFLICT(actor_id) DO UPDATE SET if not actor_id:
full_name = excluded.full_name, continue # skip malformed entries
updated_at = excluded.updated_at
""", full_name = (u.get("full_name") or "").strip()
( if not full_name:
u["id"], full_name = f"{u.get('first_name','')} {u.get('last_name','')}".strip()
full_name or f"User {u['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: