37769-vm/patch_views_v3.py
2026-05-30 08:01:02 +00:00

45 lines
1.8 KiB
Python

import sys
import os
def replace_function(content, func_name, new_func_content):
import re
# Find the function definition
pattern = re.compile(r'^def ' + func_name + r'\(.*\):.*?(?=^def |^class |$)', re.MULTILINE | re.DOTALL)
if not pattern.search(content):
# Try with decorator
pattern = re.compile(r'^@.*?\ndef ' + func_name + r'\(.*\):.*?(?=^def |^class |$)', re.MULTILINE | re.DOTALL)
if pattern.search(content):
return pattern.sub(new_func_content + '\n', content)
else:
print(f"Warning: Could not find function {func_name}")
return content
file_path = 'core/views.py'
with open(file_path, 'r') as f:
content = f.read()
with open('core/views_new.py', 'r') as f:
voter_advanced_search_new = f.read().strip()
with open('core/bulk_sms_new.py', 'r') as f:
bulk_send_sms_new = f.read().strip()
with open('core/bulk_email_new.py', 'r') as f:
voter_bulk_send_email_new = f.read().strip()
with open('core/export_new.py', 'r') as f:
export_voters_csv_new = f.read().strip()
# Decorators for replacement
voter_advanced_search_new_with_decorator = "@role_required(['admin', 'campaign_manager', 'campaign_staff', 'system_admin', 'campaign_admin'], permission='core.view_voter')\n" + voter_advanced_search_new.split('\n', 1)[1] if voter_advanced_search_new.startswith('@') else voter_advanced_search_new
# Actually, my views_new.py already has the decorator.
content = replace_function(content, 'voter_advanced_search', voter_advanced_search_new)
content = replace_function(content, 'bulk_send_sms', bulk_send_sms_new)
content = replace_function(content, 'voter_bulk_send_email', voter_bulk_send_email_new)
content = replace_function(content, 'export_voters_csv', export_voters_csv_new)
with open(file_path, 'w') as f:
f.write(content)