Update app.py
Browse files
app.py
CHANGED
|
@@ -232,46 +232,56 @@ async def generate(
|
|
| 232 |
# --------------------------------------------------
|
| 233 |
now = datetime.utcnow()
|
| 234 |
today_date = now.date()
|
| 235 |
-
doc = media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
|
| 236 |
-
daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
|
| 237 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
daily_updates = []
|
| 239 |
|
| 240 |
if not daily_entries:
|
| 241 |
-
# First-ever usage → only today
|
| 242 |
daily_updates.append({"date": today_date, "count": 1})
|
| 243 |
else:
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
last_date_in_db = max(existing_dates.keys())
|
| 246 |
|
| 247 |
-
# Fill all missing days between last_date_in_db and today-1 with 0
|
| 248 |
next_day = last_date_in_db + timedelta(days=1)
|
| 249 |
while next_day < today_date:
|
| 250 |
if next_day not in existing_dates:
|
| 251 |
daily_updates.append({"date": next_day, "count": 0})
|
| 252 |
next_day += timedelta(days=1)
|
| 253 |
|
| 254 |
-
# Add today if not already present
|
| 255 |
if today_date not in existing_dates:
|
| 256 |
daily_updates.append({"date": today_date, "count": 1})
|
| 257 |
|
| 258 |
-
# Push updates if any
|
| 259 |
if daily_updates:
|
| 260 |
media_clicks_col.update_one(
|
| 261 |
{"userId": user_oid},
|
| 262 |
{"$push": {"ai_edit_daily_count": {"$each": daily_updates}}}
|
| 263 |
)
|
| 264 |
|
| 265 |
-
# Sort
|
| 266 |
doc = media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
|
| 267 |
daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
|
| 268 |
-
daily_entries.sort(
|
|
|
|
|
|
|
|
|
|
| 269 |
if len(daily_entries) > 32:
|
| 270 |
daily_entries = daily_entries[-32:]
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
|
|
|
|
|
|
| 275 |
|
| 276 |
# --------------------------------------------------
|
| 277 |
# CATEGORY CLICK LOGIC
|
|
|
|
| 232 |
# --------------------------------------------------
|
| 233 |
now = datetime.utcnow()
|
| 234 |
today_date = now.date()
|
|
|
|
|
|
|
| 235 |
|
| 236 |
+
doc = media_clicks_col.find_one(
|
| 237 |
+
{"userId": user_oid},
|
| 238 |
+
{"ai_edit_daily_count": 1}
|
| 239 |
+
)
|
| 240 |
+
|
| 241 |
+
daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
|
| 242 |
daily_updates = []
|
| 243 |
|
| 244 |
if not daily_entries:
|
|
|
|
| 245 |
daily_updates.append({"date": today_date, "count": 1})
|
| 246 |
else:
|
| 247 |
+
# Normalize DB dates (THIS FIXES THE ERROR)
|
| 248 |
+
existing_dates = {
|
| 249 |
+
(entry["date"].date() if isinstance(entry["date"], datetime) else entry["date"]): entry["count"]
|
| 250 |
+
for entry in daily_entries
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
last_date_in_db = max(existing_dates.keys())
|
| 254 |
|
|
|
|
| 255 |
next_day = last_date_in_db + timedelta(days=1)
|
| 256 |
while next_day < today_date:
|
| 257 |
if next_day not in existing_dates:
|
| 258 |
daily_updates.append({"date": next_day, "count": 0})
|
| 259 |
next_day += timedelta(days=1)
|
| 260 |
|
|
|
|
| 261 |
if today_date not in existing_dates:
|
| 262 |
daily_updates.append({"date": today_date, "count": 1})
|
| 263 |
|
|
|
|
| 264 |
if daily_updates:
|
| 265 |
media_clicks_col.update_one(
|
| 266 |
{"userId": user_oid},
|
| 267 |
{"$push": {"ai_edit_daily_count": {"$each": daily_updates}}}
|
| 268 |
)
|
| 269 |
|
| 270 |
+
# Sort + trim
|
| 271 |
doc = media_clicks_col.find_one({"userId": user_oid}, {"ai_edit_daily_count": 1})
|
| 272 |
daily_entries = doc.get("ai_edit_daily_count", []) if doc else []
|
| 273 |
+
daily_entries.sort(
|
| 274 |
+
key=lambda x: x["date"].date() if isinstance(x["date"], datetime) else x["date"]
|
| 275 |
+
)
|
| 276 |
+
|
| 277 |
if len(daily_entries) > 32:
|
| 278 |
daily_entries = daily_entries[-32:]
|
| 279 |
+
|
| 280 |
+
media_clicks_col.update_one(
|
| 281 |
+
{"userId": user_oid},
|
| 282 |
+
{"$set": {"ai_edit_daily_count": daily_entries}}
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
|
| 286 |
# --------------------------------------------------
|
| 287 |
# CATEGORY CLICK LOGIC
|