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