Initial work on #10244: Protection rules (#14097)

This commit is contained in:
Jeremy Stretch
2023-10-30 14:36:56 -04:00
committed by GitHub
parent c4e765c4a8
commit edc4a35296
8 changed files with 183 additions and 13 deletions

View File

@@ -1,15 +1,38 @@
from django.core.exceptions import ValidationError
from django.core import validators
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _
# NOTE: As this module may be imported by configuration.py, we cannot import
# anything from NetBox itself.
class IsEqualValidator(validators.BaseValidator):
"""
Employed by CustomValidator to require a specific value.
"""
message = _("Ensure this value is equal to %(limit_value)s.")
code = "is_equal"
def compare(self, a, b):
return a != b
class IsNotEqualValidator(validators.BaseValidator):
"""
Employed by CustomValidator to exclude a specific value.
"""
message = _("Ensure this value does not equal %(limit_value)s.")
code = "is_not_equal"
def compare(self, a, b):
return a == b
class IsEmptyValidator:
"""
Employed by CustomValidator to enforce required fields.
"""
message = "This field must be empty."
message = _("This field must be empty.")
code = 'is_empty'
def __init__(self, enforce=True):
@@ -24,7 +47,7 @@ class IsNotEmptyValidator:
"""
Employed by CustomValidator to enforce prohibited fields.
"""
message = "This field must not be empty."
message = _("This field must not be empty.")
code = 'not_empty'
def __init__(self, enforce=True):
@@ -50,6 +73,8 @@ class CustomValidator:
:param validation_rules: A dictionary mapping object attributes to validation rules
"""
VALIDATORS = {
'eq': IsEqualValidator,
'neq': IsNotEqualValidator,
'min': validators.MinValueValidator,
'max': validators.MaxValueValidator,
'min_length': validators.MinLengthValidator,