Skip to content
Snippets Groups Projects
Commit e1ba3816 authored by Benjamin Bertrand's avatar Benjamin Bertrand
Browse files

Handle CSEntry exceptions in admin views

Allow to use the same CSEntryError exceptions used for validation
in the API and admin views.
parent b4cf45ee
No related branches found
No related tags found
No related merge requests found
......@@ -10,11 +10,13 @@ This module customizes the admin views.
"""
from wtforms import validators, fields
from flask import current_app, flash
from flask_admin.contrib import sqla
from flask_admin.model.form import converts
from flask_login import current_user
from .validators import IPNetwork
from ..models import ICS_ID_RE
from ..utils import CSEntryError
# Monkey patch flask-admin Unique validator to disable it
......@@ -46,6 +48,16 @@ class AdminModelView(sqla.ModelView):
def is_accessible(self):
return current_user.is_authenticated and current_user.is_admin
def handle_view_exception(self, exc):
"""Handle CSEntryError exceptions in admin views"""
if isinstance(exc, CSEntryError):
if current_app.config.get('ADMIN_RAISE_ON_VIEW_EXCEPTION'):
raise
else:
flash(f'CSEntry error: {exc.message}', 'error')
return True
return super().handle_view_exception(exc)
class GroupAdmin(AdminModelView):
can_create = False
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment