import sys import os file_path = 'core/views.py' with open(file_path, 'r') as f: content = f.read() old_code = """ # Prepare data for Google Map map_data = [ { 'lat': h['latitude'], 'lng': h['longitude'], 'address': f"{h['address_street']}, {h['city']}, {h['state']}", 'voters': ", ".join([f"{v.first_name} {v.last_name}" for v in h['voters_who_want_sign']]), 'notes': ", ".join([f"{v.first_name}: {v.notes}" for v in h['voters_with_sign' if 'voters_with_sign' in h else 'voters_who_want_sign'] if v.notes]), 'voter_ids': [v.id for v in h['voters_who_want_sign']] } for h in households_list if h['latitude'] and h['longitude'] ]""" # Note: The above replacement might fail because I used a complex list comprehension. # Let's try to be more precise for yard_sign_voters. content = content.replace( """ # Prepare data for Google Map map_data = [ { 'lat': h['latitude'], 'lng': h['longitude'], 'address': f"{h['address_street']}, {h['city']}, {h['state']}", 'voters': ", ".join([f"{v.first_name} {v.last_name}" for v in h['voters_who_want_sign']]), 'notes': ", ".join([f"{v.first_name}: {v.notes}" for v in h['voters_who_want_sign'] if v.notes]), 'voter_ids': [v.id for v in h['voters_who_want_sign']] } for h in households_list if h['latitude'] and h['longitude'] ]""", """ # Prepare data for Google Map # Limit map markers to 3000 for performance map_data = [ { 'lat': h['latitude'], 'lng': h['longitude'], 'address': f"{h['address_street']}, {h['city']}, {h['state']}", 'voters': ", ".join([f"{v.first_name} {v.last_name}" for v in h['voters_who_want_sign']]), 'notes': ", ".join([f"{v.first_name}: {v.notes}" for v in h['voters_who_want_sign'] if v.notes]), 'voter_ids': [v.id for v in h['voters_who_want_sign']] } for h in households_list[:3000] if h['latitude'] and h['longitude'] ]""" ) # And the context for yard_sign_voters content = content.replace( """ context = { 'selected_tenant': tenant, 'households': households_page, 'district_filter': district_filter, 'neighborhood_filter': neighborhood_filter, 'address_filter': address_filter, 'city_filter': city_filter, 'map_data_json': json.dumps(map_data), 'GOOGLE_MAPS_API_KEY': getattr(settings, 'GOOGLE_MAPS_API_KEY', ''), }""", """ context = { 'selected_tenant': tenant, 'households': households_page, 'district_filter': district_filter, 'neighborhood_filter': neighborhood_filter, 'address_filter': address_filter, 'city_filter': city_filter, 'map_data_json': json.dumps(map_data), 'map_limit_reached': len(households_list) > 3000, 'GOOGLE_MAPS_API_KEY': getattr(settings, 'GOOGLE_MAPS_API_KEY', ''), }""" ) with open(file_path, 'w') as f: f.write(content)