Major refactor: deduplicate routes, sync, JS, and fix bugs
- Extract 8 common route patterns into factory functions in routes/shared.py (favourite, upload, replace cover, save defaults, clone, save JSON, get missing, clear covers) — removes ~1,100 lines across 9 route files - Extract generic _sync_category() in sync.py — 7 sync functions become one-liner wrappers, removing ~350 lines - Extract shared detail page JS into static/js/detail-common.js — all 9 detail templates now call initDetailPage() with minimal config - Extract layout inline JS into static/js/layout-utils.js (~185 lines) - Extract library toolbar JS into static/js/library-toolbar.js - Fix finalize missing-image bug: raise RuntimeError instead of logging warning so job is marked failed - Fix missing scheduler default in _default_checkpoint_data() - Fix N+1 query in Character.get_available_outfits() with batch IN query - Convert all print() to logger across services and routes - Add missing tags display to styles, scenes, detailers, checkpoints detail - Update delete buttons to use trash.png icon with solid red background - Update CLAUDE.md to reflect new architecture Net reduction: ~1,600 lines Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import os
|
||||
import logging
|
||||
import subprocess
|
||||
|
||||
logger = logging.getLogger('gaze')
|
||||
|
||||
# Path to the MCP docker-compose projects, relative to the main app file.
|
||||
MCP_TOOLS_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'tools')
|
||||
MCP_COMPOSE_DIR = os.path.join(MCP_TOOLS_DIR, 'danbooru-mcp')
|
||||
@@ -19,28 +22,28 @@ def _ensure_mcp_repo():
|
||||
os.makedirs(MCP_TOOLS_DIR, exist_ok=True)
|
||||
try:
|
||||
if not os.path.isdir(MCP_COMPOSE_DIR):
|
||||
print(f'Cloning danbooru-mcp from {MCP_REPO_URL} …')
|
||||
logger.info('Cloning danbooru-mcp from %s …', MCP_REPO_URL)
|
||||
subprocess.run(
|
||||
['git', 'clone', MCP_REPO_URL, MCP_COMPOSE_DIR],
|
||||
timeout=120, check=True,
|
||||
)
|
||||
print('danbooru-mcp cloned successfully.')
|
||||
logger.info('danbooru-mcp cloned successfully.')
|
||||
else:
|
||||
print('Updating danbooru-mcp via git pull …')
|
||||
logger.info('Updating danbooru-mcp via git pull …')
|
||||
subprocess.run(
|
||||
['git', 'pull'],
|
||||
cwd=MCP_COMPOSE_DIR,
|
||||
timeout=60, check=True,
|
||||
)
|
||||
print('danbooru-mcp updated.')
|
||||
logger.info('danbooru-mcp updated.')
|
||||
except FileNotFoundError:
|
||||
print('WARNING: git not found on PATH — danbooru-mcp repo will not be cloned/updated.')
|
||||
logger.warning('git not found on PATH — danbooru-mcp repo will not be cloned/updated.')
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f'WARNING: git operation failed for danbooru-mcp: {e}')
|
||||
logger.warning('git operation failed for danbooru-mcp: %s', e)
|
||||
except subprocess.TimeoutExpired:
|
||||
print('WARNING: git timed out while cloning/updating danbooru-mcp.')
|
||||
logger.warning('git timed out while cloning/updating danbooru-mcp.')
|
||||
except Exception as e:
|
||||
print(f'WARNING: Could not clone/update danbooru-mcp repo: {e}')
|
||||
logger.warning('Could not clone/update danbooru-mcp repo: %s', e)
|
||||
|
||||
|
||||
def ensure_mcp_server_running():
|
||||
@@ -55,7 +58,7 @@ def ensure_mcp_server_running():
|
||||
danbooru-mcp service is managed by compose instead).
|
||||
"""
|
||||
if os.environ.get('SKIP_MCP_AUTOSTART', '').lower() == 'true':
|
||||
print('SKIP_MCP_AUTOSTART set — skipping danbooru-mcp auto-start.')
|
||||
logger.info('SKIP_MCP_AUTOSTART set — skipping danbooru-mcp auto-start.')
|
||||
return
|
||||
_ensure_mcp_repo()
|
||||
try:
|
||||
@@ -64,22 +67,22 @@ def ensure_mcp_server_running():
|
||||
capture_output=True, text=True, timeout=10,
|
||||
)
|
||||
if 'danbooru-mcp' in result.stdout:
|
||||
print('danbooru-mcp container already running.')
|
||||
logger.info('danbooru-mcp container already running.')
|
||||
return
|
||||
# Container not running — start it via docker compose
|
||||
print('Starting danbooru-mcp container via docker compose …')
|
||||
logger.info('Starting danbooru-mcp container via docker compose …')
|
||||
subprocess.run(
|
||||
['docker', 'compose', 'up', '-d'],
|
||||
cwd=MCP_COMPOSE_DIR,
|
||||
timeout=120,
|
||||
)
|
||||
print('danbooru-mcp container started.')
|
||||
logger.info('danbooru-mcp container started.')
|
||||
except FileNotFoundError:
|
||||
print('WARNING: docker not found on PATH — danbooru-mcp will not be started automatically.')
|
||||
logger.warning('docker not found on PATH — danbooru-mcp will not be started automatically.')
|
||||
except subprocess.TimeoutExpired:
|
||||
print('WARNING: docker timed out while starting danbooru-mcp.')
|
||||
logger.warning('docker timed out while starting danbooru-mcp.')
|
||||
except Exception as e:
|
||||
print(f'WARNING: Could not ensure danbooru-mcp is running: {e}')
|
||||
logger.warning('Could not ensure danbooru-mcp is running: %s', e)
|
||||
|
||||
|
||||
def _ensure_character_mcp_repo():
|
||||
@@ -92,28 +95,28 @@ def _ensure_character_mcp_repo():
|
||||
os.makedirs(MCP_TOOLS_DIR, exist_ok=True)
|
||||
try:
|
||||
if not os.path.isdir(CHAR_MCP_COMPOSE_DIR):
|
||||
print(f'Cloning character-mcp from {CHAR_MCP_REPO_URL} …')
|
||||
logger.info('Cloning character-mcp from %s …', CHAR_MCP_REPO_URL)
|
||||
subprocess.run(
|
||||
['git', 'clone', CHAR_MCP_REPO_URL, CHAR_MCP_COMPOSE_DIR],
|
||||
timeout=120, check=True,
|
||||
)
|
||||
print('character-mcp cloned successfully.')
|
||||
logger.info('character-mcp cloned successfully.')
|
||||
else:
|
||||
print('Updating character-mcp via git pull …')
|
||||
logger.info('Updating character-mcp via git pull …')
|
||||
subprocess.run(
|
||||
['git', 'pull'],
|
||||
cwd=CHAR_MCP_COMPOSE_DIR,
|
||||
timeout=60, check=True,
|
||||
)
|
||||
print('character-mcp updated.')
|
||||
logger.info('character-mcp updated.')
|
||||
except FileNotFoundError:
|
||||
print('WARNING: git not found on PATH — character-mcp repo will not be cloned/updated.')
|
||||
logger.warning('git not found on PATH — character-mcp repo will not be cloned/updated.')
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f'WARNING: git operation failed for character-mcp: {e}')
|
||||
logger.warning('git operation failed for character-mcp: %s', e)
|
||||
except subprocess.TimeoutExpired:
|
||||
print('WARNING: git timed out while cloning/updating character-mcp.')
|
||||
logger.warning('git timed out while cloning/updating character-mcp.')
|
||||
except Exception as e:
|
||||
print(f'WARNING: Could not clone/update character-mcp repo: {e}')
|
||||
logger.warning('Could not clone/update character-mcp repo: %s', e)
|
||||
|
||||
|
||||
def ensure_character_mcp_server_running():
|
||||
@@ -128,7 +131,7 @@ def ensure_character_mcp_server_running():
|
||||
character-mcp service is managed by compose instead).
|
||||
"""
|
||||
if os.environ.get('SKIP_MCP_AUTOSTART', '').lower() == 'true':
|
||||
print('SKIP_MCP_AUTOSTART set — skipping character-mcp auto-start.')
|
||||
logger.info('SKIP_MCP_AUTOSTART set — skipping character-mcp auto-start.')
|
||||
return
|
||||
_ensure_character_mcp_repo()
|
||||
try:
|
||||
@@ -137,19 +140,19 @@ def ensure_character_mcp_server_running():
|
||||
capture_output=True, text=True, timeout=10,
|
||||
)
|
||||
if 'character-mcp' in result.stdout:
|
||||
print('character-mcp container already running.')
|
||||
logger.info('character-mcp container already running.')
|
||||
return
|
||||
# Container not running — start it via docker compose
|
||||
print('Starting character-mcp container via docker compose …')
|
||||
logger.info('Starting character-mcp container via docker compose …')
|
||||
subprocess.run(
|
||||
['docker', 'compose', 'up', '-d'],
|
||||
cwd=CHAR_MCP_COMPOSE_DIR,
|
||||
timeout=120,
|
||||
)
|
||||
print('character-mcp container started.')
|
||||
logger.info('character-mcp container started.')
|
||||
except FileNotFoundError:
|
||||
print('WARNING: docker not found on PATH — character-mcp will not be started automatically.')
|
||||
logger.warning('docker not found on PATH — character-mcp will not be started automatically.')
|
||||
except subprocess.TimeoutExpired:
|
||||
print('WARNING: docker timed out while starting character-mcp.')
|
||||
logger.warning('docker timed out while starting character-mcp.')
|
||||
except Exception as e:
|
||||
print(f'WARNING: Could not ensure character-mcp is running: {e}')
|
||||
logger.warning('Could not ensure character-mcp is running: %s', e)
|
||||
|
||||
Reference in New Issue
Block a user