Files
encoderPro/apply-pagination.py
2026-01-24 17:43:28 -05:00

94 lines
4.5 KiB
Python

#!/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'<div style="display: flex; gap: 8px; align-items: center;">\s*<label style="color: #94a3b8; font-weight: 500;">Filter:</label>\s*<select id="qualityFilter"[^>]*>.*?</select>\s*</div>'
new_filter_html = '''<div style="display: flex; gap: 15px; align-items: center;">
<div style="display: flex; gap: 8px; align-items: center;">
<label style="color: #94a3b8; font-weight: 500;">Status:</label>
<select id="statusFilter" onchange="changeStatusFilter(this.value)" style="padding: 10px; border-radius: 6px; background: #1e293b; color: #e2e8f0; border: 1px solid #334155;">
<option value="all">All States</option>
<option value="discovered">Discovered</option>
<option value="pending">Pending</option>
<option value="processing">Processing</option>
<option value="completed">Completed</option>
<option value="failed">Failed</option>
<option value="skipped">Skipped</option>
</select>
</div>
</div>'''
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'(</table>\s*</div>)'
pagination_html = r'\1\n\n <!-- Pagination Controls -->\n <div id="paginationControls"></div>'
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")