feat: Initial commit with full deployment architecture and scripts
This commit is contained in:
commit
abb3efe114
96 files changed
+16789
No files matched your search
@@ -0,0 +1,191 @@
|
||||
"""
|
||||
backend/app/api/user.py
|
||||
文件用途:用户相关 API 路由
|
||||
引用文件:flask, models, decorators
|
||||
逻辑关系:提供用户登录、首页配置、收藏夹、常用问题等接口
|
||||
"""
|
||||
from flask import Blueprint, jsonify, request, g, current_app
|
||||
from app import db
|
||||
from app.models import User, Agent, UserFavoriteAgent, UserHomepageConfig, QuickQuestion, UserDefaultAgent
|
||||
from app.decorators import _get_current_user
|
||||
|
||||
user_bp = Blueprint('user', __name__)
|
||||
|
||||
@user_bp.route('/login', methods=['POST'])
|
||||
def login():
|
||||
data = request.get_json() or {}
|
||||
username = data.get('username')
|
||||
password = data.get('password')
|
||||
|
||||
user = User.query.filter_by(username=username).first()
|
||||
if user and user.check_password(password):
|
||||
return jsonify({
|
||||
'id': user.id,
|
||||
'username': user.username,
|
||||
'role': user.role,
|
||||
'token': 'dummy-token' # In real app use JWT
|
||||
})
|
||||
return jsonify({'error': 'Invalid credentials'}), 401
|
||||
|
||||
@user_bp.route('/home', methods=['GET'])
|
||||
def get_user_home():
|
||||
user = _get_current_user()
|
||||
|
||||
# Favorites
|
||||
favorites = db.session.query(Agent).join(UserFavoriteAgent).filter(UserFavoriteAgent.user_id == user.id).all()
|
||||
favorite_ids = [a.id for a in favorites]
|
||||
|
||||
# Layout
|
||||
config = UserHomepageConfig.query.get(user.id)
|
||||
layout = config.layout if config else {}
|
||||
|
||||
# Embedded agents
|
||||
embedded_agents = []
|
||||
udas = UserDefaultAgent.query.filter_by(user_id=user.id).order_by(UserDefaultAgent.sort_order).all()
|
||||
for uda in udas:
|
||||
agent = Agent.query.get(uda.agent_id)
|
||||
if agent:
|
||||
embedded_agents.append({
|
||||
'id': agent.id,
|
||||
'name': agent.name,
|
||||
'icon_url': agent.icon_url,
|
||||
'description': agent.description
|
||||
})
|
||||
|
||||
# Quick questions
|
||||
qqs = QuickQuestion.query.filter_by(user_id=user.id).all()
|
||||
quick_questions = [{
|
||||
'id': q.id,
|
||||
'title': q.title,
|
||||
'question': q.question,
|
||||
'agent_id': q.agent_id
|
||||
} for q in qqs]
|
||||
|
||||
# All active agents (for selection)
|
||||
all_agents = Agent.query.filter_by(is_active=True).all()
|
||||
agents_list = [{
|
||||
'id': a.id,
|
||||
'name': a.name
|
||||
} for a in all_agents]
|
||||
|
||||
return jsonify({
|
||||
'user': {
|
||||
'id': user.id,
|
||||
'username': user.username,
|
||||
'role': user.role,
|
||||
'real_name': user.real_name,
|
||||
'department': user.department
|
||||
},
|
||||
'favorite_agent_ids': favorite_ids,
|
||||
'layout': layout,
|
||||
'embedded_agents': embedded_agents,
|
||||
'quick_questions': quick_questions,
|
||||
'agents': agents_list
|
||||
})
|
||||
|
||||
@user_bp.route('/agents/<int:agent_id>', methods=['GET'])
|
||||
def get_agent_details(agent_id):
|
||||
"""
|
||||
获取单个 Agent 详情
|
||||
"""
|
||||
user = _get_current_user()
|
||||
agent = Agent.query.get(agent_id)
|
||||
|
||||
if not agent:
|
||||
return jsonify({'error': 'Agent not found'}), 404
|
||||
|
||||
# Check permission (optional, but good practice)
|
||||
# For now, allow if active or user is admin
|
||||
if not agent.is_active and user.role != 'admin':
|
||||
return jsonify({'error': 'Agent is not active'}), 403
|
||||
|
||||
return jsonify({
|
||||
'id': agent.id,
|
||||
'name': agent.name,
|
||||
'description': agent.description,
|
||||
'icon_url': agent.icon_url,
|
||||
'type': agent.type,
|
||||
'extra_config': agent.extra_config
|
||||
})
|
||||
|
||||
@user_bp.route('/config', methods=['GET'])
|
||||
def get_user_config():
|
||||
"""
|
||||
获取用户/系统配置
|
||||
"""
|
||||
user = _get_current_user()
|
||||
return jsonify({
|
||||
'feedback_enabled': current_app.config.get('FEEDBACK_ENABLED', True)
|
||||
})
|
||||
|
||||
@user_bp.route('/favorites/<int:agent_id>', methods=['POST'])
|
||||
def add_favorite(agent_id):
|
||||
user = _get_current_user()
|
||||
if not UserFavoriteAgent.query.filter_by(user_id=user.id, agent_id=agent_id).first():
|
||||
fav = UserFavoriteAgent(user_id=user.id, agent_id=agent_id)
|
||||
db.session.add(fav)
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
@user_bp.route('/favorites/<int:agent_id>', methods=['DELETE'])
|
||||
def remove_favorite(agent_id):
|
||||
user = _get_current_user()
|
||||
fav = UserFavoriteAgent.query.filter_by(user_id=user.id, agent_id=agent_id).first()
|
||||
if fav:
|
||||
db.session.delete(fav)
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
@user_bp.route('/homepage-layout', methods=['PUT'])
|
||||
def update_layout():
|
||||
user = _get_current_user()
|
||||
data = request.get_json() or {}
|
||||
config = UserHomepageConfig.query.get(user.id)
|
||||
if not config:
|
||||
config = UserHomepageConfig(user_id=user.id)
|
||||
db.session.add(config)
|
||||
|
||||
config.layout = data.get('layout', {})
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
@user_bp.route('/embedded-agents', methods=['PUT'])
|
||||
def update_embedded_agents():
|
||||
user = _get_current_user()
|
||||
data = request.get_json() or {}
|
||||
agent_ids = data.get('agent_ids', [])
|
||||
|
||||
if len(agent_ids) > 2:
|
||||
return jsonify({'error': 'Max 2 embedded agents'}), 400
|
||||
|
||||
# Clear existing
|
||||
UserDefaultAgent.query.filter_by(user_id=user.id).delete()
|
||||
|
||||
for i, aid in enumerate(agent_ids):
|
||||
uda = UserDefaultAgent(user_id=user.id, agent_id=aid, sort_order=i)
|
||||
db.session.add(uda)
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({'status': 'ok'})
|
||||
|
||||
@user_bp.route('/quick-questions/<int:qq_id>', methods=['PUT'])
|
||||
def update_quick_question(qq_id: int):
|
||||
"""
|
||||
更新常用问题
|
||||
"""
|
||||
user = _get_current_user()
|
||||
data = request.get_json() or {}
|
||||
|
||||
qq = db.session.query(QuickQuestion).filter_by(id=qq_id, user_id=user.id).first()
|
||||
if not qq:
|
||||
return jsonify({"error": "QuickQuestion not found"}), 404
|
||||
|
||||
if 'title' in data:
|
||||
qq.title = data['title']
|
||||
if 'question' in data:
|
||||
qq.question = data['question']
|
||||
if 'agent_id' in data:
|
||||
qq.agent_id = data['agent_id']
|
||||
|
||||
db.session.commit()
|
||||
return jsonify({"status": "ok"})
|
||||
Reference in new issue
Block a user