LogicGoInfotechSpaces commited on
Commit
d6c9bc8
·
verified ·
1 Parent(s): 0a0439b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -119
app.py CHANGED
@@ -175,152 +175,58 @@ async def generate(
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
  {
221
- "userId": user_oid,
222
- "categories.categoryId": category_oid
223
- },
224
- [
225
- {
226
- "$set": {
227
- "updatedAt": now,
228
- "ai_edit_last_date": now,
229
- "ai_edit_complete": {
230
- "$add": [
231
- { "$ifNull": ["$ai_edit_complete", 0] },
232
- 1
233
- ]
234
- },
235
- "categories": {
236
- "$map": {
237
- "input": "$categories",
238
- "as": "cat",
239
- "in": {
240
- "$cond": [
241
- { "$eq": ["$$cat.categoryId", category_oid] },
242
- {
243
- "$mergeObjects": [
244
- "$$cat",
245
- {
246
- "click_count": {
247
- "$add": [
248
- { "$ifNull": ["$$cat.click_count", 0] },
249
- 1
250
- ]
251
- },
252
- "lastClickedAt": now
253
- }
254
- ]
255
- },
256
- "$$cat"
257
- ]
258
- }
259
- }
260
- }
261
  }
262
  }
263
- ],
264
  upsert=True
265
  )
266
 
267
-
268
  except Exception as e:
269
  print("CATEGORY_LOG_ERROR:", e)
270
- # Do NOT break image generation
271
-
272
-
273
- # if user_id and category_id:
274
- # try:
275
- # admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
276
- # admin_db = admin_client["adminPanel"]
277
-
278
- # categories_col = admin_db.categories
279
- # media_clicks_col = admin_db.media_clicks
280
-
281
- # # Validate user_oid & category_oid
282
- # user_oid = ObjectId(user_id)
283
- # category_oid = ObjectId(category_id)
284
-
285
- # # Check category exists
286
- # category_doc = categories_col.find_one({"_id": category_oid})
287
- # if not category_doc:
288
- # raise HTTPException(400, f"Invalid category_id: {category_id}")
289
-
290
- # now = datetime.utcnow()
291
-
292
- # # Try updating existing category click
293
- # update_res = media_clicks_col.update_one(
294
- # {"userId": user_oid, "categories.categoryId": category_oid},
295
- # {
296
- # "$set": {
297
- # "updatedAt": now,
298
- # "categories.$.lastClickedAt": now
299
- # },
300
- # "$inc": {"categories.$.click_count": 1}
301
- # }
302
- # )
303
-
304
- # # If user or category does not exist → create/push new
305
- # if update_res.matched_count == 0:
306
- # media_clicks_col.update_one(
307
- # {"userId": user_oid},
308
- # {
309
- # "$setOnInsert": {"createdAt": now},
310
- # "$set": {"updatedAt": now},
311
- # "$push": {
312
- # "categories": {
313
- # "categoryId": category_oid,
314
- # "click_count": 1,
315
- # "lastClickedAt": now
316
- # }
317
- # }
318
- # },
319
- # upsert=True
320
- # )
321
-
322
- # except Exception as e:
323
- # print("CATEGORY_LOG_ERROR:", e)
324
  # but do NOT stop the image generation
325
 
326
  # -------------------------
 
175
  # -------------------------
176
  # 3. OPTIONAL CATEGORY CLICK LOGIC
177
  # -------------------------
178
+
 
 
179
  if user_id and category_id:
180
  try:
181
  admin_client = MongoClient(os.getenv("ADMIN_MONGODB_URI"))
182
  admin_db = admin_client["adminPanel"]
183
+
184
  categories_col = admin_db.categories
185
  media_clicks_col = admin_db.media_clicks
186
+
187
+ # Validate user_oid & category_oid
188
  user_oid = ObjectId(user_id)
189
  category_oid = ObjectId(category_id)
190
+
191
+ # Check category exists
192
+ category_doc = categories_col.find_one({"_id": category_oid})
193
+ if not category_doc:
194
  raise HTTPException(400, f"Invalid category_id: {category_id}")
195
+
196
  now = datetime.utcnow()
197
+
198
+ # Try updating existing category click
199
  update_res = media_clicks_col.update_one(
200
+ {"userId": user_oid, "categories.categoryId": category_oid},
 
 
 
201
  {
202
  "$set": {
203
  "updatedAt": now,
204
+ "categories.$.lastClickedAt": now
 
205
  },
206
+ "$inc": {"categories.$.click_count": 1}
 
 
 
207
  }
208
  )
209
+
210
+ # If user or category does not exist → create/push new
211
  if update_res.matched_count == 0:
212
  media_clicks_col.update_one(
213
+ {"userId": user_oid},
214
  {
215
+ "$setOnInsert": {"createdAt": now},
216
+ "$set": {"updatedAt": now},
217
+ "$push": {
218
+ "categories": {
219
+ "categoryId": category_oid,
220
+ "click_count": 1,
221
+ "lastClickedAt": now
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
222
  }
223
  }
224
+ },
225
  upsert=True
226
  )
227
 
 
228
  except Exception as e:
229
  print("CATEGORY_LOG_ERROR:", e)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  # but do NOT stop the image generation
231
 
232
  # -------------------------