#!/usr/bin/env python3 """ Script to automatically apply pagination changes to dashboard.html """ import re import shutil from pathlib import Path def apply_pagination(): dashboard_path = Path("templates/dashboard.html") backup_path = Path("templates/dashboard.html.backup") pagination_js_path = Path("pagination-replacement.js") # Create backup print(f"Creating backup: {backup_path}") shutil.copy(dashboard_path, backup_path) # Read files with open(dashboard_path, 'r', encoding='utf-8') as f: html_content = f.read() with open(pagination_js_path, 'r', encoding='utf-8') as f: pagination_js = f.read() # Extract just the JavaScript code (skip comments) pagination_js = '\n'.join([line for line in pagination_js.split('\n') if not line.strip().startswith('//')]) # 1. Replace the old filter dropdown with new status filter old_filter_pattern = r'
\s*\s*\s*
' new_filter_html = '''
''' html_content = re.sub(old_filter_pattern, new_filter_html, html_content, flags=re.DOTALL) # 2. Add pagination controls after the table table_end_pattern = r'(\s*)' pagination_html = r'\1\n\n \n
' html_content = re.sub(table_end_pattern, pagination_html, html_content) # 3. Replace the JavaScript section # Find and replace from "// File Quality Analysis" to just before "function toggleFileSelection" js_pattern = r'(//\s*File Quality Analysis.*?)(\n\s*function toggleFileSelection\()' replacement = r' // File Quality Analysis with Pagination\n' + pagination_js + r'\2' html_content = re.sub(js_pattern, replacement, html_content, flags=re.DOTALL) # 4. Remove infinite scroll event listeners scroll_listener_pattern = r'\s*//\s*Add infinite scroll.*?window\.addEventListener\(\'scroll\', handleScroll\);' html_content = re.sub(scroll_listener_pattern, '', html_content, flags=re.DOTALL) # 5. Update applyFilter to reset currentPage apply_filter_pattern = r'(function applyFilter\(filterType\) \{[^}]*currentAttributeFilter = filterType[^;]*;)' apply_filter_replacement = r'\1\n currentPage = 1; // Reset to first page when changing filter' html_content = re.sub(apply_filter_pattern, apply_filter_replacement, html_content) # Write the modified content with open(dashboard_path, 'w', encoding='utf-8') as f: f.write(html_content) print(f"\nSuccessfully applied pagination changes!") print(f"Backup saved to: {backup_path}") print(f"\nChanges made:") print(" 1. Replaced 'Filter' dropdown with 'Status' dropdown") print(" 2. Added pagination controls container") print(" 3. Replaced infinite scroll with pagination JavaScript") print(" 4. Removed scroll event listeners") print(" 5. Updated applyFilter to reset page on filter change") print(f"\nIf anything goes wrong, restore from backup:") print(f" cp {backup_path} {dashboard_path}") if __name__ == '__main__': try: apply_pagination() except Exception as e: print(f"\nError: {e}") print("\nPlease check:") print(" - templates/dashboard.html exists") print(" - pagination-replacement.js exists") print(" - You have write permissions")