from flask import Blueprint, render_template, request, jsonify, session, current_app import sqlite3 import json import time import os from config import MAIN_DB, THEMES_DIR from utils.theme_engine import scan_available_themes, get_theme_config, render_gkachele_template from utils.auth_decorators import login_required customizer_bp = Blueprint('customizer', __name__) @customizer_bp.route('/api/themes') def list_themes(): """Listar todos los templates disponibles""" from utils.theme_engine import get_themes_by_rubro rubro = request.args.get('rubro', None) themes = get_themes_by_rubro(rubro) if rubro else scan_available_themes() return jsonify({'success': True, 'themes': themes, 'total': len(themes)}) @customizer_bp.route('/customizer/') def customizer_view(site_id): """Customizer: Sidebar + Preview""" conn = sqlite3.connect(MAIN_DB) c = conn.cursor() c.execute('SELECT user_id, slug, theme, content_json FROM sites WHERE id = ?', (site_id,)) site = c.fetchone() conn.close() if not site: return "Sitio no encontrado", 404 if 'user_id' in session and site[0] != session['user_id']: return "No autorizado", 403 content = json.loads(site[3]) if site[3] else {} theme = site[2] theme_template = None theme_path = os.path.join(THEMES_DIR, theme, 'template.html') if os.path.exists(theme_path): with open(theme_path, 'r', encoding='utf-8') as f: theme_template = f.read() theme_config = get_theme_config(theme) available_themes = scan_available_themes() return render_template('customizer.html', site_id=site_id, slug=site[1], theme=theme, content=content, theme_template=theme_template, theme_config=theme_config, available_themes=available_themes) @customizer_bp.route('/api/customizer/save', methods=['POST']) def save_customizer(): data = request.get_json() site_id = data.get('site_id') content = data.get('content') new_theme = data.get('theme') conn = sqlite3.connect(MAIN_DB) c = conn.cursor() if new_theme: c.execute('UPDATE sites SET theme = ? WHERE id = ?', (new_theme, site_id)) c.execute('UPDATE sites SET content_json = ? WHERE id = ?', (json.dumps(content), site_id)) conn.commit() conn.close() return jsonify({'success': True}) @customizer_bp.route('/api/customizer/add-block', methods=['POST']) def add_block(): data = request.get_json() site_id = data.get('site_id') block_type = data.get('block_type') conn = sqlite3.connect(MAIN_DB) c = conn.cursor() c.execute('SELECT content_json FROM sites WHERE id = ?', (site_id,)) result = c.fetchone() content = json.loads(result[0]) if result[0] else {} if 'blocks' not in content: content['blocks'] = [] new_block = { 'id': f'block_{int(time.time() * 1000)}', 'type': block_type, 'content': data.get('content', {}), 'order': len(content['blocks']) } content['blocks'].append(new_block) c.execute('UPDATE sites SET content_json = ? WHERE id = ?', (json.dumps(content), site_id)) conn.commit() conn.close() return jsonify({'success': True, 'block': new_block}) @customizer_bp.route('/api/customizer/preview-frame/') def preview_frame(site_id): conn = sqlite3.connect(MAIN_DB) c = conn.cursor() c.execute('SELECT theme, content_json, user_id FROM sites WHERE id = ?', (site_id,)) site = c.fetchone() conn.close() if not site: return "Sitio no encontrado", 404 try: return render_gkachele_template(site[0], json.loads(site[1]), site_id, site[2]) except Exception as e: return f"Error renderizando preview: {e}", 500