LogicGoInfotechSpaces commited on
Commit
4a1ba8d
·
1 Parent(s): defc757

Add category_id and user_id as optional parameters to all edit endpoints

Browse files
Files changed (2) hide show
  1. app.py +32 -9
  2. postman_collection.json +18 -4
app.py CHANGED
@@ -296,11 +296,21 @@ def save_media_click(user_id: Optional[int], category_id: str):
296
  existing_doc = collection.find_one({"userId": user_object_id})
297
 
298
  if existing_doc:
 
299
  # Document exists, check if category exists in categories array
300
  category_exists = False
301
  for cat in existing_doc.get("categories", []):
302
- if cat.get("categoryId") == category_object_id:
 
 
 
 
 
 
 
303
  category_exists = True
 
 
304
  break
305
 
306
  if category_exists:
@@ -318,9 +328,13 @@ def save_media_click(user_id: Optional[int], category_id: str):
318
  }
319
  }
320
  )
321
- print(f"[MongoDB] Updated category click_count - userId: {user_object_id}, categoryId: {category_object_id}")
 
 
 
322
  else:
323
  # Category doesn't exist, add new category to array
 
324
  result = collection.update_one(
325
  {"userId": user_object_id},
326
  {
@@ -334,7 +348,10 @@ def save_media_click(user_id: Optional[int], category_id: str):
334
  "$set": {"updatedAt": now}
335
  }
336
  )
337
- print(f"[MongoDB] Added new category - userId: {user_object_id}, categoryId: {category_object_id}")
 
 
 
338
  else:
339
  # Document doesn't exist, try to create new document
340
  # Use find_one_and_update with upsert which handles conflicts better
@@ -768,16 +785,18 @@ async def edit_image_nanobanana(
768
  request: Request,
769
  image: UploadFile = File(...),
770
  user_id: Optional[str] = Form(None),
 
771
  app_check_claims: dict = Depends(verify_app_check_token),
772
  ):
773
  """
774
  Edit an image using the default nanobanana prompt.
775
  Automatically saves to nanobanana MongoDB database.
776
- Automatically uses categoryId: 69368d62b95a6c2a75920505 for media_clicks logging.
777
 
778
  Parameters:
779
  - image: Image file to edit
780
  - user_id: Optional integer user ID
 
781
  """
782
  task_id = str(uuid.uuid4())
783
  start_time = datetime.utcnow()
@@ -792,8 +811,9 @@ async def edit_image_nanobanana(
792
  except ValueError:
793
  return JSONResponse({"error": "user_id must be a valid integer"}, 400)
794
 
795
- # Set default categoryId for nanobanana
796
- category_id = "69368d62b95a6c2a75920505"
 
797
 
798
  response, mongo_data = await process_image_edit(
799
  image=image,
@@ -813,16 +833,18 @@ async def edit_image_descratch(
813
  request: Request,
814
  image: UploadFile = File(...),
815
  user_id: Optional[str] = Form(None),
 
816
  app_check_claims: dict = Depends(verify_app_check_token),
817
  ):
818
  """
819
  Edit an image using the default Descratch prompt.
820
  Automatically saves to Descratch_logicgo MongoDB database.
821
- Automatically uses categoryId: 69368fbb2e46bd68ae18899e for media_clicks logging.
822
 
823
  Parameters:
824
  - image: Image file to edit
825
  - user_id: Optional integer user ID
 
826
  """
827
  task_id = str(uuid.uuid4())
828
  start_time = datetime.utcnow()
@@ -837,8 +859,9 @@ async def edit_image_descratch(
837
  except ValueError:
838
  return JSONResponse({"error": "user_id must be a valid integer"}, 400)
839
 
840
- # Set default categoryId for descratch
841
- category_id = "69368fbb2e46bd68ae18899e"
 
842
 
843
  response, mongo_data = await process_image_edit(
844
  image=image,
 
296
  existing_doc = collection.find_one({"userId": user_object_id})
297
 
298
  if existing_doc:
299
+ print(f"[MongoDB] Found existing document for userId: {user_object_id}, categories count: {len(existing_doc.get('categories', []))}")
300
  # Document exists, check if category exists in categories array
301
  category_exists = False
302
  for cat in existing_doc.get("categories", []):
303
+ cat_id = cat.get("categoryId")
304
+ if isinstance(cat_id, ObjectId):
305
+ if cat_id == category_object_id:
306
+ category_exists = True
307
+ current_count = cat.get("click_count", 0)
308
+ print(f"[MongoDB] Category found - categoryId: {category_object_id}, current click_count: {current_count}")
309
+ break
310
+ elif str(cat_id) == str(category_object_id):
311
  category_exists = True
312
+ current_count = cat.get("click_count", 0)
313
+ print(f"[MongoDB] Category found (string match) - categoryId: {category_object_id}, current click_count: {current_count}")
314
  break
315
 
316
  if category_exists:
 
328
  }
329
  }
330
  )
331
+ if result.modified_count > 0:
332
+ print(f"[MongoDB] Updated category click_count - userId: {user_object_id}, categoryId: {category_object_id}, matched: {result.matched_count}, modified: {result.modified_count}")
333
+ else:
334
+ print(f"[MongoDB] WARNING: Update matched but did not modify - userId: {user_object_id}, categoryId: {category_object_id}, matched: {result.matched_count}, modified: {result.modified_count}")
335
  else:
336
  # Category doesn't exist, add new category to array
337
+ print(f"[MongoDB] Category not found in existing document, adding new category - userId: {user_object_id}, categoryId: {category_object_id}")
338
  result = collection.update_one(
339
  {"userId": user_object_id},
340
  {
 
348
  "$set": {"updatedAt": now}
349
  }
350
  )
351
+ if result.modified_count > 0:
352
+ print(f"[MongoDB] Added new category - userId: {user_object_id}, categoryId: {category_object_id}, matched: {result.matched_count}, modified: {result.modified_count}")
353
+ else:
354
+ print(f"[MongoDB] WARNING: Add category matched but did not modify - userId: {user_object_id}, categoryId: {category_object_id}, matched: {result.matched_count}, modified: {result.modified_count}")
355
  else:
356
  # Document doesn't exist, try to create new document
357
  # Use find_one_and_update with upsert which handles conflicts better
 
785
  request: Request,
786
  image: UploadFile = File(...),
787
  user_id: Optional[str] = Form(None),
788
+ category_id: Optional[str] = Form(None),
789
  app_check_claims: dict = Depends(verify_app_check_token),
790
  ):
791
  """
792
  Edit an image using the default nanobanana prompt.
793
  Automatically saves to nanobanana MongoDB database.
794
+ Uses categoryId: 69368d62b95a6c2a75920505 for media_clicks logging (default, can be overridden).
795
 
796
  Parameters:
797
  - image: Image file to edit
798
  - user_id: Optional integer user ID
799
+ - category_id: Optional MongoDB ObjectId string for category (defaults to 69368d62b95a6c2a75920505)
800
  """
801
  task_id = str(uuid.uuid4())
802
  start_time = datetime.utcnow()
 
811
  except ValueError:
812
  return JSONResponse({"error": "user_id must be a valid integer"}, 400)
813
 
814
+ # Set default categoryId for nanobanana if not provided
815
+ if not category_id:
816
+ category_id = "69368d62b95a6c2a75920505"
817
 
818
  response, mongo_data = await process_image_edit(
819
  image=image,
 
833
  request: Request,
834
  image: UploadFile = File(...),
835
  user_id: Optional[str] = Form(None),
836
+ category_id: Optional[str] = Form(None),
837
  app_check_claims: dict = Depends(verify_app_check_token),
838
  ):
839
  """
840
  Edit an image using the default Descratch prompt.
841
  Automatically saves to Descratch_logicgo MongoDB database.
842
+ Uses categoryId: 69368fbb2e46bd68ae18899e for media_clicks logging (default, can be overridden).
843
 
844
  Parameters:
845
  - image: Image file to edit
846
  - user_id: Optional integer user ID
847
+ - category_id: Optional MongoDB ObjectId string for category (defaults to 69368fbb2e46bd68ae18899e)
848
  """
849
  task_id = str(uuid.uuid4())
850
  start_time = datetime.utcnow()
 
859
  except ValueError:
860
  return JSONResponse({"error": "user_id must be a valid integer"}, 400)
861
 
862
+ # Set default categoryId for descratch if not provided
863
+ if not category_id:
864
+ category_id = "69368fbb2e46bd68ae18899e"
865
 
866
  response, mongo_data = await process_image_edit(
867
  image=image,
postman_collection.json CHANGED
@@ -277,7 +277,14 @@
277
  "value": "{{userId}}",
278
  "type": "text",
279
  "disabled": false,
280
- "description": "Optional: Integer user ID. If provided, logs to media_clicks collection with categoryId: 69368d62b95a6c2a75920505"
 
 
 
 
 
 
 
281
  }
282
  ]
283
  },
@@ -325,7 +332,14 @@
325
  "value": "{{userId}}",
326
  "type": "text",
327
  "disabled": false,
328
- "description": "Optional: Integer user ID. If provided, logs to media_clicks collection with categoryId: 69368fbb2e46bd68ae18899e"
 
 
 
 
 
 
 
329
  }
330
  ]
331
  },
@@ -557,8 +571,8 @@
557
  },
558
  {
559
  "key": "categoryId",
560
- "value": "69368fbb2e46bd68ae18899e",
561
- "description": "Optional: MongoDB ObjectId string for category (24 hex characters). Used only for custom /edit endpoint."
562
  }
563
  ]
564
  }
 
277
  "value": "{{userId}}",
278
  "type": "text",
279
  "disabled": false,
280
+ "description": "Optional: Integer user ID. Required for media_clicks logging."
281
+ },
282
+ {
283
+ "key": "category_id",
284
+ "value": "{{categoryId}}",
285
+ "type": "text",
286
+ "disabled": false,
287
+ "description": "Optional: MongoDB ObjectId string for category. Defaults to 69368d62b95a6c2a75920505 if not provided."
288
  }
289
  ]
290
  },
 
332
  "value": "{{userId}}",
333
  "type": "text",
334
  "disabled": false,
335
+ "description": "Optional: Integer user ID. Required for media_clicks logging."
336
+ },
337
+ {
338
+ "key": "category_id",
339
+ "value": "{{categoryId}}",
340
+ "type": "text",
341
+ "disabled": false,
342
+ "description": "Optional: MongoDB ObjectId string for category. Defaults to 69368fbb2e46bd68ae18899e if not provided."
343
  }
344
  ]
345
  },
 
571
  },
572
  {
573
  "key": "categoryId",
574
+ "value": "69368d62b95a6c2a75920505",
575
+ "description": "Optional: MongoDB ObjectId string for category (24 hex characters). Defaults: nanobanana=69368d62b95a6c2a75920505, descratch=69368fbb2e46bd68ae18899e"
576
  }
577
  ]
578
  }