- Corrections to tenant, catalog, and tenant_make

- Clean-up of tenant elements
- ensure the chat_client get's it's initial call rifht.
This commit is contained in:
Josako
2025-06-10 16:10:08 +02:00
parent 3f77871c4f
commit 9cc266b97f
13 changed files with 182 additions and 38 deletions

View File

@@ -3,13 +3,32 @@ from flask import Blueprint, render_template, request, session, current_app, jso
from sqlalchemy.exc import SQLAlchemyError
from common.extensions import db
from common.models.user import Tenant, SpecialistMagicLinkTenant
from common.models.user import Tenant, SpecialistMagicLinkTenant, TenantMake
from common.models.interaction import SpecialistMagicLink, Specialist, ChatSession, Interaction
from common.services.interaction.specialist_services import SpecialistServices
from common.utils.database import Database
from common.utils.chat_utils import get_default_chat_customisation
chat_bp = Blueprint('chat', __name__)
chat_bp = Blueprint('chat_bp', __name__, url_prefix='/chat')
@chat_bp.before_request
def log_before_request():
current_app.logger.debug(f'Before request: {request.path} =====================================')
@chat_bp.after_request
def log_after_request(response):
return response
# @chat_bp.before_request
# def before_request():
# try:
# mw_before_request()
# except Exception as e:
# current_app.logger.error(f'Error switching schema in Document Blueprint: {e}')
# raise
@chat_bp.route('/')
def index():
@@ -31,14 +50,12 @@ def chat(magic_link_code):
current_app.logger.error(f"Invalid magic link code: {magic_link_code}")
return render_template('error.html', message="Invalid magic link code.")
tenant_id = magic_link_tenant.tenant_id
# Get tenant information
tenant_id = magic_link_tenant.tenant_id
tenant = Tenant.query.get(tenant_id)
if not tenant:
current_app.logger.error(f"Tenant not found for ID: {tenant_id}")
return render_template('error.html', message="Tenant not found.")
# Switch to tenant schema
Database(tenant_id).switch_schema()
@@ -48,6 +65,12 @@ def chat(magic_link_code):
current_app.logger.error(f"Specialist magic link not found in tenant schema: {tenant_id}")
return render_template('error.html', message="Specialist configuration not found.")
# Get relevant TenantMake
tenant_make = TenantMake.query.get(specialist_ml.tenant_make_id)
if not tenant_make:
current_app.logger.error(f"Tenant make not found: {specialist_ml.tenant_make_id}")
return render_template('error.html', message="Tenant make not found.")
# Get specialist details
specialist = Specialist.query.get(specialist_ml.specialist_id)
if not specialist:
@@ -55,13 +78,13 @@ def chat(magic_link_code):
return render_template('error.html', message="Specialist not found.")
# Store necessary information in session
session['tenant_id'] = tenant_id
session['specialist_id'] = specialist_ml.specialist_id
session['specialist_args'] = specialist_ml.specialist_args or {}
session['magic_link_code'] = magic_link_code
session['tenant'] = tenant.to_dict()
session['specialist'] = specialist.to_dict()
session['magic_link'] = specialist_ml.to_dict()
session['tenant_make'] = tenant_make.to_dict()
# Get customisation options with defaults
customisation = get_default_chat_customisation(tenant.chat_customisation_options)
customisation = get_default_chat_customisation(tenant_make.chat_customisation_options)
# Start a new chat session
session['chat_session_id'] = SpecialistServices.start_session()