launch process completed!
This commit is contained in:
@@ -973,4 +973,221 @@ def apply_company_information():
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error applying company information: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@launch_api.route('/apply-colors', methods=['POST'])
|
||||
@csrf.exempt
|
||||
def apply_colors():
|
||||
"""Apply colors to a launched instance"""
|
||||
try:
|
||||
if not request.is_json:
|
||||
return jsonify({'error': 'Request must be JSON'}), 400
|
||||
|
||||
data = request.get_json()
|
||||
required_fields = ['instance_url', 'colors_data']
|
||||
|
||||
if not all(field in data for field in required_fields):
|
||||
missing_fields = [field for field in required_fields if field not in data]
|
||||
return jsonify({'error': f'Missing required fields: {", ".join(missing_fields)}'}), 400
|
||||
|
||||
instance_url = data['instance_url']
|
||||
colors_data = data['colors_data']
|
||||
|
||||
# Get the instance from database to get the connection token
|
||||
instance = Instance.query.filter_by(main_url=instance_url).first()
|
||||
|
||||
if not instance:
|
||||
return jsonify({'error': 'Instance not found in database'}), 404
|
||||
|
||||
if not instance.connection_token:
|
||||
return jsonify({'error': 'Instance not authenticated'}), 400
|
||||
|
||||
# First get JWT token from the instance
|
||||
jwt_response = requests.post(
|
||||
f"{instance_url.rstrip('/')}/api/admin/management-token",
|
||||
headers={
|
||||
'X-API-Key': instance.connection_token,
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if jwt_response.status_code != 200:
|
||||
return jsonify({'error': f'Failed to get JWT token: {jwt_response.text}'}), 400
|
||||
|
||||
jwt_data = jwt_response.json()
|
||||
jwt_token = jwt_data.get('token')
|
||||
|
||||
if not jwt_token:
|
||||
return jsonify({'error': 'No JWT token received'}), 400
|
||||
|
||||
# Prepare colors data for the API
|
||||
api_colors_data = {
|
||||
'primary_color': colors_data.get('primary'),
|
||||
'secondary_color': colors_data.get('secondary')
|
||||
}
|
||||
|
||||
# Apply colors to the instance
|
||||
colors_response = requests.put(
|
||||
f"{instance_url.rstrip('/')}/api/admin/settings",
|
||||
headers={
|
||||
'Authorization': f'Bearer {jwt_token}',
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
json=api_colors_data,
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if colors_response.status_code != 200:
|
||||
return jsonify({'error': f'Failed to apply colors: {colors_response.text}'}), 400
|
||||
|
||||
return jsonify({
|
||||
'message': 'Colors applied successfully',
|
||||
'data': {
|
||||
'primary_color': colors_data.get('primary'),
|
||||
'secondary_color': colors_data.get('secondary'),
|
||||
'instance_url': instance_url
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error applying colors: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@launch_api.route('/update-admin-credentials', methods=['POST'])
|
||||
@csrf.exempt
|
||||
def update_admin_credentials():
|
||||
"""Update admin credentials on a launched instance"""
|
||||
try:
|
||||
if not request.is_json:
|
||||
return jsonify({'error': 'Request must be JSON'}), 400
|
||||
|
||||
data = request.get_json()
|
||||
required_fields = ['instance_url', 'email']
|
||||
|
||||
if not all(field in data for field in required_fields):
|
||||
missing_fields = [field for field in required_fields if field not in data]
|
||||
return jsonify({'error': f'Missing required fields: {", ".join(missing_fields)}'}), 400
|
||||
|
||||
instance_url = data['instance_url']
|
||||
email = data['email']
|
||||
|
||||
# Get the instance from database to get the connection token
|
||||
instance = Instance.query.filter_by(main_url=instance_url).first()
|
||||
|
||||
if not instance:
|
||||
return jsonify({'error': 'Instance not found in database'}), 404
|
||||
|
||||
if not instance.connection_token:
|
||||
return jsonify({'error': 'Instance not authenticated'}), 400
|
||||
|
||||
# First get JWT token from the instance
|
||||
jwt_response = requests.post(
|
||||
f"{instance_url.rstrip('/')}/api/admin/management-token",
|
||||
headers={
|
||||
'X-API-Key': instance.connection_token,
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if jwt_response.status_code != 200:
|
||||
return jsonify({'error': f'Failed to get JWT token: {jwt_response.text}'}), 400
|
||||
|
||||
jwt_data = jwt_response.json()
|
||||
jwt_token = jwt_data.get('token')
|
||||
|
||||
if not jwt_token:
|
||||
return jsonify({'error': 'No JWT token received'}), 400
|
||||
|
||||
# Generate a secure password
|
||||
import secrets
|
||||
import string
|
||||
alphabet = string.ascii_letters + string.digits + "!@#$%^&*"
|
||||
new_password = ''.join(secrets.choice(alphabet) for i in range(16))
|
||||
|
||||
# First, login with default credentials to get admin token
|
||||
login_response = requests.post(
|
||||
f"{instance_url.rstrip('/')}/api/admin/login",
|
||||
headers={
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
json={
|
||||
'email': 'administrator@docupulse.com',
|
||||
'password': 'changeme'
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if login_response.status_code != 200:
|
||||
return jsonify({'error': f'Failed to login with default credentials: {login_response.text}'}), 400
|
||||
|
||||
login_data = login_response.json()
|
||||
if login_data.get('status') != 'success' or not login_data.get('token'):
|
||||
return jsonify({'error': 'Failed to get admin token'}), 400
|
||||
|
||||
admin_token = login_data.get('token')
|
||||
|
||||
# Get the admin user ID first
|
||||
users_response = requests.get(
|
||||
f"{instance_url.rstrip('/')}/api/admin/contacts",
|
||||
headers={
|
||||
'Authorization': f'Bearer {admin_token}',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if users_response.status_code != 200:
|
||||
return jsonify({'error': f'Failed to get users: {users_response.text}'}), 400
|
||||
|
||||
users_data = users_response.json()
|
||||
admin_user = None
|
||||
|
||||
# Find the administrator user
|
||||
for user in users_data:
|
||||
if user.get('email') == 'administrator@docupulse.com':
|
||||
admin_user = user
|
||||
break
|
||||
|
||||
if not admin_user:
|
||||
return jsonify({'error': 'Administrator user not found'}), 404
|
||||
|
||||
admin_user_id = admin_user.get('id')
|
||||
|
||||
# Update the admin user with new email and password
|
||||
update_response = requests.put(
|
||||
f"{instance_url.rstrip('/')}/api/admin/contacts/{admin_user_id}",
|
||||
headers={
|
||||
'Authorization': f'Bearer {admin_token}',
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
json={
|
||||
'email': email,
|
||||
'password': new_password,
|
||||
'username': 'administrator',
|
||||
'last_name': 'Administrator',
|
||||
'role': 'admin'
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if update_response.status_code != 200:
|
||||
return jsonify({'error': f'Failed to update admin credentials: {update_response.text}'}), 400
|
||||
|
||||
return jsonify({
|
||||
'message': 'Admin credentials updated successfully',
|
||||
'data': {
|
||||
'email': email,
|
||||
'password': new_password,
|
||||
'username': 'administrator',
|
||||
'instance_url': instance_url
|
||||
}
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error updating admin credentials: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
Reference in New Issue
Block a user