为各类别添加数据导入导出功能
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
AI模型 CRUD API
|
||||
"""
|
||||
import uuid
|
||||
import json
|
||||
from datetime import datetime
|
||||
from flask import Blueprint, request, jsonify
|
||||
from flask import Blueprint, request, jsonify, Response
|
||||
from config import MODELS_FILE
|
||||
from utils import load_data, save_data, parse_date_to_timestamp, safe_sort_key
|
||||
|
||||
@@ -88,3 +89,63 @@ def api_toggle_model_visible(model_id):
|
||||
model['visible'] = not model.get('visible', True)
|
||||
save_data(MODELS_FILE, models)
|
||||
return jsonify({'success': True, 'visible': model['visible']})
|
||||
|
||||
|
||||
# ─── 导出导入 ───────────────────────────────────────────────────────
|
||||
|
||||
@models_bp.route('/api/models/export', methods=['GET'])
|
||||
def api_export_models():
|
||||
"""导出所有模型数据"""
|
||||
try:
|
||||
models = load_data(MODELS_FILE)
|
||||
export_data = {
|
||||
'type': 'models',
|
||||
'items': models,
|
||||
'count': len(models),
|
||||
'export_time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
|
||||
'version': '1.0'
|
||||
}
|
||||
json_str = json.dumps(export_data, ensure_ascii=False, indent=2)
|
||||
response = Response(
|
||||
json_str,
|
||||
mimetype='application/json',
|
||||
headers={'Content-Disposition': f'attachment; filename=models-export-{datetime.now().strftime("%Y%m%d%H%M%S")}.json'}
|
||||
)
|
||||
return response
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
|
||||
@models_bp.route('/api/models/import', methods=['GET', 'POST'])
|
||||
def api_import_models():
|
||||
"""导入模型数据"""
|
||||
try:
|
||||
if request.method == 'GET':
|
||||
return jsonify({'endpoint': '/api/models/import', 'method': 'POST', 'params': {'mode': 'merge 或 replace'}})
|
||||
|
||||
import_data = request.get_json()
|
||||
if not import_data or 'items' not in import_data:
|
||||
return jsonify({'error': '缺少 items 字段'}), 400
|
||||
|
||||
mode = request.args.get('mode', 'merge')
|
||||
models = load_data(MODELS_FILE)
|
||||
imported_items = import_data['items']
|
||||
|
||||
result = {'success': True, 'imported': 0, 'updated': 0, 'skipped': []}
|
||||
|
||||
for item in imported_items:
|
||||
existing = next((m for m in models if m['id'] == item['id']), None)
|
||||
if existing:
|
||||
if mode == 'replace':
|
||||
existing.update(item)
|
||||
result['updated'] += 1
|
||||
else:
|
||||
result['skipped'].append(item['id'])
|
||||
else:
|
||||
models.append(item)
|
||||
result['imported'] += 1
|
||||
|
||||
save_data(MODELS_FILE, models)
|
||||
return jsonify(result)
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
Reference in New Issue
Block a user