From f8e44c09ebd54704594eb1c544a98a7e3e991fa9 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Mon, 8 Nov 2021 15:22:29 -0500 Subject: [PATCH] Move CUSTOM_VALIDATORS to dynamic configuration --- docs/configuration/dynamic-settings.md | 23 +++++++++++++++++++++++ docs/configuration/optional-settings.md | 16 ---------------- netbox/extras/admin.py | 3 +++ netbox/extras/signals.py | 5 +++-- netbox/netbox/config/parameters.py | 9 +++++++++ netbox/netbox/configuration.example.py | 14 -------------- netbox/netbox/settings.py | 1 - 7 files changed, 38 insertions(+), 33 deletions(-) diff --git a/docs/configuration/dynamic-settings.md b/docs/configuration/dynamic-settings.md index bb8fb33d7..a98143045 100644 --- a/docs/configuration/dynamic-settings.md +++ b/docs/configuration/dynamic-settings.md @@ -43,6 +43,29 @@ changes in the database indefinitely. --- +## CUSTOM_VALIDATORS + +This is a mapping of models to [custom validators](../customization/custom-validation.md) that have been defined locally to enforce custom validation logic. An example is provided below: + +```python +CUSTOM_VALIDATORS = { + "dcim.site": [ + { + "name": { + "min_length": 5, + "max_length": 30 + } + }, + "my_plugin.validators.Validator1" + ], + "dim.device": [ + "my_plugin.validators.Validator1" + ] +} +``` + +--- + ## ENFORCE_GLOBAL_UNIQUE Default: False diff --git a/docs/configuration/optional-settings.md b/docs/configuration/optional-settings.md index 49a4a776b..e49968130 100644 --- a/docs/configuration/optional-settings.md +++ b/docs/configuration/optional-settings.md @@ -49,22 +49,6 @@ CORS_ORIGIN_WHITELIST = [ --- -## CUSTOM_VALIDATORS - -This is a mapping of models to [custom validators](../customization/custom-validation.md) that have been defined locally to enforce custom validation logic. An example is provided below: - -```python -CUSTOM_VALIDATORS = { - 'dcim.site': ( - Validator1, - Validator2, - Validator3 - ) -} -``` - ---- - ## DEBUG Default: False diff --git a/netbox/extras/admin.py b/netbox/extras/admin.py index a905367c5..73ffb40fc 100644 --- a/netbox/extras/admin.py +++ b/netbox/extras/admin.py @@ -27,6 +27,9 @@ class ConfigRevisionAdmin(admin.ModelAdmin): ('Pagination', { 'fields': ('PAGINATE_COUNT', 'MAX_PAGE_SIZE'), }), + ('Validation', { + 'fields': ('CUSTOM_VALIDATORS',), + }), ('NAPALM', { 'fields': ('NAPALM_USERNAME', 'NAPALM_PASSWORD', 'NAPALM_TIMEOUT', 'NAPALM_ARGS'), }), diff --git a/netbox/extras/signals.py b/netbox/extras/signals.py index 99bc91236..77931f268 100644 --- a/netbox/extras/signals.py +++ b/netbox/extras/signals.py @@ -1,13 +1,13 @@ import importlib import logging -from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.db.models.signals import m2m_changed, post_save, pre_delete from django.dispatch import receiver, Signal from django_prometheus.models import model_deletes, model_inserts, model_updates from extras.validators import CustomValidator +from netbox.config import get_config from netbox.signals import post_clean from .choices import ObjectChangeActionChoices from .models import ConfigRevision, CustomField, ObjectChange @@ -159,8 +159,9 @@ m2m_changed.connect(handle_cf_removed_obj_types, sender=CustomField.content_type @receiver(post_clean) def run_custom_validators(sender, instance, **kwargs): + config = get_config() model_name = f'{sender._meta.app_label}.{sender._meta.model_name}' - validators = settings.CUSTOM_VALIDATORS.get(model_name, []) + validators = config.CUSTOM_VALIDATORS.get(model_name, []) for validator in validators: diff --git a/netbox/netbox/config/parameters.py b/netbox/netbox/config/parameters.py index 7b9f6a1f7..1be664b28 100644 --- a/netbox/netbox/config/parameters.py +++ b/netbox/netbox/config/parameters.py @@ -94,6 +94,15 @@ PARAMS = ( field=forms.IntegerField ), + # Validation + ConfigParam( + name='CUSTOM_VALIDATORS', + label='Custom validators', + default={}, + description="Custom validation rules (JSON)", + field=forms.JSONField + ), + # NAPALM ConfigParam( name='NAPALM_USERNAME', diff --git a/netbox/netbox/configuration.example.py b/netbox/netbox/configuration.example.py index 947ed6d53..48885f844 100644 --- a/netbox/netbox/configuration.example.py +++ b/netbox/netbox/configuration.example.py @@ -87,20 +87,6 @@ CORS_ORIGIN_REGEX_WHITELIST = [ # r'^(https?://)?(\w+\.)?example\.com$', ] -# Specify any custom validators here, as a mapping of model to a list of validators classes. Validators should be -# instances of or inherit from CustomValidator. -# from extras.validators import CustomValidator -CUSTOM_VALIDATORS = { - # 'dcim.site': [ - # CustomValidator({ - # 'name': { - # 'min_length': 10, - # 'regex': r'\d{3}$', - # } - # }) - # ], -} - # Set to True to enable server debugging. WARNING: Debugging introduces a substantial performance penalty and may reveal # sensitive information about your installation. Only enable debugging while performing testing. Never enable debugging # on a production system. diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index c91a5e75b..529da77dc 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -83,7 +83,6 @@ if BASE_PATH: CORS_ORIGIN_ALLOW_ALL = getattr(configuration, 'CORS_ORIGIN_ALLOW_ALL', False) CORS_ORIGIN_REGEX_WHITELIST = getattr(configuration, 'CORS_ORIGIN_REGEX_WHITELIST', []) CORS_ORIGIN_WHITELIST = getattr(configuration, 'CORS_ORIGIN_WHITELIST', []) -CUSTOM_VALIDATORS = getattr(configuration, 'CUSTOM_VALIDATORS', {}) DATE_FORMAT = getattr(configuration, 'DATE_FORMAT', 'N j, Y') DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a') DEBUG = getattr(configuration, 'DEBUG', False)