agdar/update_patient_templates.py
2025-11-02 14:35:35 +03:00

174 lines
6.9 KiB
Python

#!/usr/bin/env python3
"""
Script to update all templates to use language-aware patient names and LTR phone numbers.
"""
import os
import re
from pathlib import Path
def update_template_file(filepath):
"""Update a single template file with the new template tags."""
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
original_content = content
changes_made = []
# Check if file already loads patient_tags
has_patient_tags = '{% load patient_tags %}' in content or "{% load patient_tags %}" in content
# Check if file needs patient_tags (has patient references)
needs_patient_tags = (
'patient.full_name_en' in content or
'patient.first_name_en' in content or
'patient.last_name_en' in content or
'patient.first_name' in content or
'patient.last_name' in content or
'patient.phone' in content or
'patient.phone_number' in content
)
if not needs_patient_tags:
return False, []
# Add {% load patient_tags %} after {% load i18n %} if not present
if not has_patient_tags:
# Find {% load i18n ... %} and add patient_tags to it
load_i18n_pattern = r'({% load i18n[^%]*%})'
match = re.search(load_i18n_pattern, content)
if match:
# Add patient_tags to existing load statement
old_load = match.group(1)
new_load = old_load.replace('%}', ' patient_tags %}')
content = content.replace(old_load, new_load, 1)
changes_made.append("Added patient_tags to load statement")
else:
# Add new load statement at the beginning
if content.startswith('{% extends'):
# Add after extends
lines = content.split('\n')
for i, line in enumerate(lines):
if '{% extends' in line:
lines.insert(i + 1, '{% load patient_tags %}')
break
content = '\n'.join(lines)
changes_made.append("Added {% load patient_tags %} after extends")
# Replace patient.full_name_en with {% patient_name patient %}
pattern1 = r'\{\{\s*patient\.full_name_en\s*\}\}'
if re.search(pattern1, content):
content = re.sub(pattern1, '{% patient_name patient %}', content)
changes_made.append("Replaced patient.full_name_en")
# Replace variations like appointment.patient.full_name_en, encounter.patient.full_name_en, etc.
pattern2 = r'\{\{\s*(\w+)\.patient\.full_name_en\s*\}\}'
matches = re.findall(pattern2, content)
if matches:
content = re.sub(pattern2, r'{% patient_name \1.patient %}', content)
changes_made.append(f"Replaced {len(set(matches))} variations of *.patient.full_name_en")
# Replace {{ patient.first_name_en }} {{ patient.last_name_en }} with {% patient_name patient %}
pattern3 = r'\{\{\s*patient\.first_name_en\s*\}\}\s*\{\{\s*patient\.last_name_en\s*\}\}'
if re.search(pattern3, content):
content = re.sub(pattern3, '{% patient_name patient %}', content)
changes_made.append("Replaced patient.first_name_en patient.last_name_en combination")
# Replace variations like {{ appointment.patient.first_name_en }} {{ appointment.patient.last_name_en }}
pattern4 = r'\{\{\s*(\w+)\.patient\.first_name_en\s*\}\}\s*\{\{\s*\1\.patient\.last_name_en\s*\}\}'
matches = re.findall(pattern4, content)
if matches:
content = re.sub(pattern4, r'{% patient_name \1.patient %}', content)
changes_made.append(f"Replaced {len(set(matches))} variations of first_name_en last_name_en")
# Replace {{ patient.first_name }} {{ patient.last_name }} (without _en)
pattern5 = r'\{\{\s*patient\.first_name\s*\}\}\s*\{\{\s*patient\.last_name\s*\}\}'
if re.search(pattern5, content):
content = re.sub(pattern5, '{% patient_name patient %}', content)
changes_made.append("Replaced patient.first_name patient.last_name combination")
# Replace variations like {{ encounter.patient.first_name }} {{ encounter.patient.last_name }}
pattern6 = r'\{\{\s*(\w+)\.patient\.first_name\s*\}\}\s*\{\{\s*\1\.patient\.last_name\s*\}\}'
matches = re.findall(pattern6, content)
if matches:
content = re.sub(pattern6, r'{% patient_name \1.patient %}', content)
changes_made.append(f"Replaced {len(set(matches))} variations of first_name last_name")
# Replace phone numbers with LTR filter
# Pattern: {{ patient.phone }}
pattern7 = r'\{\{\s*patient\.phone\s*\}\}'
if re.search(pattern7, content):
content = re.sub(pattern7, '{{ patient.phone|ltr }}', content)
changes_made.append("Added LTR filter to patient.phone")
# Pattern: {{ patient.phone_number }}
pattern8 = r'\{\{\s*patient\.phone_number\s*\}\}'
if re.search(pattern8, content):
content = re.sub(pattern8, '{{ patient.phone_number|ltr }}', content)
changes_made.append("Added LTR filter to patient.phone_number")
# Pattern: {{ *.patient.phone }}
pattern9 = r'\{\{\s*(\w+)\.patient\.phone\s*\}\}'
matches = re.findall(pattern9, content)
if matches:
content = re.sub(pattern9, r'{{ \1.patient.phone|ltr }}', content)
changes_made.append(f"Added LTR filter to {len(set(matches))} variations of *.patient.phone")
# Pattern: {{ *.patient.phone_number }}
pattern10 = r'\{\{\s*(\w+)\.patient\.phone_number\s*\}\}'
matches = re.findall(pattern10, content)
if matches:
content = re.sub(pattern10, r'{{ \1.patient.phone_number|ltr }}', content)
changes_made.append(f"Added LTR filter to {len(set(matches))} variations of *.patient.phone_number")
# Only write if changes were made
if content != original_content:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
return True, changes_made
return False, []
def main():
"""Main function to process all template files."""
templates_dir = Path('templates')
if not templates_dir.exists():
print("Error: templates directory not found")
return
# Find all HTML files
html_files = list(templates_dir.rglob('*.html'))
print(f"Found {len(html_files)} HTML template files")
print("=" * 80)
updated_count = 0
skipped_count = 0
for filepath in html_files:
updated, changes = update_template_file(filepath)
if updated:
updated_count += 1
print(f"\n✓ Updated: {filepath}")
for change in changes:
print(f" - {change}")
else:
skipped_count += 1
print("\n" + "=" * 80)
print(f"Summary:")
print(f" Updated: {updated_count} files")
print(f" Skipped: {skipped_count} files")
print(f" Total: {len(html_files)} files")
if __name__ == '__main__':
main()