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

173 lines
6.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 update all detail templates with signing edit prevention UI.
Updates templates for OT, SLP, Medical, and Nursing modules.
"""
import os
import re
# Template files to update
TEMPLATES_TO_UPDATE = {
'ot': [
'ot/templates/ot/session_detail.html',
'ot/templates/ot/consult_detail.html',
],
'slp': [
'slp/templates/slp/consultation_detail.html',
'slp/templates/slp/assessment_detail.html',
'slp/templates/slp/intervention_detail.html',
'slp/templates/slp/progress_detail.html',
],
'medical': [
'medical/templates/medical/consultation_detail.html',
'medical/templates/medical/followup_detail.html',
],
'nursing': [
'nursing/templates/nursing/encounter_detail.html',
]
}
def update_template_edit_button(content, object_name, update_url_pattern):
"""Update the Edit button to check if document is signed."""
# Pattern 1: Simple edit button
pattern1 = rf'<a href="{{% url \'{update_url_pattern}\' {object_name}\.pk %}}" class="btn btn-primary">'
if pattern1 in content:
replacement = f'''{{%if not {object_name}.signed_by %}}
<a href="{{% url '{update_url_pattern}' {object_name}.pk %}}" class="btn btn-primary">
<i class="fas fa-edit me-2"></i>{{% trans "Edit" %}}
</a>
{{% else %}}
<button class="btn btn-secondary" disabled title="{{% trans 'Cannot edit signed document' %}}">
<i class="fas fa-lock me-2"></i>{{% trans "Signed - No Editing" %}}
</button>
{{% endif %}}'''
content = content.replace(
f'<a href="{{% url \'{update_url_pattern}\' {object_name}.pk %}}" class="btn btn-primary">',
replacement
)
# Pattern 2: Edit button with icon already
pattern2 = rf'<a href="{{% url \'{update_url_pattern}\' {object_name}\.pk %}}" class="btn btn-primary">\s*<i class="fas fa-edit'
if re.search(pattern2, content):
# Find and replace the entire button section
content = re.sub(
rf'(<a href="{{% url \'{update_url_pattern}\' {object_name}\.pk %}}" class="btn btn-primary">.*?</a>)',
f'''{{%if not {object_name}.signed_by %}}
\\1
{{% else %}}
<button class="btn btn-secondary" disabled title="{{% trans 'Cannot edit signed document' %}}">
<i class="fas fa-lock me-2"></i>{{% trans "Signed - No Editing" %}}
</button>
{{% endif %}}''',
content,
flags=re.DOTALL
)
return content
def update_template_sign_confirmation(content):
"""Update the sign form confirmation message."""
# Pattern: onsubmit with confirm
old_confirm = 'Are you sure you want to sign this document? This action cannot be undone.'
new_confirm = 'Are you sure you want to sign this document? Once signed, no further editing will be allowed. This action cannot be undone.'
content = content.replace(old_confirm, new_confirm)
# Also update any variations
content = content.replace(
'Are you sure you want to sign this',
'Are you sure you want to sign this document? Once signed, no further editing will be allowed. This action cannot be undone'
)
return content
def update_template_file(template_path):
"""Update a single template file."""
if not os.path.exists(template_path):
print(f"⚠️ {template_path} not found - skipping")
return False
with open(template_path, 'r') as f:
content = f.read()
# Determine object name and URL pattern from path
if 'session_detail' in template_path:
object_name = 'session'
if 'ot' in template_path:
update_url = 'ot:session_update'
elif 'slp' in template_path:
update_url = 'slp:session_update'
elif 'consult_detail' in template_path or 'consultation_detail' in template_path:
if 'ot' in template_path:
object_name = 'consult'
update_url = 'ot:consult_update'
elif 'slp' in template_path:
object_name = 'consultation'
update_url = 'slp:consultation_update'
elif 'medical' in template_path:
object_name = 'consultation'
update_url = 'medical:consultation_update'
elif 'assessment_detail' in template_path:
object_name = 'assessment'
update_url = 'slp:assessment_update'
elif 'intervention_detail' in template_path:
object_name = 'intervention'
update_url = 'slp:intervention_update'
elif 'progress_detail' in template_path:
object_name = 'report'
update_url = 'slp:progress_report_update'
elif 'followup_detail' in template_path:
object_name = 'followup'
update_url = 'medical:followup_update'
elif 'encounter_detail' in template_path:
object_name = 'encounter'
update_url = 'nursing:encounter_update'
else:
print(f"⚠️ Could not determine object name for {template_path}")
return False
# Apply updates
original_content = content
content = update_template_edit_button(content, object_name, update_url)
content = update_template_sign_confirmation(content)
# Only write if changes were made
if content != original_content:
with open(template_path, 'w') as f:
f.write(content)
print(f"✅ Updated {template_path}")
return True
else:
print(f" No changes needed for {template_path}")
return False
def main():
"""Main execution."""
print("=" * 70)
print("Updating Detail Templates with Signing Edit Prevention UI")
print("=" * 70)
total_updated = 0
total_skipped = 0
for app_name, templates in TEMPLATES_TO_UPDATE.items():
print(f"\n📝 Processing {app_name.upper()} templates...")
for template_path in templates:
if update_template_file(template_path):
total_updated += 1
else:
total_skipped += 1
print("\n" + "=" * 70)
print(f"✅ Template updates complete!")
print(f" Updated: {total_updated} files")
print(f" Skipped: {total_skipped} files")
print("=" * 70)
if __name__ == '__main__':
main()