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

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.
parent 05937d53
No related branches found
No related tags found
No related merge requests found
...@@ -10,13 +10,11 @@ This module customizes the admin views. ...@@ -10,13 +10,11 @@ This module customizes the admin views.
""" """
from wtforms import validators, fields from wtforms import validators, fields
from flask import current_app, flash
from flask_admin.contrib import sqla from flask_admin.contrib import sqla
from flask_admin.model.form import converts from flask_admin.model.form import converts
from flask_login import current_user from flask_login import current_user
from .validators import IPNetwork from .validators import IPNetwork
from ..models import ICS_ID_RE from ..models import ICS_ID_RE
from ..utils import CSEntryError
# Monkey patch flask-admin Unique validator to disable it # Monkey patch flask-admin Unique validator to disable it
...@@ -48,16 +46,6 @@ class AdminModelView(sqla.ModelView): ...@@ -48,16 +46,6 @@ class AdminModelView(sqla.ModelView):
def is_accessible(self): def is_accessible(self):
return current_user.is_authenticated and current_user.is_admin 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): class GroupAdmin(AdminModelView):
can_create = False can_create = False
......
...@@ -20,6 +20,7 @@ from sqlalchemy_continuum import make_versioned, version_class ...@@ -20,6 +20,7 @@ from sqlalchemy_continuum import make_versioned, version_class
from citext import CIText from citext import CIText
from flask import current_app from flask import current_app
from flask_login import UserMixin from flask_login import UserMixin
from wtforms import ValidationError
from .extensions import db, login_manager, ldap_manager, jwt from .extensions import db, login_manager, ldap_manager, jwt
from .plugins import FlaskUserPlugin from .plugins import FlaskUserPlugin
from . import utils from . import utils
...@@ -235,7 +236,7 @@ class Item(db.Model): ...@@ -235,7 +236,7 @@ class Item(db.Model):
"""Ensure the ICS id field matches the required format""" """Ensure the ICS id field matches the required format"""
if string is not None: if string is not None:
if ICS_ID_RE.fullmatch(string) is 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 return string
def to_dict(self, long=False): def to_dict(self, long=False):
...@@ -304,12 +305,12 @@ class Network(db.Model): ...@@ -304,12 +305,12 @@ class Network(db.Model):
:param str user_id: unicode ID of a user :param str user_id: unicode ID of a user
:returns: a tuple with the IP and network as (IPv4Address, IPv4Network) :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) addr = ipaddress.ip_address(ip)
net = ipaddress.ip_network(prefix) net = ipaddress.ip_network(prefix)
if addr not in net: 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) return (addr, net)
@validates('first') @validates('first')
...@@ -323,7 +324,7 @@ class Network(db.Model): ...@@ -323,7 +324,7 @@ class Network(db.Model):
"""Ensure the last IP is in the network""" """Ensure the last IP is in the network"""
addr, net = self.ip_in_network(ip, self.prefix) addr, net = self.ip_in_network(ip, self.prefix)
if addr < ipaddress.ip_address(self.first): 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 return ip
@validates('hosts') @validates('hosts')
...@@ -331,7 +332,7 @@ class Network(db.Model): ...@@ -331,7 +332,7 @@ class Network(db.Model):
"""Ensure the host IP is in the network range""" """Ensure the host IP is in the network range"""
addr, net = self.ip_in_network(host.ip, self.prefix) addr, net = self.ip_in_network(host.ip, self.prefix)
if addr < ipaddress.ip_address(self.first) or addr > ipaddress.ip_address(self.last): 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 return host
def to_dict(self): def to_dict(self):
......
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