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

34 lines
1.0 KiB
Python

import os
def patch_voter_search():
with open('core/views.py', 'r') as f:
lines = f.readlines()
# Add BulkTask to imports if not already there
for i, line in enumerate(lines):
if 'from .models import' in line and 'BulkTask' not in line:
lines[i] = line.replace('ScheduledCall', 'ScheduledCall, BulkTask')
break
# Find voter_advanced_search
start_idx = -1
for i, line in enumerate(lines):
if 'def voter_advanced_search(request):' in line:
start_idx = i
break
if start_idx != -1:
# Find the context dict
for i in range(start_idx, start_idx + 100):
if 'context = {' in lines[i]:
# Add bulk_tasks to context
lines.insert(i + 1, " 'bulk_tasks': BulkTask.objects.filter(tenant=tenant).order_by('-created_at')[:5],\n")
break
with open('core/views.py', 'w') as f:
f.writelines(lines)
if __name__ == '__main__':
patch_voter_search()