#!/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'' if pattern1 in content: replacement = f'''{{%if not {object_name}.signed_by %}} {{% trans "Edit" %}} {{% else %}} {{% endif %}}''' content = content.replace( f'', replacement ) # Pattern 2: Edit button with icon already pattern2 = rf'\s*.*?)', f'''{{%if not {object_name}.signed_by %}} \\1 {{% else %}} {{% 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()