Skip to content

Commit bd6f54f

Browse files
Update settings.py
1 parent 4f1be32 commit bd6f54f

File tree

1 file changed

+55
-78
lines changed

1 file changed

+55
-78
lines changed

app/settings.py

Lines changed: 55 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from app.models import db
44
from app.directadmin_api import DirectAdminAPI
55
import traceback
6+
import json
67

78
settings_bp = Blueprint('settings', __name__, url_prefix='/settings')
89

@@ -28,16 +29,39 @@ def get_da_config():
2829
@settings_bp.route('/api/da-config', methods=['POST'])
2930
@login_required
3031
def update_da_config():
31-
"""Save DirectAdmin settings WITHOUT testing connection"""
32+
"""Save DirectAdmin settings"""
33+
print(f"\n=== SAVE ENDPOINT CALLED ===")
34+
print(f"Request method: {request.method}")
35+
print(f"Content-Type: {request.content_type}")
36+
print(f"Request headers: {dict(request.headers)}")
37+
38+
# Check if request has JSON content type
39+
if not request.content_type or 'application/json' not in request.content_type:
40+
print(f"ERROR: Wrong content type: {request.content_type}")
41+
return jsonify({
42+
'error': 'Content-Type must be application/json',
43+
'received_content_type': request.content_type
44+
}), 400
45+
3246
try:
33-
# Ensure we have JSON data
34-
if not request.is_json:
35-
return jsonify({'error': 'Content-Type must be application/json'}), 400
47+
# Try to get JSON data
48+
try:
49+
data = request.get_json(force=True) # Force parsing even if content-type is wrong
50+
if not data:
51+
print("ERROR: No JSON data in request body")
52+
return jsonify({'error': 'No JSON data provided'}), 400
53+
except Exception as json_error:
54+
print(f"ERROR: Failed to parse JSON: {json_error}")
55+
print(f"Request data: {request.data}")
56+
return jsonify({
57+
'error': 'Invalid JSON in request body',
58+
'details': str(json_error)
59+
}), 400
3660

37-
data = request.json
38-
print(f"Saving settings for user: {current_user.username}")
61+
print(f"Received data: {data}")
62+
print(f"Current user: {current_user.username}")
3963

40-
# Basic validation - just check if fields are provided
64+
# Validate required fields
4165
required_fields = ['da_server', 'da_username', 'da_domain']
4266
missing_fields = []
4367

@@ -46,19 +70,19 @@ def update_da_config():
4670
missing_fields.append(field)
4771

4872
if missing_fields:
73+
print(f"Missing fields: {missing_fields}")
4974
return jsonify({
5075
'error': f'Missing required fields: {", ".join(missing_fields)}',
5176
'missing_fields': missing_fields
5277
}), 400
5378

5479
# Ensure user has encryption key
5580
if not current_user.encryption_key:
56-
print("Generating encryption key for user")
81+
print("Generating encryption key")
5782
current_user.generate_encryption_key()
5883

5984
# Clean and save the server URL
6085
server_url = data['da_server'].strip()
61-
# Ensure it has a protocol
6286
if not server_url.startswith(('http://', 'https://')):
6387
server_url = 'https://' + server_url
6488

@@ -79,80 +103,33 @@ def update_da_config():
79103
}), 400
80104

81105
# Save to database
82-
db.session.commit()
83-
print("Settings saved successfully")
106+
try:
107+
db.session.commit()
108+
print("✓ Settings saved successfully")
84109

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-
})
110+
return jsonify({
111+
'success': True,
112+
'message': 'Settings saved successfully!',
113+
'saved_data': {
114+
'da_server': current_user.da_server,
115+
'da_username': current_user.da_username,
116+
'da_domain': current_user.da_domain
117+
}
118+
})
119+
120+
except Exception as db_error:
121+
print(f"Database error: {db_error}")
122+
db.session.rollback()
123+
return jsonify({
124+
'error': 'Database error while saving',
125+
'details': str(db_error)
126+
}), 500
94127

95128
except Exception as e:
96-
print(f"Error saving settings: {str(e)}")
129+
print(f"ERROR in update_da_config: {str(e)}")
97130
print(traceback.format_exc())
98131
db.session.rollback()
99132
return jsonify({
100133
'error': f'Failed to save settings: {str(e)}',
101-
'details': 'Check server logs for more information'
134+
'exception_type': type(e).__name__
102135
}), 500
103-
104-
@settings_bp.route('/api/test-connection', methods=['POST'])
105-
@login_required
106-
def test_connection():
107-
"""Test DirectAdmin connection - completely separate from saving"""
108-
try:
109-
data = request.json
110-
print(f"Testing connection for user: {current_user.username}")
111-
112-
# Use provided credentials or current user's saved ones
113-
server = data.get('da_server') or current_user.da_server
114-
username = data.get('da_username') or current_user.da_username
115-
password = data.get('da_password') or current_user.get_da_password()
116-
117-
if not all([server, username, password]):
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
127-
128-
# Ensure server URL is properly formatted
129-
if not server.startswith(('http://', 'https://')):
130-
server = 'https://' + server
131-
132-
# Test connection
133-
api = DirectAdminAPI(server, username, password)
134-
success, message = api.test_connection()
135-
136-
if success:
137-
return jsonify({'success': True, 'message': message})
138-
else:
139-
return jsonify({'error': message, 'success': False}), 200 # Return 200 even on test failure
140-
141-
except Exception as e:
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)