agdar/fix_all_modal_confirmations.py
Marwan Alwali 2f1681b18c update
2025-11-11 13:44:48 +03:00

99 lines
3.3 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
Script to properly fix all signing confirmation modals.
Removes inline onsubmit and adds proper JavaScript event handlers.
"""
import os
import re
TEMPLATES = {
'ot/templates/ot/session_detail.html': 'ot:session_sign',
'ot/templates/ot/consult_detail.html': 'ot:consult_sign',
'slp/templates/slp/consultation_detail.html': 'slp:consult_sign',
'slp/templates/slp/assessment_detail.html': 'slp:assessment_sign',
'slp/templates/slp/intervention_detail.html': 'slp:intervention_sign',
'slp/templates/slp/progress_detail.html': 'slp:progress_report_sign',
'medical/templates/medical/consultation_detail.html': 'medical:consultation_sign',
'medical/templates/medical/followup_detail.html': 'medical:followup_sign',
'nursing/templates/nursing/encounter_detail.html': 'nursing:encounter_sign',
}
def fix_template(template_path, sign_url):
"""Fix a single template."""
if not os.path.exists(template_path):
print(f"⚠️ {template_path} not found")
return False
with open(template_path, 'r') as f:
content = f.read()
# Check if already has the JS block
if 'document.getElementById(\'signForm\')' in content:
print(f" {template_path} already fixed")
return False
# Step 1: Find and replace the form tag - remove onsubmit, add id
form_pattern = r'<form method="post" action="{%\s*url\s+\'' + re.escape(sign_url) + r'\'[^}]*%}"[^>]*>'
def replace_form(match):
return match.group(0).split('>')[0].split('onsubmit')[0].rstrip() + ' id="signForm">'
content = re.sub(form_pattern, replace_form, content)
# Step 2: Add JavaScript block before {% endblock %} if not already there
if '{% block js %}' not in content:
# Find the last {% endblock %} and add JS before it
js_block = '''
{% block js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
const signForm = document.getElementById('signForm');
if (signForm) {
signForm.addEventListener('submit', function(e) {
e.preventDefault();
showConfirmModal(
'{% trans "Are you sure you want to sign this document? Once signed, no further editing will be allowed. This action cannot be undone." %}',
'{% trans "Confirm Signature" %}'
).then((confirmed) => {
if (confirmed) {
this.submit();
}
});
});
}
});
</script>
{% endblock %}'''
# Insert before the last {% endblock %}
last_endblock = content.rfind('{% endblock %}')
if last_endblock != -1:
content = content[:last_endblock] + js_block + '\n'
with open(template_path, 'w') as f:
f.write(content)
print(f"✅ Fixed {template_path}")
return True
def main():
"""Main execution."""
print("=" * 70)
print("Fixing All Modal Confirmations")
print("=" * 70)
fixed = 0
for template_path, sign_url in TEMPLATES.items():
if fix_template(template_path, sign_url):
fixed += 1
print("\n" + "=" * 70)
print(f"✅ Fixed {fixed} templates")
print("=" * 70)
print("\nAll signing confirmations now use proper modal implementation!")
if __name__ == '__main__':
main()