139 lines
3.8 KiB
Python
Executable File
139 lines
3.8 KiB
Python
Executable File
from flask import request, jsonify
|
|
from app import db
|
|
from app.api import bp
|
|
from app.models import User, PrintJob
|
|
from app.auth.routes import admin_required, token_required
|
|
|
|
@bp.route('/users', methods=['GET'])
|
|
@admin_required
|
|
def get_users():
|
|
"""Get all users (admin only)"""
|
|
users = User.query.all()
|
|
result = []
|
|
|
|
for user in users:
|
|
# Count jobs
|
|
total_jobs = PrintJob.query.filter_by(user_id=user.id).count()
|
|
active_jobs = PrintJob.query.filter_by(user_id=user.id, aborted=False).count()
|
|
|
|
user_data = {
|
|
'id': user.id,
|
|
'github_id': user.github_id,
|
|
'username': user.username,
|
|
'display_name': user.display_name,
|
|
'email': user.email,
|
|
'role': user.role,
|
|
'job_count': total_jobs,
|
|
'active_job_count': active_jobs
|
|
}
|
|
result.append(user_data)
|
|
|
|
return jsonify(result)
|
|
|
|
@bp.route('/users/<user_id>', methods=['GET'])
|
|
@admin_required
|
|
def get_user(user_id):
|
|
"""Get a specific user (admin only)"""
|
|
user = User.query.get_or_404(user_id)
|
|
|
|
# Count jobs
|
|
total_jobs = PrintJob.query.filter_by(user_id=user.id).count()
|
|
active_jobs = PrintJob.query.filter_by(user_id=user.id, aborted=False).count()
|
|
|
|
result = {
|
|
'id': user.id,
|
|
'github_id': user.github_id,
|
|
'username': user.username,
|
|
'display_name': user.display_name,
|
|
'email': user.email,
|
|
'role': user.role,
|
|
'job_count': total_jobs,
|
|
'active_job_count': active_jobs
|
|
}
|
|
|
|
return jsonify(result)
|
|
|
|
@bp.route('/users/<user_id>', methods=['PUT'])
|
|
@admin_required
|
|
def update_user(user_id):
|
|
"""Update a user (admin only)"""
|
|
user = User.query.get_or_404(user_id)
|
|
data = request.get_json() or {}
|
|
|
|
if 'role' in data and data['role'] in ['admin', 'user', 'guest']:
|
|
user.role = data['role']
|
|
|
|
if 'display_name' in data:
|
|
user.display_name = data['display_name']
|
|
|
|
db.session.commit()
|
|
|
|
return jsonify({
|
|
'id': user.id,
|
|
'github_id': user.github_id,
|
|
'username': user.username,
|
|
'display_name': user.display_name,
|
|
'email': user.email,
|
|
'role': user.role
|
|
})
|
|
|
|
@bp.route('/users/<user_id>', methods=['DELETE'])
|
|
@admin_required
|
|
def delete_user(user_id):
|
|
"""Delete a user (admin only)"""
|
|
user = User.query.get_or_404(user_id)
|
|
|
|
# Check if user has active jobs
|
|
active_jobs = PrintJob.query.filter_by(user_id=user.id, aborted=False).first()
|
|
if active_jobs:
|
|
return jsonify({'error': 'Cannot delete user with active jobs'}), 400
|
|
|
|
db.session.delete(user)
|
|
db.session.commit()
|
|
|
|
return jsonify({'message': 'User deleted successfully'})
|
|
|
|
@bp.route('/me', methods=['GET'])
|
|
@token_required
|
|
def get_current_user():
|
|
"""Get the current user's profile"""
|
|
user = User.query.get(request.user_id)
|
|
if not user:
|
|
return jsonify({'error': 'User not found'}), 404
|
|
|
|
result = {
|
|
'id': user.id,
|
|
'github_id': user.github_id,
|
|
'username': user.username,
|
|
'display_name': user.display_name,
|
|
'email': user.email,
|
|
'role': user.role
|
|
}
|
|
|
|
return jsonify(result)
|
|
|
|
@bp.route('/me', methods=['PUT'])
|
|
@token_required
|
|
def update_current_user():
|
|
"""Update the current user's profile"""
|
|
user = User.query.get(request.user_id)
|
|
if not user:
|
|
return jsonify({'error': 'User not found'}), 404
|
|
|
|
data = request.get_json() or {}
|
|
|
|
if 'display_name' in data:
|
|
user.display_name = data['display_name']
|
|
|
|
db.session.commit()
|
|
|
|
result = {
|
|
'id': user.id,
|
|
'github_id': user.github_id,
|
|
'username': user.username,
|
|
'display_name': user.display_name,
|
|
'email': user.email,
|
|
'role': user.role
|
|
}
|
|
|
|
return jsonify(result) |