47 lines
No EOL
1.6 KiB
Python
47 lines
No EOL
1.6 KiB
Python
from flask import Blueprint, render_template, jsonify, redirect, url_for, flash, current_app
|
|
from flask_login import login_required, current_user
|
|
from ..models import User
|
|
from ..migrations import run_migrations
|
|
from ..utils.reset_db import reset_database
|
|
|
|
bp = Blueprint('admin', __name__, url_prefix='/admin')
|
|
|
|
@bp.route('/')
|
|
@login_required
|
|
def index():
|
|
"""Admin panel home"""
|
|
if not current_user.is_admin:
|
|
flash('Access denied. Admin privileges required.', 'error')
|
|
return redirect(url_for('dashboard.index'))
|
|
|
|
return render_template('admin/panel.html')
|
|
|
|
@bp.route('/run-migrations', methods=['POST'])
|
|
@login_required
|
|
def trigger_migrations():
|
|
"""Manually trigger database migrations (admin only)"""
|
|
# Check if user is admin
|
|
if not current_user.is_admin:
|
|
return jsonify({'error': 'Unauthorized. Admin privileges required'}), 403
|
|
|
|
# Run migrations
|
|
with current_app.app_context():
|
|
run_migrations()
|
|
|
|
return jsonify({'success': True, 'message': 'Migrations completed successfully'})
|
|
|
|
@bp.route('/reset-database', methods=['POST'])
|
|
@login_required
|
|
def reset_db():
|
|
"""Reset the entire database (admin only)"""
|
|
# Check if user is admin
|
|
if not current_user.is_admin:
|
|
return jsonify({'error': 'Unauthorized. Admin privileges required'}), 403
|
|
|
|
# Reset database
|
|
success = reset_database()
|
|
|
|
if success:
|
|
return jsonify({'success': True, 'message': 'Database reset successfully. You will be logged out.'})
|
|
else:
|
|
return jsonify({'error': 'Failed to reset database. Check logs for details.'}), 500 |