from django.core.management.base import BaseCommand import yaml import os from django.utils.text import slugify from tours.models import Tour class Command(BaseCommand): help = "Import tours from knowledge base" def handle(self, *args, **kwargs): input_file = "haikal_kb.yaml" try: with open(input_file, 'r', encoding='utf-8') as f: kb = yaml.safe_load(f) except Exception as e: self.stdout.write(self.style.ERROR(f"Error reading knowledge base file: {e}")) return workflows = kb.get("user_workflows", {}) tours_created = 0 for workflow_name, workflow in workflows.items(): slug = slugify(workflow_name) tour_file = f"{slug}_tour.json" tour, created = Tour.objects.update_or_create( slug=slug, defaults={ 'name': workflow_name, 'description': workflow.get('description', ''), 'tour_file': tour_file, 'is_active': True } ) if created: tours_created += 1 self.stdout.write(self.style.SUCCESS(f"Created tour: {workflow_name}")) else: self.stdout.write(self.style.SUCCESS(f"Updated tour: {workflow_name}")) self.stdout.write(self.style.SUCCESS(f"✅ Imported {tours_created} tours from knowledge base"))