add connections
This commit is contained in:
179
routes/main.py
179
routes/main.py
@@ -1,6 +1,6 @@
|
||||
from flask import render_template, Blueprint, redirect, url_for, request, flash, Response, jsonify, session, current_app
|
||||
from flask_login import current_user, login_required
|
||||
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event, Conversation, Message, MessageAttachment, Notif, EmailTemplate, Mail, KeyValueSettings, DocuPulseSettings, PasswordSetupToken, Instance
|
||||
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event, Conversation, Message, MessageAttachment, Notif, EmailTemplate, Mail, KeyValueSettings, DocuPulseSettings, PasswordSetupToken, Instance, ManagementAPIKey
|
||||
from routes.auth import require_password_change
|
||||
import os
|
||||
from werkzeug.utils import secure_filename
|
||||
@@ -905,7 +905,7 @@ def init_routes(main_bp):
|
||||
|
||||
active_tab = request.args.get('tab', 'colors')
|
||||
# Validate tab parameter
|
||||
valid_tabs = ['colors', 'general', 'email_templates', 'mails', 'security', 'events', 'debugging', 'smtp']
|
||||
valid_tabs = ['colors', 'general', 'email_templates', 'mails', 'security', 'events', 'debugging', 'smtp', 'connections']
|
||||
if active_tab not in valid_tabs:
|
||||
active_tab = 'colors'
|
||||
|
||||
@@ -917,6 +917,20 @@ def init_routes(main_bp):
|
||||
if active_tab == 'smtp':
|
||||
smtp_settings = KeyValueSettings.get_value('smtp_settings')
|
||||
|
||||
# Get connection settings for the connections tab
|
||||
portainer_settings = None
|
||||
nginx_settings = None
|
||||
git_settings = None
|
||||
if active_tab == 'connections':
|
||||
portainer_settings = KeyValueSettings.get_value('portainer_settings')
|
||||
nginx_settings = KeyValueSettings.get_value('nginx_settings')
|
||||
git_settings = KeyValueSettings.get_value('git_settings')
|
||||
|
||||
# Get management API key for the connections tab
|
||||
management_api_key = ManagementAPIKey.query.filter_by(is_active=True).first()
|
||||
if management_api_key:
|
||||
site_settings.management_api_key = management_api_key.api_key
|
||||
|
||||
# Get events for the events tab
|
||||
events = None
|
||||
total_pages = 0
|
||||
@@ -978,6 +992,9 @@ def init_routes(main_bp):
|
||||
email_templates=email_templates,
|
||||
form=company_form,
|
||||
smtp_settings=smtp_settings,
|
||||
portainer_settings=portainer_settings,
|
||||
nginx_settings=nginx_settings,
|
||||
git_settings=git_settings,
|
||||
csrf_token=generate_csrf())
|
||||
|
||||
@main_bp.route('/settings/update-smtp', methods=['POST'])
|
||||
@@ -1493,4 +1510,160 @@ def init_routes(main_bp):
|
||||
headers={
|
||||
'Content-Disposition': f'attachment; filename=event_log_{datetime.utcnow().strftime("%Y%m%d_%H%M%S")}.csv'
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
@main_bp.route('/settings/save-portainer-connection', methods=['POST'])
|
||||
@login_required
|
||||
def save_portainer_connection():
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'error': 'Unauthorized'}), 403
|
||||
|
||||
data = request.get_json()
|
||||
url = data.get('url')
|
||||
api_key = data.get('api_key')
|
||||
|
||||
if not url or not api_key:
|
||||
return jsonify({'error': 'Missing required fields'}), 400
|
||||
|
||||
try:
|
||||
# Save Portainer settings
|
||||
KeyValueSettings.set_value('portainer_settings', {
|
||||
'url': url,
|
||||
'api_key': api_key
|
||||
})
|
||||
|
||||
return jsonify({'message': 'Settings saved successfully'})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@main_bp.route('/settings/save-nginx-connection', methods=['POST'])
|
||||
@login_required
|
||||
def save_nginx_connection():
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'error': 'Unauthorized'}), 403
|
||||
|
||||
data = request.get_json()
|
||||
url = data.get('url')
|
||||
username = data.get('username')
|
||||
password = data.get('password')
|
||||
|
||||
if not url or not username or not password:
|
||||
return jsonify({'error': 'Missing required fields'}), 400
|
||||
|
||||
try:
|
||||
# Save NGINX Proxy Manager settings
|
||||
KeyValueSettings.set_value('nginx_settings', {
|
||||
'url': url,
|
||||
'username': username,
|
||||
'password': password
|
||||
})
|
||||
|
||||
return jsonify({'message': 'Settings saved successfully'})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@main_bp.route('/settings/save-git-connection', methods=['POST'])
|
||||
@login_required
|
||||
def save_git_connection():
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'error': 'Unauthorized'}), 403
|
||||
|
||||
data = request.get_json()
|
||||
provider = data.get('provider')
|
||||
url = data.get('url')
|
||||
username = data.get('username')
|
||||
token = data.get('token')
|
||||
repo = data.get('repo')
|
||||
|
||||
if not provider or not url or not username or not token or not repo:
|
||||
return jsonify({'error': 'Missing required fields'}), 400
|
||||
|
||||
if provider not in ['gitea', 'gitlab']:
|
||||
return jsonify({'error': 'Invalid provider'}), 400
|
||||
|
||||
try:
|
||||
# Save Git settings
|
||||
KeyValueSettings.set_value('git_settings', {
|
||||
'provider': provider,
|
||||
'url': url,
|
||||
'username': username,
|
||||
'token': token,
|
||||
'repo': repo
|
||||
})
|
||||
|
||||
return jsonify({'message': 'Settings saved successfully'})
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@main_bp.route('/settings/test-git-connection', methods=['POST'])
|
||||
@login_required
|
||||
def test_git_connection():
|
||||
if not current_user.is_admin:
|
||||
return jsonify({'error': 'Unauthorized'}), 403
|
||||
|
||||
data = request.get_json()
|
||||
provider = data.get('provider')
|
||||
url = data.get('url')
|
||||
username = data.get('username')
|
||||
token = data.get('token')
|
||||
|
||||
if not provider or not url or not username or not token:
|
||||
return jsonify({'error': 'Missing required fields'}), 400
|
||||
|
||||
if provider not in ['gitea', 'gitlab']:
|
||||
return jsonify({'error': 'Invalid provider'}), 400
|
||||
|
||||
try:
|
||||
if provider == 'gitea':
|
||||
# Test Gitea connection with different authentication methods
|
||||
headers = {
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
|
||||
# First try token in Authorization header
|
||||
headers['Authorization'] = f'token {token}'
|
||||
|
||||
# Try to get user information
|
||||
response = requests.get(
|
||||
f'{url.rstrip("/")}/api/v1/user',
|
||||
headers=headers,
|
||||
timeout=5
|
||||
)
|
||||
|
||||
# If that fails, try token as query parameter
|
||||
if response.status_code != 200:
|
||||
response = requests.get(
|
||||
f'{url.rstrip("/")}/api/v1/user?token={token}',
|
||||
headers={'Accept': 'application/json'},
|
||||
timeout=5
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
return jsonify({'message': 'Connection successful'})
|
||||
else:
|
||||
return jsonify({'error': f'Connection failed: {response.json().get("message", "Unknown error")}'}), 400
|
||||
|
||||
elif provider == 'gitlab':
|
||||
# Test GitLab connection
|
||||
headers = {
|
||||
'PRIVATE-TOKEN': token,
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
|
||||
# Try to get user information
|
||||
response = requests.get(
|
||||
f'{url.rstrip("/")}/api/v4/user',
|
||||
headers=headers,
|
||||
timeout=5
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
return jsonify({'message': 'Connection successful'})
|
||||
else:
|
||||
return jsonify({'error': f'Connection failed: {response.json().get("message", "Unknown error")}'}), 400
|
||||
|
||||
except Exception as e:
|
||||
return jsonify({'error': f'Connection failed: {str(e)}'}), 400
|
||||
Reference in New Issue
Block a user