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

46 lines
1.6 KiB
Python

import sys
import re
def replace_func(content, func_name, new_content):
r1 = r'(^(@.*\n)*def '
r2 = func_name
r3 = r'\(.*\?\):.*\?)(\n+(?=@|def |class )|\Z)'
regex = r1 + r2 + r3
pattern = re.compile(regex, re.MULTILINE | re.DOTALL)
if pattern.search(content):
return pattern.sub(new_content.strip() + '\n\n', content)
r1 = r'(^def '
regex = r1 + r2 + r3
pattern = re.compile(regex, re.MULTILINE | re.DOTALL)
if pattern.search(content):
return pattern.sub(new_content.strip() + '\n\n', content)
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/filter_helper.py', 'r') as f:
helper = f.read().strip()
if 'def get_filtered_voter_queryset' not in content:
content = content.replace('def voter_advanced_search', helper + '\n\n' + 'def voter_advanced_search', 1)
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()
content = replace_func(content, 'voter_advanced_search', voter_advanced_search_new)
content = replace_func(content, 'bulk_send_sms', bulk_send_sms_new)
content = replace_func(content, 'voter_bulk_send_email', voter_bulk_send_email_new)
content = replace_func(content, 'export_voters_csv', export_voters_csv_new)
with open(file_path, 'w') as f:
f.write(content)