""" Randomly assign templates to all domains Usage: uv run python scripts/assign_templates_to_domains.py """ import sys from pathlib import Path import random project_root = Path(__file__).parent.parent sys.path.insert(0, str(project_root)) from src.database.session import db_manager from src.database.models import SiteDeployment from src.templating.service import TemplateService def assign_templates(): """Randomly assign templates to all site deployments""" db_manager.initialize() session = db_manager.get_session() try: template_service = TemplateService() available_templates = template_service.get_available_templates() if not available_templates: print("Error: No templates found!") return print(f"Available templates: {', '.join(available_templates)}") sites = session.query(SiteDeployment).all() fqdn_sites = [s for s in sites if s.custom_hostname is not None] bcdn_sites = [s for s in sites if s.custom_hostname is None] print(f"\nTotal sites: {len(sites)}") print(f" FQDN domains: {len(fqdn_sites)}") print(f" b-cdn.net domains: {len(bcdn_sites)}") updated_fqdn = 0 updated_bcdn = 0 for site in fqdn_sites: if site.template_name == "basic": site.template_name = random.choice(available_templates) updated_fqdn += 1 for site in bcdn_sites: if site.template_name == "basic": site.template_name = random.choice(available_templates) updated_bcdn += 1 session.commit() print(f"\nUpdated templates:") print(f" FQDN domains: {updated_fqdn}") print(f" b-cdn.net domains: {updated_bcdn}") print(f" Total: {updated_fqdn + updated_bcdn}") except Exception as e: session.rollback() print(f"Error: {e}") raise finally: session.close() if __name__ == "__main__": assign_templates()