From 642a9b8c2331c0acb44a0190117d0d24f000ec63 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Mon, 13 Nov 2017 21:21:51 +0100 Subject: [PATCH] Replace CSEntryError with ValidationError in models ValidationError is catched by default by flask-admin. It's easy to catch in the api/main.py to raise a CSEntryError. We actually don't need to catch ValidationError as it's based on ValueError that we already catch. --- app/admin/views.py | 12 ------------ app/models.py | 11 ++++++----- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/app/admin/views.py b/app/admin/views.py index 2a10fd1..15981da 100644 --- a/app/admin/views.py +++ b/app/admin/views.py @@ -10,13 +10,11 @@ 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 @@ -48,16 +46,6 @@ 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 diff --git a/app/models.py b/app/models.py index a643ab0..d711617 100644 --- a/app/models.py +++ b/app/models.py @@ -20,6 +20,7 @@ from sqlalchemy_continuum import make_versioned, version_class from citext import CIText from flask import current_app from flask_login import UserMixin +from wtforms import ValidationError from .extensions import db, login_manager, ldap_manager, jwt from .plugins import FlaskUserPlugin from . import utils @@ -235,7 +236,7 @@ class Item(db.Model): """Ensure the ICS id field matches the required format""" if string is not None: if ICS_ID_RE.fullmatch(string) is None: - raise utils.CSEntryError('ICS id shall match [A-Z]{3}[0-9]{3}', status_code=422) + raise ValidationError('ICS id shall match [A-Z]{3}[0-9]{3}') return string def to_dict(self, long=False): @@ -304,12 +305,12 @@ class Network(db.Model): :param str user_id: unicode ID of a user :returns: a tuple with the IP and network as (IPv4Address, IPv4Network) - :raises: CSEntryError if the IP is not in the network + :raises: ValidationError if the IP is not in the network """ addr = ipaddress.ip_address(ip) net = ipaddress.ip_network(prefix) if addr not in net: - raise utils.CSEntryError(f'IP address {ip} is not in network {prefix}', status_code=422) + raise ValidationError(f'IP address {ip} is not in network {prefix}') return (addr, net) @validates('first') @@ -323,7 +324,7 @@ class Network(db.Model): """Ensure the last IP is in the network""" addr, net = self.ip_in_network(ip, self.prefix) if addr < ipaddress.ip_address(self.first): - raise utils.CSEntryError(f'Last IP address {ip} is less than the first address {self.first}', status_code=422) + raise ValidationError(f'Last IP address {ip} is less than the first address {self.first}') return ip @validates('hosts') @@ -331,7 +332,7 @@ class Network(db.Model): """Ensure the host IP is in the network range""" addr, net = self.ip_in_network(host.ip, self.prefix) if addr < ipaddress.ip_address(self.first) or addr > ipaddress.ip_address(self.last): - raise utils.CSEntryError(f'IP address {host.ip} is not in range {self.first} - {self.last}', status_code=422) + raise ValidationError(f'IP address {host.ip} is not in range {self.first} - {self.last}') return host def to_dict(self): -- GitLab