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:
@@ -16,24 +16,13 @@ from services.sync import sync_actions
|
||||
from services.file_io import get_available_loras
|
||||
from services.llm import load_prompt, call_llm
|
||||
from utils import allowed_file, _LORA_DEFAULTS
|
||||
from routes.shared import register_common_routes
|
||||
|
||||
logger = logging.getLogger('gaze')
|
||||
|
||||
|
||||
def register_routes(app):
|
||||
|
||||
@app.route('/get_missing_actions')
|
||||
def get_missing_actions():
|
||||
missing = Action.query.filter((Action.image_path == None) | (Action.image_path == '')).order_by(Action.name).all()
|
||||
return {'missing': [{'slug': a.slug, 'name': a.name} for a in missing]}
|
||||
|
||||
@app.route('/clear_all_action_covers', methods=['POST'])
|
||||
def clear_all_action_covers():
|
||||
actions = Action.query.all()
|
||||
for action in actions:
|
||||
action.image_path = None
|
||||
db.session.commit()
|
||||
return {'success': True}
|
||||
register_common_routes(app, 'actions')
|
||||
|
||||
@app.route('/actions')
|
||||
def actions_index():
|
||||
@@ -151,40 +140,11 @@ def register_routes(app):
|
||||
return redirect(url_for('action_detail', slug=slug))
|
||||
|
||||
except Exception as e:
|
||||
print(f"Edit error: {e}")
|
||||
logger.exception("Edit error: %s", e)
|
||||
flash(f"Error saving changes: {str(e)}")
|
||||
|
||||
return render_template('actions/edit.html', action=action, loras=loras)
|
||||
|
||||
@app.route('/action/<path:slug>/upload', methods=['POST'])
|
||||
def upload_action_image(slug):
|
||||
action = Action.query.filter_by(slug=slug).first_or_404()
|
||||
|
||||
if 'image' not in request.files:
|
||||
flash('No file part')
|
||||
return redirect(request.url)
|
||||
|
||||
file = request.files['image']
|
||||
if file.filename == '':
|
||||
flash('No selected file')
|
||||
return redirect(request.url)
|
||||
|
||||
if file and allowed_file(file.filename):
|
||||
# Create action subfolder
|
||||
action_folder = os.path.join(app.config['UPLOAD_FOLDER'], f"actions/{slug}")
|
||||
os.makedirs(action_folder, exist_ok=True)
|
||||
|
||||
filename = secure_filename(file.filename)
|
||||
file_path = os.path.join(action_folder, filename)
|
||||
file.save(file_path)
|
||||
|
||||
# Store relative path in DB
|
||||
action.image_path = f"actions/{slug}/{filename}"
|
||||
db.session.commit()
|
||||
flash('Image uploaded successfully!')
|
||||
|
||||
return redirect(url_for('action_detail', slug=slug))
|
||||
|
||||
@app.route('/action/<path:slug>/generate', methods=['POST'])
|
||||
def generate_action_image(slug):
|
||||
action_obj = Action.query.filter_by(slug=slug).first_or_404()
|
||||
@@ -352,7 +312,7 @@ def register_routes(app):
|
||||
# Append to main prompt
|
||||
if extra_parts:
|
||||
prompts["main"] += ", " + ", ".join(extra_parts)
|
||||
print(f"Added extra character: {extra_char.name}")
|
||||
logger.debug("Added extra character: %s", extra_char.name)
|
||||
|
||||
_append_background(prompts, character)
|
||||
|
||||
@@ -377,33 +337,12 @@ def register_routes(app):
|
||||
return redirect(url_for('action_detail', slug=slug))
|
||||
|
||||
except Exception as e:
|
||||
print(f"Generation error: {e}")
|
||||
logger.exception("Generation error: %s", e)
|
||||
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
|
||||
return {'error': str(e)}, 500
|
||||
flash(f"Error during generation: {str(e)}")
|
||||
return redirect(url_for('action_detail', slug=slug))
|
||||
|
||||
@app.route('/action/<path:slug>/replace_cover_from_preview', methods=['POST'])
|
||||
def replace_action_cover_from_preview(slug):
|
||||
action = Action.query.filter_by(slug=slug).first_or_404()
|
||||
preview_path = request.form.get('preview_path')
|
||||
if preview_path and os.path.exists(os.path.join(app.config['UPLOAD_FOLDER'], preview_path)):
|
||||
action.image_path = preview_path
|
||||
db.session.commit()
|
||||
flash('Cover image updated!')
|
||||
else:
|
||||
flash('No valid preview image selected.', 'error')
|
||||
return redirect(url_for('action_detail', slug=slug))
|
||||
|
||||
@app.route('/action/<path:slug>/save_defaults', methods=['POST'])
|
||||
def save_action_defaults(slug):
|
||||
action = Action.query.filter_by(slug=slug).first_or_404()
|
||||
selected_fields = request.form.getlist('include_field')
|
||||
action.default_fields = selected_fields
|
||||
db.session.commit()
|
||||
flash('Default prompt selection saved for this action!')
|
||||
return redirect(url_for('action_detail', slug=slug))
|
||||
|
||||
@app.route('/actions/bulk_create', methods=['POST'])
|
||||
def bulk_create_actions_from_loras():
|
||||
_s = Settings.query.first()
|
||||
@@ -556,7 +495,7 @@ def register_routes(app):
|
||||
action_data['action_id'] = safe_slug
|
||||
action_data['action_name'] = name
|
||||
except Exception as e:
|
||||
print(f"LLM error: {e}")
|
||||
logger.exception("LLM error: %s", e)
|
||||
flash(f"Failed to generate action profile: {e}")
|
||||
return render_template('actions/create.html', form_data=form_data)
|
||||
else:
|
||||
@@ -587,74 +526,9 @@ def register_routes(app):
|
||||
flash('Action created successfully!')
|
||||
return redirect(url_for('action_detail', slug=safe_slug))
|
||||
except Exception as e:
|
||||
print(f"Save error: {e}")
|
||||
logger.exception("Save error: %s", e)
|
||||
flash(f"Failed to create action: {e}")
|
||||
return render_template('actions/create.html', form_data=form_data)
|
||||
|
||||
return render_template('actions/create.html', form_data=form_data)
|
||||
|
||||
@app.route('/action/<path:slug>/clone', methods=['POST'])
|
||||
def clone_action(slug):
|
||||
action = Action.query.filter_by(slug=slug).first_or_404()
|
||||
|
||||
# Find the next available number for the clone
|
||||
base_id = action.action_id
|
||||
match = re.match(r'^(.+?)_(\d+)$', base_id)
|
||||
if match:
|
||||
base_name = match.group(1)
|
||||
current_num = int(match.group(2))
|
||||
else:
|
||||
base_name = base_id
|
||||
current_num = 1
|
||||
|
||||
next_num = current_num + 1
|
||||
while True:
|
||||
new_id = f"{base_name}_{next_num:02d}"
|
||||
new_filename = f"{new_id}.json"
|
||||
new_path = os.path.join(app.config['ACTIONS_DIR'], new_filename)
|
||||
if not os.path.exists(new_path):
|
||||
break
|
||||
next_num += 1
|
||||
|
||||
new_data = action.data.copy()
|
||||
new_data['action_id'] = new_id
|
||||
new_data['action_name'] = f"{action.name} (Copy)"
|
||||
|
||||
with open(new_path, 'w') as f:
|
||||
json.dump(new_data, f, indent=2)
|
||||
|
||||
new_slug = re.sub(r'[^a-zA-Z0-9_]', '', new_id)
|
||||
new_action = Action(
|
||||
action_id=new_id, slug=new_slug, filename=new_filename,
|
||||
name=new_data['action_name'], data=new_data
|
||||
)
|
||||
db.session.add(new_action)
|
||||
db.session.commit()
|
||||
|
||||
flash(f'Action cloned as "{new_id}"!')
|
||||
return redirect(url_for('action_detail', slug=new_slug))
|
||||
|
||||
@app.route('/action/<path:slug>/save_json', methods=['POST'])
|
||||
def save_action_json(slug):
|
||||
action = Action.query.filter_by(slug=slug).first_or_404()
|
||||
try:
|
||||
new_data = json.loads(request.form.get('json_data', ''))
|
||||
except (ValueError, TypeError) as e:
|
||||
return {'success': False, 'error': f'Invalid JSON: {e}'}, 400
|
||||
action.data = new_data
|
||||
flag_modified(action, 'data')
|
||||
db.session.commit()
|
||||
if action.filename:
|
||||
file_path = os.path.join(app.config['ACTIONS_DIR'], action.filename)
|
||||
with open(file_path, 'w') as f:
|
||||
json.dump(new_data, f, indent=2)
|
||||
return {'success': True}
|
||||
|
||||
@app.route('/action/<path:slug>/favourite', methods=['POST'])
|
||||
def toggle_action_favourite(slug):
|
||||
action = Action.query.filter_by(slug=slug).first_or_404()
|
||||
action.is_favourite = not action.is_favourite
|
||||
db.session.commit()
|
||||
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
|
||||
return {'success': True, 'is_favourite': action.is_favourite}
|
||||
return redirect(url_for('action_detail', slug=slug))
|
||||
|
||||
Reference in New Issue
Block a user