Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
app.py
CHANGED
|
@@ -307,34 +307,34 @@ async def face_swap_api(
|
|
| 307 |
# ------------------------------------------------------------------
|
| 308 |
# MEDIA_CLICKS (ONLY IF user_id PRESENT)
|
| 309 |
# ------------------------------------------------------------------
|
|
|
|
| 310 |
if user_id:
|
| 311 |
try:
|
| 312 |
user_oid = ObjectId(user_id.strip())
|
| 313 |
now = datetime.utcnow()
|
| 314 |
-
|
|
|
|
| 315 |
update_result = await media_clicks_col.update_one(
|
| 316 |
-
|
| 317 |
-
{"userId": user_oid, "categories.categoryId": subcategory_oid},
|
| 318 |
{
|
| 319 |
"$set": {
|
| 320 |
"updatedAt": now,
|
| 321 |
-
"
|
| 322 |
},
|
| 323 |
-
"$inc": {"
|
| 324 |
}
|
| 325 |
)
|
| 326 |
-
|
|
|
|
| 327 |
if update_result.matched_count == 0:
|
| 328 |
-
# category does not exist → push new entry
|
| 329 |
await media_clicks_col.update_one(
|
| 330 |
{"userId": user_oid},
|
| 331 |
{
|
| 332 |
"$setOnInsert": {"createdAt": now},
|
| 333 |
"$set": {"updatedAt": now},
|
| 334 |
"$push": {
|
| 335 |
-
"
|
| 336 |
-
|
| 337 |
-
"categoryId": subcategory_oid,
|
| 338 |
"click_count": 1,
|
| 339 |
"lastClickedAt": now
|
| 340 |
}
|
|
@@ -342,9 +342,51 @@ async def face_swap_api(
|
|
| 342 |
},
|
| 343 |
upsert=True
|
| 344 |
)
|
| 345 |
-
|
| 346 |
except Exception as media_err:
|
| 347 |
logger.error(f"MEDIA_CLICK ERROR: {media_err}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
# do NOT interrupt faceswap flow
|
| 349 |
|
| 350 |
# ------------------------------------------------------------------
|
|
|
|
| 307 |
# ------------------------------------------------------------------
|
| 308 |
# MEDIA_CLICKS (ONLY IF user_id PRESENT)
|
| 309 |
# ------------------------------------------------------------------
|
| 310 |
+
# MEDIA_CLICKS (ONLY IF user_id PRESENT)
|
| 311 |
if user_id:
|
| 312 |
try:
|
| 313 |
user_oid = ObjectId(user_id.strip())
|
| 314 |
now = datetime.utcnow()
|
| 315 |
+
|
| 316 |
+
# Try updating existing subCategory
|
| 317 |
update_result = await media_clicks_col.update_one(
|
| 318 |
+
{"userId": user_oid, "subCategories.subCategoryId": subcategory_oid},
|
|
|
|
| 319 |
{
|
| 320 |
"$set": {
|
| 321 |
"updatedAt": now,
|
| 322 |
+
"subCategories.$.lastClickedAt": now
|
| 323 |
},
|
| 324 |
+
"$inc": {"subCategories.$.click_count": 1}
|
| 325 |
}
|
| 326 |
)
|
| 327 |
+
|
| 328 |
+
# If subCategory does NOT exist → push new one
|
| 329 |
if update_result.matched_count == 0:
|
|
|
|
| 330 |
await media_clicks_col.update_one(
|
| 331 |
{"userId": user_oid},
|
| 332 |
{
|
| 333 |
"$setOnInsert": {"createdAt": now},
|
| 334 |
"$set": {"updatedAt": now},
|
| 335 |
"$push": {
|
| 336 |
+
"subCategories": {
|
| 337 |
+
"subCategoryId": subcategory_oid,
|
|
|
|
| 338 |
"click_count": 1,
|
| 339 |
"lastClickedAt": now
|
| 340 |
}
|
|
|
|
| 342 |
},
|
| 343 |
upsert=True
|
| 344 |
)
|
| 345 |
+
|
| 346 |
except Exception as media_err:
|
| 347 |
logger.error(f"MEDIA_CLICK ERROR: {media_err}")
|
| 348 |
+
|
| 349 |
+
# if user_id:
|
| 350 |
+
# try:
|
| 351 |
+
# user_oid = ObjectId(user_id.strip())
|
| 352 |
+
# now = datetime.utcnow()
|
| 353 |
+
|
| 354 |
+
# update_result = await media_clicks_col.update_one(
|
| 355 |
+
# #{"userId": user_oid, "categories.categoryId": category_oid},
|
| 356 |
+
# {"userId": user_oid, "categories.subcategoryId": subcategory_oid},
|
| 357 |
+
|
| 358 |
+
|
| 359 |
+
# {
|
| 360 |
+
# "$set": {
|
| 361 |
+
# "updatedAt": now,
|
| 362 |
+
# "categories.$.lastClickedAt": now
|
| 363 |
+
# },
|
| 364 |
+
# "$inc": {"categories.$.click_count": 1}
|
| 365 |
+
# }
|
| 366 |
+
# )
|
| 367 |
+
|
| 368 |
+
# if update_result.matched_count == 0:
|
| 369 |
+
# # category does not exist → push new entry
|
| 370 |
+
# await media_clicks_col.update_one(
|
| 371 |
+
# {"userId": user_oid},
|
| 372 |
+
# {
|
| 373 |
+
# "$setOnInsert": {"createdAt": now},
|
| 374 |
+
# "$set": {"updatedAt": now},
|
| 375 |
+
# "$push": {
|
| 376 |
+
# "categories": {
|
| 377 |
+
# #"categoryId": category_oid,
|
| 378 |
+
# #categoryId": subcategory_oid,
|
| 379 |
+
# "subCategoryId": subcategory_oid,
|
| 380 |
+
# "click_count": 1,
|
| 381 |
+
# "lastClickedAt": now
|
| 382 |
+
# }
|
| 383 |
+
# }
|
| 384 |
+
# },
|
| 385 |
+
# upsert=True
|
| 386 |
+
# )
|
| 387 |
+
|
| 388 |
+
# except Exception as media_err:
|
| 389 |
+
# logger.error(f"MEDIA_CLICK ERROR: {media_err}")
|
| 390 |
# do NOT interrupt faceswap flow
|
| 391 |
|
| 392 |
# ------------------------------------------------------------------
|