mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 09:51:22 -06:00
Move CUSTOM_VALIDATORS to dynamic configuration
This commit is contained in:
parent
2a00519b93
commit
f8e44c09eb
@ -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
|
## ENFORCE_GLOBAL_UNIQUE
|
||||||
|
|
||||||
Default: False
|
Default: False
|
||||||
|
@ -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
|
## DEBUG
|
||||||
|
|
||||||
Default: False
|
Default: False
|
||||||
|
@ -27,6 +27,9 @@ class ConfigRevisionAdmin(admin.ModelAdmin):
|
|||||||
('Pagination', {
|
('Pagination', {
|
||||||
'fields': ('PAGINATE_COUNT', 'MAX_PAGE_SIZE'),
|
'fields': ('PAGINATE_COUNT', 'MAX_PAGE_SIZE'),
|
||||||
}),
|
}),
|
||||||
|
('Validation', {
|
||||||
|
'fields': ('CUSTOM_VALIDATORS',),
|
||||||
|
}),
|
||||||
('NAPALM', {
|
('NAPALM', {
|
||||||
'fields': ('NAPALM_USERNAME', 'NAPALM_PASSWORD', 'NAPALM_TIMEOUT', 'NAPALM_ARGS'),
|
'fields': ('NAPALM_USERNAME', 'NAPALM_PASSWORD', 'NAPALM_TIMEOUT', 'NAPALM_ARGS'),
|
||||||
}),
|
}),
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
import importlib
|
import importlib
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.db.models.signals import m2m_changed, post_save, pre_delete
|
from django.db.models.signals import m2m_changed, post_save, pre_delete
|
||||||
from django.dispatch import receiver, Signal
|
from django.dispatch import receiver, Signal
|
||||||
from django_prometheus.models import model_deletes, model_inserts, model_updates
|
from django_prometheus.models import model_deletes, model_inserts, model_updates
|
||||||
|
|
||||||
from extras.validators import CustomValidator
|
from extras.validators import CustomValidator
|
||||||
|
from netbox.config import get_config
|
||||||
from netbox.signals import post_clean
|
from netbox.signals import post_clean
|
||||||
from .choices import ObjectChangeActionChoices
|
from .choices import ObjectChangeActionChoices
|
||||||
from .models import ConfigRevision, CustomField, ObjectChange
|
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)
|
@receiver(post_clean)
|
||||||
def run_custom_validators(sender, instance, **kwargs):
|
def run_custom_validators(sender, instance, **kwargs):
|
||||||
|
config = get_config()
|
||||||
model_name = f'{sender._meta.app_label}.{sender._meta.model_name}'
|
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:
|
for validator in validators:
|
||||||
|
|
||||||
|
@ -94,6 +94,15 @@ PARAMS = (
|
|||||||
field=forms.IntegerField
|
field=forms.IntegerField
|
||||||
),
|
),
|
||||||
|
|
||||||
|
# Validation
|
||||||
|
ConfigParam(
|
||||||
|
name='CUSTOM_VALIDATORS',
|
||||||
|
label='Custom validators',
|
||||||
|
default={},
|
||||||
|
description="Custom validation rules (JSON)",
|
||||||
|
field=forms.JSONField
|
||||||
|
),
|
||||||
|
|
||||||
# NAPALM
|
# NAPALM
|
||||||
ConfigParam(
|
ConfigParam(
|
||||||
name='NAPALM_USERNAME',
|
name='NAPALM_USERNAME',
|
||||||
|
@ -87,20 +87,6 @@ CORS_ORIGIN_REGEX_WHITELIST = [
|
|||||||
# r'^(https?://)?(\w+\.)?example\.com$',
|
# 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
|
# 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
|
# sensitive information about your installation. Only enable debugging while performing testing. Never enable debugging
|
||||||
# on a production system.
|
# on a production system.
|
||||||
|
@ -83,7 +83,6 @@ if BASE_PATH:
|
|||||||
CORS_ORIGIN_ALLOW_ALL = getattr(configuration, 'CORS_ORIGIN_ALLOW_ALL', False)
|
CORS_ORIGIN_ALLOW_ALL = getattr(configuration, 'CORS_ORIGIN_ALLOW_ALL', False)
|
||||||
CORS_ORIGIN_REGEX_WHITELIST = getattr(configuration, 'CORS_ORIGIN_REGEX_WHITELIST', [])
|
CORS_ORIGIN_REGEX_WHITELIST = getattr(configuration, 'CORS_ORIGIN_REGEX_WHITELIST', [])
|
||||||
CORS_ORIGIN_WHITELIST = getattr(configuration, 'CORS_ORIGIN_WHITELIST', [])
|
CORS_ORIGIN_WHITELIST = getattr(configuration, 'CORS_ORIGIN_WHITELIST', [])
|
||||||
CUSTOM_VALIDATORS = getattr(configuration, 'CUSTOM_VALIDATORS', {})
|
|
||||||
DATE_FORMAT = getattr(configuration, 'DATE_FORMAT', 'N j, Y')
|
DATE_FORMAT = getattr(configuration, 'DATE_FORMAT', 'N j, Y')
|
||||||
DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a')
|
DATETIME_FORMAT = getattr(configuration, 'DATETIME_FORMAT', 'N j, Y g:i a')
|
||||||
DEBUG = getattr(configuration, 'DEBUG', False)
|
DEBUG = getattr(configuration, 'DEBUG', False)
|
||||||
|
Loading…
Reference in New Issue
Block a user