Merge pull request #8 from jasonMPM/v7
Update app.py
This commit was merged in pull request #8.
This commit is contained in:
46
app.py
46
app.py
@@ -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:
|
||||||
|
|||||||
Reference in New Issue
Block a user