Просмотр исходного кода

Move CUSTOM_VALIDATORS to dynamic configuration

jeremystretch 4 лет назад
Родитель
Сommit
f8e44c09eb

+ 23 - 0
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

+ 0 - 16
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

+ 3 - 0
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'),
         }),

+ 3 - 2
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:
 

+ 9 - 0
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',

+ 0 - 14
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.

+ 0 - 1
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)