#!/usr/bin/env python3 """Script to add ObjectId conversion before creating model instances""" import re def add_objectid_conversion(file_path, model_name): """Add doc['_id'] = str(doc['_id']) before Model(**doc) calls""" with open(file_path, 'r') as f: lines = f.readlines() modified_lines = [] i = 0 changes_made = 0 while i < len(lines): line = lines[i] # Pattern 1: Single line creation like: return Keyword(**doc) if re.search(rf'{model_name}\(\*\*doc\)', line): indent = len(line) - len(line.lstrip()) # Check if previous line already has the conversion if i > 0 and 'doc["_id"] = str(doc["_id"])' in lines[i-1]: modified_lines.append(line) i += 1 continue # Check if this is inside a loop or conditional that already has conversion if i > 0 and any('doc["_id"] = str(doc["_id"])' in lines[j] for j in range(max(0, i-5), i)): modified_lines.append(line) i += 1 continue # Add conversion before this line modified_lines.append(' ' * indent + 'doc["_id"] = str(doc["_id"]) # Convert ObjectId to string\n') modified_lines.append(line) changes_made += 1 i += 1 continue # Pattern 2: In loops like: keywords.append(Keyword(**doc)) if re.search(rf'{model_name}\(\*\*doc\)', line) and ('append' in line or 'for' in line): indent = len(line) - len(line.lstrip()) # Check if previous line already has the conversion if i > 0 and 'doc["_id"] = str(doc["_id"])' in lines[i-1]: modified_lines.append(line) i += 1 continue # Add conversion before this line modified_lines.append(' ' * indent + 'doc["_id"] = str(doc["_id"]) # Convert ObjectId to string\n') modified_lines.append(line) changes_made += 1 i += 1 continue # Pattern 3: Dictionary creation like: return Keyword(**keyword_dict) match = re.search(rf'{model_name}\(\*\*(\w+)\)', line) if match and match.group(1) != 'doc': dict_name = match.group(1) indent = len(line) - len(line.lstrip()) # Check if previous line already has the conversion if i > 0 and f'{dict_name}["_id"] = str({dict_name}["_id"])' in lines[i-1]: modified_lines.append(line) i += 1 continue # Add conversion before this line modified_lines.append(' ' * indent + f'{dict_name}["_id"] = str({dict_name}["_id"]) # Convert ObjectId to string\n') modified_lines.append(line) changes_made += 1 i += 1 continue # Pattern 4: List comprehension like: [Keyword(**kw) for kw in keywords_dicts] if re.search(rf'\[{model_name}\(\*\*\w+\)', line): # This needs manual fixing as it's a list comprehension print(f"Line {i+1}: List comprehension found, needs manual fixing: {line.strip()}") modified_lines.append(line) i += 1 # Write back with open(file_path, 'w') as f: f.writelines(modified_lines) print(f"Modified {file_path}: {changes_made} changes made") return changes_made if __name__ == "__main__": total = 0 total += add_objectid_conversion("app/services/keyword_service.py", "Keyword") total += add_objectid_conversion("app/services/pipeline_service.py", "Pipeline") total += add_objectid_conversion("app/services/application_service.py", "Application") print(f"\nTotal changes: {total}")