|
| 1 | +from flask import Blueprint, render_template, request, jsonify, flash, redirect, url_for |
| 2 | +from flask_login import login_required, current_user |
| 3 | +from app.models import db |
| 4 | +from app.directadmin_api import DirectAdminAPI |
| 5 | + |
| 6 | +settings_bp = Blueprint('settings', __name__, url_prefix='/settings') |
| 7 | + |
| 8 | +@settings_bp.route('/') |
| 9 | +@login_required |
| 10 | +def index(): |
| 11 | + return render_template('settings.html') |
| 12 | + |
| 13 | +@settings_bp.route('/api/da-config', methods=['GET']) |
| 14 | +@login_required |
| 15 | +def get_da_config(): |
| 16 | + return jsonify({ |
| 17 | + 'da_server': current_user.da_server or '', |
| 18 | + 'da_username': current_user.da_username or '', |
| 19 | + 'da_domain': current_user.da_domain or '', |
| 20 | + 'has_password': bool(current_user.da_password_encrypted) |
| 21 | + }) |
| 22 | + |
| 23 | +@settings_bp.route('/api/da-config', methods=['POST']) |
| 24 | +@login_required |
| 25 | +def update_da_config(): |
| 26 | + data = request.json |
| 27 | + |
| 28 | + # Validate required fields |
| 29 | + required_fields = ['da_server', 'da_username', 'da_domain'] |
| 30 | + for field in required_fields: |
| 31 | + if not data.get(field): |
| 32 | + return jsonify({'error': f'{field} is required'}), 400 |
| 33 | + |
| 34 | + # Update DirectAdmin settings |
| 35 | + current_user.da_server = data['da_server'].rstrip('/') |
| 36 | + current_user.da_username = data['da_username'] |
| 37 | + current_user.da_domain = data['da_domain'] |
| 38 | + |
| 39 | + # Only update password if provided |
| 40 | + if data.get('da_password'): |
| 41 | + current_user.set_da_password(data['da_password']) |
| 42 | + |
| 43 | + db.session.commit() |
| 44 | + |
| 45 | + return jsonify({'success': True, 'message': 'DirectAdmin settings updated successfully'}) |
| 46 | + |
| 47 | +@settings_bp.route('/api/test-connection', methods=['POST']) |
| 48 | +@login_required |
| 49 | +def test_connection(): |
| 50 | + """Test DirectAdmin connection with provided credentials""" |
| 51 | + data = request.json |
| 52 | + |
| 53 | + try: |
| 54 | + # Use provided credentials or current user's |
| 55 | + server = data.get('da_server') or current_user.da_server |
| 56 | + username = data.get('da_username') or current_user.da_username |
| 57 | + password = data.get('da_password') or current_user.get_da_password() |
| 58 | + |
| 59 | + if not all([server, username, password]): |
| 60 | + return jsonify({'error': 'Missing credentials'}), 400 |
| 61 | + |
| 62 | + # Test connection |
| 63 | + api = DirectAdminAPI(server, username, password) |
| 64 | + # Try to get domains as a test |
| 65 | + response = api._make_request('API_SHOW_DOMAINS') |
| 66 | + |
| 67 | + if response.status_code == 200: |
| 68 | + return jsonify({'success': True, 'message': 'Connection successful!'}) |
| 69 | + else: |
| 70 | + return jsonify({'error': 'Connection failed'}), 400 |
| 71 | + |
| 72 | + except Exception as e: |
| 73 | + return jsonify({'error': str(e)}), 500 |
0 commit comments