- Add Specialist Magic Links
- correction of some bugs: - dynamic fields for adding documents / urls to dossier catalog - tabs in latest bootstrap version no longer functional - partner association of license tier not working when no partner selected - data-type dynamic field needs conversion to isoformat - Add public tables to env.py of tenant schema
This commit is contained in:
@@ -215,3 +215,24 @@ class SpecialistDispatcher(db.Model):
|
||||
dispatcher_id = db.Column(db.Integer, db.ForeignKey(Dispatcher.id, ondelete='CASCADE'), primary_key=True)
|
||||
|
||||
dispatcher = db.relationship("Dispatcher", backref="specialist_dispatchers")
|
||||
|
||||
|
||||
class SpecialistMagicLink(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(50), nullable=False)
|
||||
description = db.Column(db.Text, nullable=True)
|
||||
specialist_id = db.Column(db.Integer, db.ForeignKey(Specialist.id, ondelete='CASCADE'), nullable=False)
|
||||
magic_link_code = db.Column(db.String(55), nullable=False, unique=True)
|
||||
|
||||
valid_from = db.Column(db.DateTime, nullable=True)
|
||||
valid_to = db.Column(db.DateTime, nullable=True)
|
||||
|
||||
specialist_args = db.Column(JSONB, nullable=True)
|
||||
|
||||
created_at = db.Column(db.DateTime, nullable=False, server_default=db.func.now())
|
||||
created_by = db.Column(db.Integer, db.ForeignKey(User.id), nullable=True)
|
||||
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.func.now(), onupdate=db.func.now())
|
||||
updated_by = db.Column(db.Integer, db.ForeignKey(User.id))
|
||||
|
||||
def __repr__(self):
|
||||
return f"<SpecialistMagicLink {self.specialist_id} {self.magic_link_code}>"
|
||||
|
||||
@@ -271,3 +271,13 @@ class PartnerTenant(db.Model):
|
||||
created_by = db.Column(db.Integer, db.ForeignKey('public.user.id'), nullable=True)
|
||||
updated_at = db.Column(db.DateTime, nullable=False, server_default=db.func.now(), onupdate=db.func.now())
|
||||
updated_by = db.Column(db.Integer, db.ForeignKey('public.user.id'), nullable=True)
|
||||
|
||||
|
||||
class SpecialistMagicLinkTenant(db.Model):
|
||||
__bind_key__ = 'public'
|
||||
__table_args__ = {'schema': 'public'}
|
||||
|
||||
magic_link_code = db.Column(db.String(55), primary_key=True)
|
||||
tenant_id = db.Column(db.Integer, db.ForeignKey('public.tenant.id'), nullable=False)
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ from sqlalchemy.exc import SQLAlchemyError
|
||||
from common.extensions import db
|
||||
from common.models.entitlements import PartnerServiceLicenseTier
|
||||
from common.models.user import Partner
|
||||
from common.utils.eveai_exceptions import EveAINoManagementPartnerService
|
||||
from common.utils.eveai_exceptions import EveAINoManagementPartnerService, EveAINoSessionPartner
|
||||
from common.utils.model_logging_utils import set_logging_information
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class LicenseTierServices:
|
||||
# Get partner service (MANAGEMENT_SERVICE type)
|
||||
partner = Partner.query.get(partner_id)
|
||||
if not partner:
|
||||
return
|
||||
raise EveAINoSessionPartner()
|
||||
|
||||
# Find a management service for this partner
|
||||
management_service = next((service for service in session['partner']['services']
|
||||
|
||||
@@ -28,7 +28,7 @@ class TenantServices:
|
||||
if service.get('type') == 'MANAGEMENT_SERVICE'), None)
|
||||
|
||||
if not management_service:
|
||||
current_app.logger.error(f"No Management Service defined for partner {partner_id}"
|
||||
current_app.logger.error(f"No Management Service defined for partner {partner_id} "
|
||||
f"while associating tenant {tenant_id} with partner.")
|
||||
raise EveAINoManagementPartnerService()
|
||||
|
||||
|
||||
135
common/utils/errors.py
Normal file
135
common/utils/errors.py
Normal file
@@ -0,0 +1,135 @@
|
||||
|
||||
import traceback
|
||||
|
||||
import jinja2
|
||||
from flask import render_template, request, jsonify, redirect, current_app, flash
|
||||
from flask_login import current_user
|
||||
|
||||
from common.utils.eveai_exceptions import EveAINoSessionTenant
|
||||
from common.utils.nginx_utils import prefixed_url_for
|
||||
|
||||
|
||||
def not_found_error(error):
|
||||
if not current_user.is_authenticated:
|
||||
return redirect(prefixed_url_for('security.login'))
|
||||
current_app.logger.error(f"Not Found Error: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error/404.html'), 404
|
||||
|
||||
|
||||
def internal_server_error(error):
|
||||
if not current_user.is_authenticated:
|
||||
return redirect(prefixed_url_for('security.login'))
|
||||
current_app.logger.error(f"Internal Server Error: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error/500.html'), 500
|
||||
|
||||
|
||||
def not_authorised_error(error):
|
||||
if not current_user.is_authenticated:
|
||||
return redirect(prefixed_url_for('security.login'))
|
||||
current_app.logger.error(f"Not Authorised Error: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error/401.html')
|
||||
|
||||
|
||||
def access_forbidden(error):
|
||||
if not current_user.is_authenticated:
|
||||
return redirect(prefixed_url_for('security.login'))
|
||||
current_app.logger.error(f"Access Forbidden: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error/403.html')
|
||||
|
||||
|
||||
def key_error_handler(error):
|
||||
# Check if the KeyError is specifically for 'tenant'
|
||||
if str(error) == "'tenant'":
|
||||
return redirect(prefixed_url_for('security.login'))
|
||||
# For other KeyErrors, you might want to log the error and return a generic error page
|
||||
current_app.logger.error(f"Key Error: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error/generic.html', error_message="An unexpected error occurred"), 500
|
||||
|
||||
|
||||
def attribute_error_handler(error):
|
||||
"""Handle AttributeError exceptions.
|
||||
|
||||
Specifically catches SQLAlchemy relationship errors when string IDs
|
||||
are used instead of model instances.
|
||||
"""
|
||||
error_msg = str(error)
|
||||
current_app.logger.error(f"AttributeError: {error_msg}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
|
||||
# Handle the SQLAlchemy relationship error specifically
|
||||
if "'str' object has no attribute '_sa_instance_state'" in error_msg:
|
||||
flash('Database relationship error. Please check your form inputs and try again.', 'error')
|
||||
return render_template('error/500.html',
|
||||
error_type="Relationship Error",
|
||||
error_details="A string value was provided where a database object was expected."), 500
|
||||
|
||||
# Handle other AttributeErrors
|
||||
flash('An application error occurred. The technical team has been notified.', 'error')
|
||||
return render_template('error/500.html',
|
||||
error_type="Attribute Error",
|
||||
error_details=error_msg), 500
|
||||
|
||||
|
||||
def no_tenant_selected_error(error):
|
||||
"""Handle errors when no tenant is selected in the current session.
|
||||
|
||||
This typically happens when a session expires or becomes invalid after
|
||||
a long period of inactivity. The user will be redirected to the login page.
|
||||
"""
|
||||
current_app.logger.error(f"No Session Tenant Error: {error}")
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
flash('Your session expired. You will have to re-enter your credentials', 'warning')
|
||||
|
||||
# Perform logout if user is authenticated
|
||||
if current_user.is_authenticated:
|
||||
from flask_security.utils import logout_user
|
||||
logout_user()
|
||||
|
||||
# Redirect to login page
|
||||
return redirect(prefixed_url_for('security.login'))
|
||||
|
||||
|
||||
def general_exception(e):
|
||||
current_app.logger.error(f"Unhandled Exception: {e}", exc_info=True)
|
||||
flash('An application error occurred. The technical team has been notified.', 'error')
|
||||
return render_template('error/500.html',
|
||||
error_type=type(e).__name__,
|
||||
error_details=str(e)), 500
|
||||
|
||||
|
||||
def template_not_found_error(error):
|
||||
"""Handle Jinja2 TemplateNotFound exceptions."""
|
||||
current_app.logger.error(f'Template not found: {error.name}')
|
||||
current_app.logger.error(f'Search Paths: {current_app.jinja_loader.list_templates()}')
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error/500.html',
|
||||
error_type="Template Not Found",
|
||||
error_details=f"Template '{error.name}' could not be found."), 404
|
||||
|
||||
|
||||
def template_syntax_error(error):
|
||||
"""Handle Jinja2 TemplateSyntaxError exceptions."""
|
||||
current_app.logger.error(f'Template syntax error: {error.message}')
|
||||
current_app.logger.error(f'In template {error.filename}, line {error.lineno}')
|
||||
current_app.logger.error(traceback.format_exc())
|
||||
return render_template('error/500.html',
|
||||
error_type="Template Syntax Error",
|
||||
error_details=f"Error in template '{error.filename}' at line {error.lineno}: {error.message}"), 500
|
||||
|
||||
|
||||
def register_error_handlers(app):
|
||||
app.register_error_handler(404, not_found_error)
|
||||
app.register_error_handler(500, internal_server_error)
|
||||
app.register_error_handler(401, not_authorised_error)
|
||||
app.register_error_handler(403, not_authorised_error)
|
||||
app.register_error_handler(EveAINoSessionTenant, no_tenant_selected_error)
|
||||
app.register_error_handler(KeyError, key_error_handler)
|
||||
app.register_error_handler(AttributeError, attribute_error_handler)
|
||||
app.register_error_handler(jinja2.TemplateNotFound, template_not_found_error)
|
||||
app.register_error_handler(jinja2.TemplateSyntaxError, template_syntax_error)
|
||||
app.register_error_handler(Exception, general_exception)
|
||||
Reference in New Issue
Block a user