Update app.py
Browse files
app.py
CHANGED
|
@@ -117,7 +117,12 @@ def normalize_category_id(raw_category_id):
|
|
| 117 |
if value == "":
|
| 118 |
return DEFAULT_CATEGORY_FALLBACK, "default_empty"
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
##-------NEW CATEGORY CLICK-------#####
|
| 123 |
def log_media_click(raw_user_id, raw_category_id):
|
|
@@ -202,7 +207,26 @@ def log_media_click(raw_user_id, raw_category_id):
|
|
| 202 |
)
|
| 203 |
|
| 204 |
# --------------------------------------------------
|
| 205 |
-
# STEP 3:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
# --------------------------------------------------
|
| 207 |
update_existing = media_clicks_collection.update_one(
|
| 208 |
{
|
|
@@ -226,7 +250,7 @@ def log_media_click(raw_user_id, raw_category_id):
|
|
| 226 |
return
|
| 227 |
|
| 228 |
# --------------------------------------------------
|
| 229 |
-
# STEP
|
| 230 |
# --------------------------------------------------
|
| 231 |
media_clicks_collection.update_one(
|
| 232 |
{"userId": user_oid},
|
|
@@ -579,7 +603,6 @@ def download(filename: str):
|
|
| 579 |
return FileResponse(file_path, media_type="image/png", filename=filename)
|
| 580 |
|
| 581 |
|
| 582 |
-
|
| 583 |
# import hashlib
|
| 584 |
# import logging
|
| 585 |
# import os
|
|
|
|
| 117 |
if value == "":
|
| 118 |
return DEFAULT_CATEGORY_FALLBACK, "default_empty"
|
| 119 |
|
| 120 |
+
# Always store categoryId as an ObjectId in Mongo
|
| 121 |
+
try:
|
| 122 |
+
return ObjectId(value), "provided_objectid"
|
| 123 |
+
except Exception:
|
| 124 |
+
# If somehow not a valid hex string, fall back to hashing
|
| 125 |
+
return _hash_to_object_id(value), "provided_hashed"
|
| 126 |
|
| 127 |
##-------NEW CATEGORY CLICK-------#####
|
| 128 |
def log_media_click(raw_user_id, raw_category_id):
|
|
|
|
| 207 |
)
|
| 208 |
|
| 209 |
# --------------------------------------------------
|
| 210 |
+
# STEP 3: NORMALIZE OLD STRING CATEGORY IDs TO ObjectId
|
| 211 |
+
# --------------------------------------------------
|
| 212 |
+
# If there is an existing category entry where categoryId was stored
|
| 213 |
+
# as a string, convert that field to an ObjectId so future updates
|
| 214 |
+
# always work with a consistent type.
|
| 215 |
+
category_id_str = str(category_id)
|
| 216 |
+
media_clicks_collection.update_one(
|
| 217 |
+
{
|
| 218 |
+
"userId": user_oid,
|
| 219 |
+
"categories.categoryId": category_id_str,
|
| 220 |
+
},
|
| 221 |
+
{
|
| 222 |
+
"$set": {
|
| 223 |
+
"categories.$.categoryId": category_id,
|
| 224 |
+
}
|
| 225 |
+
},
|
| 226 |
+
)
|
| 227 |
+
|
| 228 |
+
# --------------------------------------------------
|
| 229 |
+
# STEP 4: UPDATE EXISTING CATEGORY (DATES CAN REPEAT)
|
| 230 |
# --------------------------------------------------
|
| 231 |
update_existing = media_clicks_collection.update_one(
|
| 232 |
{
|
|
|
|
| 250 |
return
|
| 251 |
|
| 252 |
# --------------------------------------------------
|
| 253 |
+
# STEP 5: PUSH NEW CATEGORY (ORDER = TIME)
|
| 254 |
# --------------------------------------------------
|
| 255 |
media_clicks_collection.update_one(
|
| 256 |
{"userId": user_oid},
|
|
|
|
| 603 |
return FileResponse(file_path, media_type="image/png", filename=filename)
|
| 604 |
|
| 605 |
|
|
|
|
| 606 |
# import hashlib
|
| 607 |
# import logging
|
| 608 |
# import os
|