import os def patch_views(): with open('core/views.py', 'r') as f: lines = f.readlines() # Find bulk_send_sms start_idx = -1 for i, line in enumerate(lines): if 'def bulk_send_sms(request):' in line: start_idx = i break if start_idx != -1: # Find the end of the function (next function or end of file) end_idx = -1 for i in range(start_idx + 1, len(lines)): if lines[i].startswith('def ') or lines[i].startswith('@role_required'): end_idx = i break if end_idx != -1: new_func = [ 'def bulk_send_sms(request):\n', ' """\n', ' Sends bulk SMS to selected voters using Twilio API in a background thread.\n', ' """\n', " if request.method != 'POST':\n", " return redirect('voter_advanced_search')\n", ' \n', ' selected_tenant_id = request.session.get("tenant_id")\n', ' if not selected_tenant_id:\n', ' messages.warning(request, "Please select a campaign first.")\n', ' return redirect("index")\n', ' \n', ' tenant = get_object_or_404(Tenant, id=selected_tenant_id)\n', " message_body = request.POST.get('message_body')\n", '\n', ' if not message_body:\n', ' messages.error(request, "Message body cannot be empty.")\n', " return redirect('voter_advanced_search')\n", ' \n', " select_all_results = request.POST.get('select_all_results') == 'true'\n", " voter_ids = request.POST.getlist('selected_voters')\n", ' \n', ' search_filters = {}\n', ' if select_all_results:\n', ' for key, value in request.POST.items():\n', ' if key.startswith("filter_") and value:\n', ' search_filters[key.replace("filter_", "")] = value\n', '\n', " start_bulk_sms_task(tenant, message_body, voter_ids, select_all_results, search_filters, object_type='voter')\n", ' \n', ' messages.success(request, "Bulk SMS process started in the background. You can continue working while it processes.")\n', " return redirect('voter_advanced_search')\n", '\n' ] lines[start_idx:end_idx] = new_func # Find volunteer_bulk_send_sms start_idx = -1 for i, line in enumerate(lines): if 'def volunteer_bulk_send_sms(request):' in line: start_idx = i break if start_idx != -1: end_idx = -1 for i in range(start_idx + 1, len(lines)): if lines[i].startswith('def ') or lines[i].startswith('@role_required'): end_idx = i break if end_idx != -1: new_func = [ 'def volunteer_bulk_send_sms(request):\n', ' """\n', ' Sends bulk SMS to selected volunteers using Twilio API in a background thread.\n', ' """\n', " if request.method != 'POST':\n", " return redirect('volunteer_list')\n", ' \n', ' selected_tenant_id = request.session.get("tenant_id")\n', ' if not selected_tenant_id:\n', ' messages.warning(request, "Please select a campaign first.")\n', ' return redirect("index")\n', ' \n', ' tenant = get_object_or_404(Tenant, id=selected_tenant_id)\n', " message_body = request.POST.get('message_body')\n", '\n', ' if not message_body:\n', ' messages.error(request, "Message body cannot be empty.")\n', " return redirect('volunteer_list')\n", ' \n', " volunteer_ids = request.POST.getlist('selected_volunteers')\n", ' \n', " start_bulk_sms_task(tenant, message_body, volunteer_ids, False, None, object_type='volunteer')\n", ' \n', ' messages.success(request, "Bulk SMS process started in the background.")\n', " return redirect('volunteer_list')\n", '\n' ] lines[start_idx:end_idx] = new_func with open('core/views.py', 'w') as f: f.writelines(lines) if __name__ == '__main__': patch_views()