Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
|
@@ -303,47 +303,108 @@ async def face_swap_api(
|
|
| 303 |
target_url = asset["url"]
|
| 304 |
category_id = str(doc["categoryId"])
|
| 305 |
|
|
|
|
| 306 |
# ---------- media_clicks update only if user_id exists ----------
|
| 307 |
if user_id:
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
"
|
|
|
|
| 323 |
},
|
| 324 |
-
"$inc": {
|
| 325 |
-
"categories.$.click_count": 1
|
| 326 |
-
}
|
| 327 |
-
}
|
| 328 |
-
)
|
| 329 |
-
|
| 330 |
-
# If category did not exist, push new category object
|
| 331 |
-
if update_result.matched_count == 0:
|
| 332 |
-
await media_clicks_col.update_one(
|
| 333 |
-
{"userId": user_oid},
|
| 334 |
{
|
| 335 |
-
"$
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
"lastClickedAt": now
|
| 342 |
-
}
|
| 343 |
}
|
| 344 |
-
}
|
| 345 |
-
upsert=True
|
| 346 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 347 |
|
| 348 |
# ==========================================================
|
| 349 |
# CASE 2: EXISTING TARGET_CATEGORY_ID (DigitalOcean)
|
|
|
|
| 303 |
target_url = asset["url"]
|
| 304 |
category_id = str(doc["categoryId"])
|
| 305 |
|
| 306 |
+
# ---------- media_clicks update only if user_id exists ----------
|
| 307 |
# ---------- media_clicks update only if user_id exists ----------
|
| 308 |
if user_id:
|
| 309 |
+
try:
|
| 310 |
+
print("\n======= MEDIA_CLICKS DEBUG LOG =======")
|
| 311 |
+
print("user_id received:", user_id)
|
| 312 |
+
print("category_id resolved from subcategory:", category_id)
|
| 313 |
+
print("ADMIN_MONGO_URL:", ADMIN_MONGO_URL)
|
| 314 |
+
|
| 315 |
+
user_oid = ObjectId(user_id)
|
| 316 |
+
cat_oid = ObjectId(category_id)
|
| 317 |
+
|
| 318 |
+
now = datetime.utcnow()
|
| 319 |
+
|
| 320 |
+
# Try to update existing category entry
|
| 321 |
+
update_result = await media_clicks_col.update_one(
|
| 322 |
+
{
|
| 323 |
+
"userId": user_oid,
|
| 324 |
+
"categories.categoryId": cat_oid
|
| 325 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
{
|
| 327 |
+
"$set": {
|
| 328 |
+
"updatedAt": now,
|
| 329 |
+
"categories.$.lastClickedAt": now
|
| 330 |
+
},
|
| 331 |
+
"$inc": {
|
| 332 |
+
"categories.$.click_count": 1
|
|
|
|
|
|
|
| 333 |
}
|
| 334 |
+
}
|
|
|
|
| 335 |
)
|
| 336 |
+
|
| 337 |
+
print("Existing category update matched_count:", update_result.matched_count)
|
| 338 |
+
print("Existing category update modified_count:", update_result.modified_count)
|
| 339 |
+
|
| 340 |
+
# If category did not exist, push new category object
|
| 341 |
+
if update_result.matched_count == 0:
|
| 342 |
+
print("Category not found under this user → pushing new category entry")
|
| 343 |
+
|
| 344 |
+
insert_result = await media_clicks_col.update_one(
|
| 345 |
+
{"userId": user_oid},
|
| 346 |
+
{
|
| 347 |
+
"$setOnInsert": {"createdAt": now},
|
| 348 |
+
"$set": {"updatedAt": now},
|
| 349 |
+
"$push": {
|
| 350 |
+
"categories": {
|
| 351 |
+
"categoryId": cat_oid,
|
| 352 |
+
"click_count": 1,
|
| 353 |
+
"lastClickedAt": now
|
| 354 |
+
}
|
| 355 |
+
}
|
| 356 |
+
},
|
| 357 |
+
upsert=True
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
print("Insert/upsert raw_result:", insert_result.raw_result)
|
| 361 |
+
|
| 362 |
+
print("======= END MEDIA_CLICKS LOG =======\n")
|
| 363 |
+
|
| 364 |
+
except Exception as media_err:
|
| 365 |
+
print("❌ ERROR while saving media_clicks:", str(media_err))
|
| 366 |
+
# DO NOT break the face-swap flow
|
| 367 |
+
|
| 368 |
+
# if user_id:
|
| 369 |
+
# user_oid = ObjectId(user_id)
|
| 370 |
+
# cat_oid = ObjectId(category_id)
|
| 371 |
+
|
| 372 |
+
# now = datetime.utcnow()
|
| 373 |
+
|
| 374 |
+
# # Try to update existing category entry
|
| 375 |
+
# update_result = await media_clicks_col.update_one(
|
| 376 |
+
# {
|
| 377 |
+
# "userId": user_oid,
|
| 378 |
+
# "categories.categoryId": cat_oid
|
| 379 |
+
# },
|
| 380 |
+
# {
|
| 381 |
+
# "$set": {
|
| 382 |
+
# "updatedAt": now,
|
| 383 |
+
# "categories.$.lastClickedAt": now
|
| 384 |
+
# },
|
| 385 |
+
# "$inc": {
|
| 386 |
+
# "categories.$.click_count": 1
|
| 387 |
+
# }
|
| 388 |
+
# }
|
| 389 |
+
# )
|
| 390 |
+
|
| 391 |
+
# # If category did not exist, push new category object
|
| 392 |
+
# if update_result.matched_count == 0:
|
| 393 |
+
# await media_clicks_col.update_one(
|
| 394 |
+
# {"userId": user_oid},
|
| 395 |
+
# {
|
| 396 |
+
# "$setOnInsert": {"createdAt": now},
|
| 397 |
+
# "$set": {"updatedAt": now},
|
| 398 |
+
# "$push": {
|
| 399 |
+
# "categories": {
|
| 400 |
+
# "categoryId": cat_oid,
|
| 401 |
+
# "click_count": 1,
|
| 402 |
+
# "lastClickedAt": now
|
| 403 |
+
# }
|
| 404 |
+
# }
|
| 405 |
+
# },
|
| 406 |
+
# upsert=True
|
| 407 |
+
# )
|
| 408 |
|
| 409 |
# ==========================================================
|
| 410 |
# CASE 2: EXISTING TARGET_CATEGORY_ID (DigitalOcean)
|