Commit
·
5fda188
1
Parent(s):
212462e
Handle category field as string name (not ObjectId) in budgets
Browse files- app/smart_recommendation.py +26 -6
app/smart_recommendation.py
CHANGED
|
@@ -483,13 +483,33 @@ class SmartBudgetRecommender:
|
|
| 483 |
# Get category name from headCategories or categories collection using category ID
|
| 484 |
if category_id:
|
| 485 |
print(f"🔍 Looking up category ID: {category_id} (type: {type(category_id).__name__})")
|
| 486 |
-
|
| 487 |
-
if
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 491 |
else:
|
| 492 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 493 |
else:
|
| 494 |
# Fallback to budget name if no category ID found
|
| 495 |
category_name = b.get("name", "Uncategorized")
|
|
|
|
| 483 |
# Get category name from headCategories or categories collection using category ID
|
| 484 |
if category_id:
|
| 485 |
print(f"🔍 Looking up category ID: {category_id} (type: {type(category_id).__name__})")
|
| 486 |
+
|
| 487 |
+
# Check if category_id is already a string name (not a valid ObjectId)
|
| 488 |
+
if isinstance(category_id, str):
|
| 489 |
+
# Check if it's a valid ObjectId format (24 hex characters)
|
| 490 |
+
is_valid_objectid = len(category_id) == 24 and all(c in '0123456789abcdefABCDEF' for c in category_id)
|
| 491 |
+
if not is_valid_objectid:
|
| 492 |
+
# It's already a category name, use it directly
|
| 493 |
+
category_name = category_id
|
| 494 |
+
print(f"✅ Using category name directly (not an ObjectId): '{category_name}'")
|
| 495 |
+
else:
|
| 496 |
+
# It's a valid ObjectId string, try to look it up
|
| 497 |
+
category_name = self._get_category_name(category_id)
|
| 498 |
+
if category_name == str(category_id):
|
| 499 |
+
# Category name lookup failed, still showing ID
|
| 500 |
+
print(f"⚠️ Category ID not resolved: {category_id} (not found in headCategories or categories collections)")
|
| 501 |
+
print(f" This means the category ID doesn't exist in the database. Please check if the category exists.")
|
| 502 |
+
else:
|
| 503 |
+
print(f"✅ Found category ID: {category_id} -> Name: '{category_name}'")
|
| 504 |
else:
|
| 505 |
+
# It's an ObjectId object, look it up
|
| 506 |
+
category_name = self._get_category_name(category_id)
|
| 507 |
+
if category_name == str(category_id):
|
| 508 |
+
# Category name lookup failed, still showing ID
|
| 509 |
+
print(f"⚠️ Category ID not resolved: {category_id} (not found in headCategories or categories collections)")
|
| 510 |
+
print(f" This means the category ID doesn't exist in the database. Please check if the category exists.")
|
| 511 |
+
else:
|
| 512 |
+
print(f"✅ Found category ID: {category_id} -> Name: '{category_name}'")
|
| 513 |
else:
|
| 514 |
# Fallback to budget name if no category ID found
|
| 515 |
category_name = b.get("name", "Uncategorized")
|