j49_c commited on
Commit
6e37f69
·
1 Parent(s): 48928d8

Fix AI Chat AttributeError and improve user session handling

Browse files
Files changed (1) hide show
  1. app.py +10 -1
app.py CHANGED
@@ -1291,9 +1291,18 @@ def api_ai_chat():
1291
  db = load_users()
1292
  u = find_user(db, username)
1293
 
 
 
 
1294
  # AI LIMITS
1295
  now_date = time.strftime("%Y-%m-%d")
1296
- ai_status = u.setdefault("ai_usage", {"date": "", "count": 0})
 
 
 
 
 
 
1297
  if ai_status["date"] != now_date:
1298
  ai_status["date"] = now_date
1299
  ai_status["count"] = 0
 
1291
  db = load_users()
1292
  u = find_user(db, username)
1293
 
1294
+ if not u and not is_admin_session():
1295
+ return jsonify({"success": False, "message": "User not found"}), 404
1296
+
1297
  # AI LIMITS
1298
  now_date = time.strftime("%Y-%m-%d")
1299
+
1300
+ if u:
1301
+ ai_status = u.setdefault("ai_usage", {"date": "", "count": 0})
1302
+ else:
1303
+ # Admin or special user not in DB, create temporary status
1304
+ ai_status = {"date": now_date, "count": 0}
1305
+
1306
  if ai_status["date"] != now_date:
1307
  ai_status["date"] = now_date
1308
  ai_status["count"] = 0