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:
@@ -15,24 +15,13 @@ from services.sync import sync_outfits
|
||||
from services.file_io import get_available_loras, _count_outfit_lora_assignments
|
||||
from utils import allowed_file, _LORA_DEFAULTS
|
||||
from services.llm import load_prompt, call_llm
|
||||
from routes.shared import register_common_routes
|
||||
|
||||
logger = logging.getLogger('gaze')
|
||||
|
||||
|
||||
def register_routes(app):
|
||||
|
||||
@app.route('/get_missing_outfits')
|
||||
def get_missing_outfits():
|
||||
missing = Outfit.query.filter((Outfit.image_path == None) | (Outfit.image_path == '')).order_by(Outfit.name).all()
|
||||
return {'missing': [{'slug': o.slug, 'name': o.name} for o in missing]}
|
||||
|
||||
@app.route('/clear_all_outfit_covers', methods=['POST'])
|
||||
def clear_all_outfit_covers():
|
||||
outfits = Outfit.query.all()
|
||||
for outfit in outfits:
|
||||
outfit.image_path = None
|
||||
db.session.commit()
|
||||
return {'success': True}
|
||||
register_common_routes(app, 'outfits')
|
||||
|
||||
@app.route('/outfits')
|
||||
def outfits_index():
|
||||
@@ -268,40 +257,11 @@ def register_routes(app):
|
||||
return redirect(url_for('outfit_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('outfits/edit.html', outfit=outfit, loras=loras)
|
||||
|
||||
@app.route('/outfit/<path:slug>/upload', methods=['POST'])
|
||||
def upload_outfit_image(slug):
|
||||
outfit = Outfit.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 outfit subfolder
|
||||
outfit_folder = os.path.join(app.config['UPLOAD_FOLDER'], f"outfits/{slug}")
|
||||
os.makedirs(outfit_folder, exist_ok=True)
|
||||
|
||||
filename = secure_filename(file.filename)
|
||||
file_path = os.path.join(outfit_folder, filename)
|
||||
file.save(file_path)
|
||||
|
||||
# Store relative path in DB
|
||||
outfit.image_path = f"outfits/{slug}/{filename}"
|
||||
db.session.commit()
|
||||
flash('Image uploaded successfully!')
|
||||
|
||||
return redirect(url_for('outfit_detail', slug=slug))
|
||||
|
||||
@app.route('/outfit/<path:slug>/generate', methods=['POST'])
|
||||
def generate_outfit_image(slug):
|
||||
outfit = Outfit.query.filter_by(slug=slug).first_or_404()
|
||||
@@ -406,24 +366,12 @@ def register_routes(app):
|
||||
return redirect(url_for('outfit_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('outfit_detail', slug=slug))
|
||||
|
||||
@app.route('/outfit/<path:slug>/replace_cover_from_preview', methods=['POST'])
|
||||
def replace_outfit_cover_from_preview(slug):
|
||||
outfit = Outfit.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)):
|
||||
outfit.image_path = preview_path
|
||||
db.session.commit()
|
||||
flash('Cover image updated!')
|
||||
else:
|
||||
flash('No valid preview image selected.', 'error')
|
||||
return redirect(url_for('outfit_detail', slug=slug))
|
||||
|
||||
@app.route('/outfit/create', methods=['GET', 'POST'])
|
||||
def create_outfit():
|
||||
form_data = {}
|
||||
@@ -496,7 +444,7 @@ def register_routes(app):
|
||||
outfit_data['tags'] = []
|
||||
|
||||
except Exception as e:
|
||||
print(f"LLM error: {e}")
|
||||
logger.exception("LLM error: %s", e)
|
||||
flash(f"Failed to generate outfit profile: {e}")
|
||||
return render_template('outfits/create.html', form_data=form_data)
|
||||
else:
|
||||
@@ -542,92 +490,10 @@ def register_routes(app):
|
||||
return redirect(url_for('outfit_detail', slug=safe_slug))
|
||||
|
||||
except Exception as e:
|
||||
print(f"Save error: {e}")
|
||||
logger.exception("Save error: %s", e)
|
||||
flash(f"Failed to create outfit: {e}")
|
||||
return render_template('outfits/create.html', form_data=form_data)
|
||||
|
||||
return render_template('outfits/create.html', form_data=form_data)
|
||||
|
||||
@app.route('/outfit/<path:slug>/save_defaults', methods=['POST'])
|
||||
def save_outfit_defaults(slug):
|
||||
outfit = Outfit.query.filter_by(slug=slug).first_or_404()
|
||||
selected_fields = request.form.getlist('include_field')
|
||||
outfit.default_fields = selected_fields
|
||||
db.session.commit()
|
||||
flash('Default prompt selection saved for this outfit!')
|
||||
return redirect(url_for('outfit_detail', slug=slug))
|
||||
|
||||
@app.route('/outfit/<path:slug>/clone', methods=['POST'])
|
||||
def clone_outfit(slug):
|
||||
outfit = Outfit.query.filter_by(slug=slug).first_or_404()
|
||||
|
||||
# Find the next available number for the clone
|
||||
base_id = outfit.outfit_id
|
||||
# Extract base name without number suffix
|
||||
import re
|
||||
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
|
||||
|
||||
# Find next available number
|
||||
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['CLOTHING_DIR'], new_filename)
|
||||
if not os.path.exists(new_path):
|
||||
break
|
||||
next_num += 1
|
||||
|
||||
# Create new outfit data (copy of original)
|
||||
new_data = outfit.data.copy()
|
||||
new_data['outfit_id'] = new_id
|
||||
new_data['outfit_name'] = f"{outfit.name} (Copy)"
|
||||
|
||||
# Save the new JSON file
|
||||
with open(new_path, 'w') as f:
|
||||
json.dump(new_data, f, indent=2)
|
||||
|
||||
# Create new outfit in database
|
||||
new_slug = re.sub(r'[^a-zA-Z0-9_]', '', new_id)
|
||||
new_outfit = Outfit(
|
||||
outfit_id=new_id,
|
||||
slug=new_slug,
|
||||
filename=new_filename,
|
||||
name=new_data['outfit_name'],
|
||||
data=new_data
|
||||
)
|
||||
db.session.add(new_outfit)
|
||||
db.session.commit()
|
||||
|
||||
flash(f'Outfit cloned as "{new_id}"!')
|
||||
return redirect(url_for('outfit_detail', slug=new_slug))
|
||||
|
||||
@app.route('/outfit/<path:slug>/save_json', methods=['POST'])
|
||||
def save_outfit_json(slug):
|
||||
outfit = Outfit.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
|
||||
outfit.data = new_data
|
||||
flag_modified(outfit, 'data')
|
||||
db.session.commit()
|
||||
if outfit.filename:
|
||||
file_path = os.path.join(app.config['CLOTHING_DIR'], outfit.filename)
|
||||
with open(file_path, 'w') as f:
|
||||
json.dump(new_data, f, indent=2)
|
||||
return {'success': True}
|
||||
|
||||
@app.route('/outfit/<path:slug>/favourite', methods=['POST'])
|
||||
def toggle_outfit_favourite(slug):
|
||||
outfit = Outfit.query.filter_by(slug=slug).first_or_404()
|
||||
outfit.is_favourite = not outfit.is_favourite
|
||||
db.session.commit()
|
||||
if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
|
||||
return {'success': True, 'is_favourite': outfit.is_favourite}
|
||||
return redirect(url_for('outfit_detail', slug=slug))
|
||||
|
||||
Reference in New Issue
Block a user