fix(content): implement state refresh on update to prevent data loss on tab switch

This commit is contained in:
2026-01-20 15:35:33 +00:00
parent eacd572124
commit c2fc5efc02
4 changed files with 336 additions and 144 deletions

View File

@@ -1,16 +1,25 @@
import sqlite3
import json
import os
import logging
from datetime import datetime
# Logging setup (rely on orchestrator config if imported, or basic config)
if not logging.getLogger().handlers:
logging.basicConfig(level=logging.INFO)
DB_PATH = os.environ.get('DB_PATH', 'content_engine.db')
GTM_DB_PATH = os.environ.get('GTM_DB_PATH', 'gtm_projects.db')
def get_db_connection(path=DB_PATH):
conn = sqlite3.connect(path)
conn.row_factory = sqlite3.Row
return conn
try:
conn = sqlite3.connect(path)
conn.row_factory = sqlite3.Row
return conn
except Exception as e:
logging.error(f"Failed to connect to DB at {path}: {e}")
raise
def init_db():
conn = get_db_connection()
@@ -51,16 +60,17 @@ def init_db():
conn.commit()
conn.close()
logging.info(f"Database initialized at {DB_PATH}")
logging.info(f"Database initialized/verified at {DB_PATH}")
# --- GTM READ ACCESS ---
def get_all_gtm_projects():
"""Lists all available GTM projects."""
if not os.path.exists(GTM_DB_PATH):
logging.warning(f"GTM DB not found at {GTM_DB_PATH}")
logging.warning(f"GTM DB NOT FOUND at {GTM_DB_PATH}. Cannot list projects.")
return []
logging.info(f"Connecting to GTM DB at {GTM_DB_PATH}")
conn = get_db_connection(GTM_DB_PATH)
try:
query = """
@@ -73,16 +83,23 @@ def get_all_gtm_projects():
ORDER BY updated_at DESC
"""
projects = [dict(row) for row in conn.execute(query).fetchall()]
logging.info(f"Retrieved {len(projects)} GTM projects.")
return projects
finally:
conn.close()
def get_gtm_project_data(gtm_id):
"""Retrieves full data for a GTM project."""
logging.info(f"Fetching GTM data for ID: {gtm_id}")
conn = get_db_connection(GTM_DB_PATH)
try:
row = conn.execute("SELECT data FROM gtm_projects WHERE id = ?", (gtm_id,)).fetchone()
return json.loads(row['data']) if row else None
if row:
logging.info("GTM Data found.")
return json.loads(row['data'])
else:
logging.warning("GTM Data NOT found.")
return None
finally:
conn.close()
@@ -90,14 +107,22 @@ def get_gtm_project_data(gtm_id):
def import_gtm_project(gtm_id):
"""Imports a GTM project as a new Content Engine project."""
logging.info(f"Importing GTM ID: {gtm_id}")
gtm_data = get_gtm_project_data(gtm_id)
if not gtm_data:
logging.error("Import failed: No data returned from GTM DB.")
return None
name = gtm_data.get('name', 'Imported Project')
# Phase 1 has the category
phase1 = gtm_data.get('phases', {}).get('phase1_result', {})
if isinstance(phase1, str): phase1 = json.loads(phase1)
if isinstance(phase1, str):
try:
phase1 = json.loads(phase1)
except:
logging.warning("Could not parse Phase 1 JSON string.")
phase1 = {}
category = phase1.get('category', 'Unknown')
conn = get_db_connection()
@@ -109,6 +134,7 @@ def import_gtm_project(gtm_id):
project_id = cursor.lastrowid
conn.commit()
conn.close()
logging.info(f"Project imported successfully into Content DB. New ID: {project_id}")
return {"id": project_id, "name": name, "category": category}
def get_all_content_projects():
@@ -176,4 +202,4 @@ def get_project_assets(project_id):
return assets
if __name__ == "__main__":
init_db()
init_db()