LogicGoInfotechSpaces commited on
Commit
6f01ae6
·
verified ·
1 Parent(s): 92a5960

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -18
app.py CHANGED
@@ -175,44 +175,61 @@ async def generate(
175
  # -------------------------
176
  # 3. OPTIONAL CATEGORY CLICK LOGIC
177
  # -------------------------
 
 
 
178
  if user_id and category_id:
179
  try:
180
  admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
181
  admin_db = admin_client["adminPanel"]
182
-
183
  categories_col = admin_db.categories
184
  media_clicks_col = admin_db.media_clicks
185
-
186
- # Validate user_oid & category_oid
187
  user_oid = ObjectId(user_id)
188
  category_oid = ObjectId(category_id)
189
-
190
- # Check category exists
191
- category_doc = categories_col.find_one({"_id": category_oid})
192
- if not category_doc:
193
  raise HTTPException(400, f"Invalid category_id: {category_id}")
194
-
195
  now = datetime.utcnow()
196
-
197
- # Try updating existing category click
198
  update_res = media_clicks_col.update_one(
199
- {"userId": user_oid, "categories.categoryId": category_oid},
 
 
 
200
  {
201
  "$set": {
202
  "updatedAt": now,
203
- "categories.$.lastClickedAt": now
 
204
  },
205
- "$inc": {"categories.$.click_count": 1}
 
 
 
206
  }
207
  )
208
-
209
- # If user or category does not exist → create/push new
210
  if update_res.matched_count == 0:
211
  media_clicks_col.update_one(
212
  {"userId": user_oid},
213
  {
214
- "$setOnInsert": {"createdAt": now},
215
- "$set": {"updatedAt": now},
 
 
 
 
 
 
 
 
 
216
  "$push": {
217
  "categories": {
218
  "categoryId": category_oid,
@@ -223,9 +240,63 @@ async def generate(
223
  },
224
  upsert=True
225
  )
226
-
227
  except Exception as e:
228
  print("CATEGORY_LOG_ERROR:", e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  # but do NOT stop the image generation
230
 
231
  # -------------------------
 
175
  # -------------------------
176
  # 3. OPTIONAL CATEGORY CLICK LOGIC
177
  # -------------------------
178
+ # -------------------------
179
+ # 3. OPTIONAL CATEGORY CLICK + AI EDIT TRACKING
180
+ # -------------------------
181
  if user_id and category_id:
182
  try:
183
  admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
184
  admin_db = admin_client["adminPanel"]
185
+
186
  categories_col = admin_db.categories
187
  media_clicks_col = admin_db.media_clicks
188
+
 
189
  user_oid = ObjectId(user_id)
190
  category_oid = ObjectId(category_id)
191
+
192
+ # Ensure category exists
193
+ if not categories_col.find_one({"_id": category_oid}):
 
194
  raise HTTPException(400, f"Invalid category_id: {category_id}")
195
+
196
  now = datetime.utcnow()
197
+
198
+ # Try update existing category + AI edit fields
199
  update_res = media_clicks_col.update_one(
200
+ {
201
+ "userId": user_oid,
202
+ "categories.categoryId": category_oid
203
+ },
204
  {
205
  "$set": {
206
  "updatedAt": now,
207
+ "categories.$.lastClickedAt": now,
208
+ "ai_edit_last_date": now
209
  },
210
+ "$inc": {
211
+ "categories.$.click_count": 1,
212
+ "ai_edit_complete": 1
213
+ }
214
  }
215
  )
216
+
217
+ # If category or user does not exist
218
  if update_res.matched_count == 0:
219
  media_clicks_col.update_one(
220
  {"userId": user_oid},
221
  {
222
+ "$setOnInsert": {
223
+ "createdAt": now,
224
+ "ai_edit_complete": 0
225
+ },
226
+ "$set": {
227
+ "updatedAt": now,
228
+ "ai_edit_last_date": now
229
+ },
230
+ "$inc": {
231
+ "ai_edit_complete": 1
232
+ },
233
  "$push": {
234
  "categories": {
235
  "categoryId": category_oid,
 
240
  },
241
  upsert=True
242
  )
243
+
244
  except Exception as e:
245
  print("CATEGORY_LOG_ERROR:", e)
246
+ # Do NOT break image generation
247
+
248
+
249
+ # if user_id and category_id:
250
+ # try:
251
+ # admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
252
+ # admin_db = admin_client["adminPanel"]
253
+
254
+ # categories_col = admin_db.categories
255
+ # media_clicks_col = admin_db.media_clicks
256
+
257
+ # # Validate user_oid & category_oid
258
+ # user_oid = ObjectId(user_id)
259
+ # category_oid = ObjectId(category_id)
260
+
261
+ # # Check category exists
262
+ # category_doc = categories_col.find_one({"_id": category_oid})
263
+ # if not category_doc:
264
+ # raise HTTPException(400, f"Invalid category_id: {category_id}")
265
+
266
+ # now = datetime.utcnow()
267
+
268
+ # # Try updating existing category click
269
+ # update_res = media_clicks_col.update_one(
270
+ # {"userId": user_oid, "categories.categoryId": category_oid},
271
+ # {
272
+ # "$set": {
273
+ # "updatedAt": now,
274
+ # "categories.$.lastClickedAt": now
275
+ # },
276
+ # "$inc": {"categories.$.click_count": 1}
277
+ # }
278
+ # )
279
+
280
+ # # If user or category does not exist → create/push new
281
+ # if update_res.matched_count == 0:
282
+ # media_clicks_col.update_one(
283
+ # {"userId": user_oid},
284
+ # {
285
+ # "$setOnInsert": {"createdAt": now},
286
+ # "$set": {"updatedAt": now},
287
+ # "$push": {
288
+ # "categories": {
289
+ # "categoryId": category_oid,
290
+ # "click_count": 1,
291
+ # "lastClickedAt": now
292
+ # }
293
+ # }
294
+ # },
295
+ # upsert=True
296
+ # )
297
+
298
+ # except Exception as e:
299
+ # print("CATEGORY_LOG_ERROR:", e)
300
  # but do NOT stop the image generation
301
 
302
  # -------------------------