38086-vm/apply_patch.py
Flatlogic Bot 9dfa03d69c adding vat
2026-02-05 13:09:11 +00:00

59 lines
1.9 KiB
Python

import re
with open('core/views.py', 'r') as f:
content = f.read()
with open('core/patch_views_vat.py', 'r') as f:
new_func = f.read()
# Regex to find the function definition
# It starts with @csrf_exempt\ndef create_sale_api(request):
# And ends before the next function definition (which likely starts with @ or def)
pattern = r"@csrf_exempt\s+def create_sale_api(request):.*?return JsonResponse({'success': False, 'error': 'Invalid request'}, status=405)"
# Note: The pattern needs to match the indentation and multiline content.
# Since regex for code blocks is tricky, I will use a simpler approach:
# 1. Read the file lines.
# 2. Find start line of create_sale_api.
# 3. Find the end line (start of next function or end of file).
# 4. Replace lines.
lines = content.splitlines()
start_index = -1
end_index = -1
for i, line in enumerate(lines):
if line.strip() == "def create_sale_api(request):":
# Check if previous line is decorator
if i > 0 and lines[i-1].strip() == "@csrf_exempt":
start_index = i - 1
else:
start_index = i
break
if start_index != -1:
# Find the next function or end
# We look for next line starting with 'def ' or '@' at top level
for i in range(start_index + 1, len(lines)):
if lines[i].startswith("def ") or lines[i].startswith("@"):
end_index = i
break
if end_index == -1:
end_index = len(lines)
# Replace
new_lines = new_func.splitlines()
# Ensure new lines have correct indentation if needed (but views.py is top level mostly)
# We need to preserve the imports and structure.
# The new_func is complete.
final_lines = lines[:start_index] + new_lines + lines[end_index:]
with open('core/views.py', 'w') as f:
f.write('\n'.join(final_lines))
print("Successfully patched create_sale_api")
else:
print("Could not find create_sale_api function")