adding instances

This commit is contained in:
2025-06-09 09:54:37 +02:00
parent 7aa96119a9
commit 112a99ffcb
9 changed files with 523 additions and 14 deletions

View File

@@ -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
from models import User, db, Room, RoomFile, RoomMemberPermission, SiteSettings, Event, Conversation, Message, MessageAttachment, Notif, EmailTemplate, Mail, KeyValueSettings, DocuPulseSettings, PasswordSetupToken, Instance
from routes.auth import require_password_change
import os
from werkzeug.utils import secure_filename
@@ -339,7 +339,97 @@ def init_routes(main_bp):
if not os.environ.get('MASTER', 'false').lower() == 'true':
flash('This page is only available in master instances.', 'error')
return redirect(url_for('main.dashboard'))
return render_template('main/instances.html')
instances = Instance.query.all()
return render_template('main/instances.html', instances=instances)
@main_bp.route('/instances/add', methods=['POST'])
@login_required
@require_password_change
def add_instance():
if not os.environ.get('MASTER', 'false').lower() == 'true':
return jsonify({'error': 'Unauthorized'}), 403
data = request.get_json()
try:
instance = Instance(
name=data['name'],
company=data['company'],
payment_plan=data['payment_plan'],
main_url=data['main_url'],
status='inactive' # New instances start as inactive
)
db.session.add(instance)
db.session.commit()
return jsonify({
'message': 'Instance added successfully',
'instance': {
'id': instance.id,
'name': instance.name,
'company': instance.company,
'rooms_count': instance.rooms_count,
'conversations_count': instance.conversations_count,
'data_size': instance.data_size,
'payment_plan': instance.payment_plan,
'main_url': instance.main_url,
'status': instance.status
}
})
except Exception as e:
db.session.rollback()
return jsonify({'error': str(e)}), 400
@main_bp.route('/instances/<int:instance_id>', methods=['PUT'])
@login_required
@require_password_change
def update_instance(instance_id):
if not os.environ.get('MASTER', 'false').lower() == 'true':
return jsonify({'error': 'Unauthorized'}), 403
instance = Instance.query.get_or_404(instance_id)
data = request.get_json()
try:
instance.name = data.get('name', instance.name)
instance.company = data.get('company', instance.company)
instance.payment_plan = data.get('payment_plan', instance.payment_plan)
instance.main_url = data.get('main_url', instance.main_url)
instance.status = data.get('status', instance.status)
db.session.commit()
return jsonify({
'message': 'Instance updated successfully',
'instance': {
'id': instance.id,
'name': instance.name,
'company': instance.company,
'rooms_count': instance.rooms_count,
'conversations_count': instance.conversations_count,
'data_size': instance.data_size,
'payment_plan': instance.payment_plan,
'main_url': instance.main_url,
'status': instance.status
}
})
except Exception as e:
db.session.rollback()
return jsonify({'error': str(e)}), 400
@main_bp.route('/instances/<int:instance_id>', methods=['DELETE'])
@login_required
@require_password_change
def delete_instance(instance_id):
if not os.environ.get('MASTER', 'false').lower() == 'true':
return jsonify({'error': 'Unauthorized'}), 403
instance = Instance.query.get_or_404(instance_id)
try:
db.session.delete(instance)
db.session.commit()
return jsonify({'message': 'Instance deleted successfully'})
except Exception as e:
db.session.rollback()
return jsonify({'error': str(e)}), 400
UPLOAD_FOLDER = '/app/uploads/profile_pics'
if not os.path.exists(UPLOAD_FOLDER):