Skip to content

Commit 1b6dd9e

Browse files
Update settings.py
1 parent 117c1da commit 1b6dd9e

File tree

1 file changed

+78
-40
lines changed

1 file changed

+78
-40
lines changed

app/settings.py

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,78 +28,102 @@ def get_da_config():
2828
@settings_bp.route('/api/da-config', methods=['POST'])
2929
@login_required
3030
def update_da_config():
31+
"""Save DirectAdmin settings WITHOUT testing connection"""
3132
try:
3233
# Ensure we have JSON data
3334
if not request.is_json:
3435
return jsonify({'error': 'Content-Type must be application/json'}), 400
3536

3637
data = request.json
37-
print(f"Received settings update: {data}") # Debug log
38+
print(f"Saving settings for user: {current_user.username}")
3839

39-
# Validate required fields
40+
# Basic validation - just check if fields are provided
4041
required_fields = ['da_server', 'da_username', 'da_domain']
42+
missing_fields = []
43+
4144
for field in required_fields:
42-
if not data.get(field):
43-
return jsonify({'error': f'{field} is required'}), 400
45+
if not data.get(field, '').strip():
46+
missing_fields.append(field)
47+
48+
if missing_fields:
49+
return jsonify({
50+
'error': f'Missing required fields: {", ".join(missing_fields)}',
51+
'missing_fields': missing_fields
52+
}), 400
4453

4554
# Ensure user has encryption key
4655
if not current_user.encryption_key:
56+
print("Generating encryption key for user")
4757
current_user.generate_encryption_key()
4858

59+
# Clean and save the server URL
60+
server_url = data['da_server'].strip()
61+
# Ensure it has a protocol
62+
if not server_url.startswith(('http://', 'https://')):
63+
server_url = 'https://' + server_url
64+
4965
# Update DirectAdmin settings
50-
current_user.da_server = data['da_server'].rstrip('/')
51-
current_user.da_username = data['da_username']
52-
current_user.da_domain = data['da_domain']
66+
current_user.da_server = server_url.rstrip('/')
67+
current_user.da_username = data['da_username'].strip()
68+
current_user.da_domain = data['da_domain'].strip()
5369

5470
# Only update password if provided
5571
if data.get('da_password'):
56-
print("Setting new DA password") # Debug log
72+
print("Updating DA password")
5773
current_user.set_da_password(data['da_password'])
58-
74+
elif not current_user.da_password_encrypted:
75+
# No existing password and none provided
76+
return jsonify({
77+
'error': 'Password is required for initial setup',
78+
'missing_fields': ['da_password']
79+
}), 400
80+
81+
# Save to database
5982
db.session.commit()
60-
print("Settings saved successfully") # Debug log
83+
print("Settings saved successfully")
6184

62-
return jsonify({'success': True, 'message': 'DirectAdmin settings updated successfully'})
85+
return jsonify({
86+
'success': True,
87+
'message': 'Settings saved successfully! (Connection not tested)',
88+
'saved_data': {
89+
'da_server': current_user.da_server,
90+
'da_username': current_user.da_username,
91+
'da_domain': current_user.da_domain
92+
}
93+
})
6394

6495
except Exception as e:
65-
print(f"Error updating settings: {str(e)}") # Debug log
66-
print(traceback.format_exc()) # Full traceback
96+
print(f"Error saving settings: {str(e)}")
97+
print(traceback.format_exc())
6798
db.session.rollback()
68-
return jsonify({'error': f'Failed to save settings: {str(e)}'}), 500
69-
70-
@settings_bp.route('/api/debug')
71-
@login_required
72-
def debug_info():
73-
"""Debug endpoint to check user state"""
74-
return jsonify({
75-
'user': current_user.username,
76-
'has_encryption_key': bool(current_user.encryption_key),
77-
'has_da_config': current_user.has_da_config(),
78-
'da_server_configured': bool(current_user.da_server),
79-
'da_username_configured': bool(current_user.da_username),
80-
'da_password_configured': bool(current_user.da_password_encrypted),
81-
'da_domain_configured': bool(current_user.da_domain)
82-
})
99+
return jsonify({
100+
'error': f'Failed to save settings: {str(e)}',
101+
'details': 'Check server logs for more information'
102+
}), 500
83103

84104
@settings_bp.route('/api/test-connection', methods=['POST'])
85105
@login_required
86106
def test_connection():
87-
"""Test DirectAdmin connection with provided credentials"""
107+
"""Test DirectAdmin connection - completely separate from saving"""
88108
try:
89-
# Ensure we have JSON data
90-
if not request.is_json:
91-
return jsonify({'error': 'Content-Type must be application/json'}), 400
92-
93109
data = request.json
94-
print(f"Testing connection with: {data.get('da_server')}") # Debug log
110+
print(f"Testing connection for user: {current_user.username}")
95111

96-
# Use provided credentials or current user's
112+
# Use provided credentials or current user's saved ones
97113
server = data.get('da_server') or current_user.da_server
98114
username = data.get('da_username') or current_user.da_username
99115
password = data.get('da_password') or current_user.get_da_password()
100116

101117
if not all([server, username, password]):
102-
return jsonify({'error': 'Missing credentials'}), 400
118+
missing = []
119+
if not server: missing.append('server')
120+
if not username: missing.append('username')
121+
if not password: missing.append('password')
122+
123+
return jsonify({
124+
'error': f'Missing credentials: {", ".join(missing)}',
125+
'missing': missing
126+
}), 400
103127

104128
# Ensure server URL is properly formatted
105129
if not server.startswith(('http://', 'https://')):
@@ -112,9 +136,23 @@ def test_connection():
112136
if success:
113137
return jsonify({'success': True, 'message': message})
114138
else:
115-
return jsonify({'error': message}), 400
139+
return jsonify({'error': message, 'success': False}), 200 # Return 200 even on test failure
116140

117141
except Exception as e:
118-
print(f"Test connection error: {str(e)}") # Debug log
119-
print(traceback.format_exc()) # Full traceback
120-
return jsonify({'error': str(e)}), 500
142+
print(f"Test connection error: {str(e)}")
143+
print(traceback.format_exc())
144+
return jsonify({'error': str(e), 'success': False}), 200 # Return 200 to avoid confusion
145+
146+
@settings_bp.route('/api/debug')
147+
@login_required
148+
def debug_info():
149+
"""Debug endpoint to check user state"""
150+
return jsonify({
151+
'user': current_user.username,
152+
'has_encryption_key': bool(current_user.encryption_key),
153+
'has_da_config': current_user.has_da_config(),
154+
'da_server_configured': bool(current_user.da_server),
155+
'da_username_configured': bool(current_user.da_username),
156+
'da_password_configured': bool(current_user.da_password_encrypted),
157+
'da_domain_configured': bool(current_user.da_domain)
158+
})

0 commit comments

Comments
 (0)