|
9 | 9 | import json |
10 | 10 | import logging |
11 | 11 | import os |
| 12 | +import re |
12 | 13 | import uuid |
13 | 14 | from datetime import datetime, timezone |
14 | 15 | from typing import Dict, Any |
@@ -992,12 +993,28 @@ async def generate(): |
992 | 993 | existing_content = raw_content if isinstance(raw_content, dict) else {} |
993 | 994 | old_image_url = existing_content.get("image_url") |
994 | 995 |
|
| 996 | + # Replace old color/product name in text_content when product changes |
| 997 | + old_products = existing_content.get("selected_products", []) |
| 998 | + old_name = old_products[0].get("product_name", "") if old_products else "" |
| 999 | + new_name = products_data[0].get("product_name", "") if products_data else "" |
| 1000 | + existing_text = existing_content.get("text_content") |
| 1001 | + if existing_text and old_name and new_name and old_name != new_name: |
| 1002 | + pat = re.compile(re.escape(old_name), re.IGNORECASE) |
| 1003 | + if isinstance(existing_text, dict): |
| 1004 | + existing_text = { |
| 1005 | + k: pat.sub(lambda _m: new_name, v) if isinstance(v, str) else v |
| 1006 | + for k, v in existing_text.items() |
| 1007 | + } |
| 1008 | + elif isinstance(existing_text, str): |
| 1009 | + existing_text = pat.sub(lambda _m: new_name, existing_text) |
| 1010 | + |
995 | 1011 | updated_content = { |
996 | 1012 | **existing_content, |
997 | 1013 | "image_url": new_image_url if new_image_url else old_image_url, |
998 | 1014 | "image_prompt": new_image_prompt if new_image_prompt else existing_content.get("image_prompt"), |
999 | 1015 | "image_revised_prompt": new_image_revised_prompt if new_image_revised_prompt else existing_content.get("image_revised_prompt"), |
1000 | 1016 | "selected_products": products_data if products_data else existing_content.get("selected_products", []), |
| 1017 | + **(({"text_content": existing_text} if existing_text is not None else {})), |
1001 | 1018 | } |
1002 | 1019 |
|
1003 | 1020 | await cosmos_service.save_generated_content( |
|
0 commit comments