1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00
Files
netbox-community-netbox/netbox/extras/models/customfields.py

582 lines
23 KiB
Python
Raw Normal View History

import re
from datetime import datetime, date
import decimal
2020-05-07 16:59:27 -04:00
import django_filters
2020-05-07 16:59:27 -04:00
from django import forms
from django.conf import settings
2020-05-07 16:59:27 -04:00
from django.contrib.contenttypes.models import ContentType
from django.contrib.postgres.fields import ArrayField
from django.core.validators import RegexValidator, ValidationError
2020-05-07 16:59:27 -04:00
from django.db import models
2021-06-22 16:28:06 -04:00
from django.urls import reverse
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _
2020-05-07 16:59:27 -04:00
from extras.choices import *
2022-01-19 15:16:10 -05:00
from extras.utils import FeatureQuery
2021-06-22 16:28:06 -04:00
from netbox.models import ChangeLoggedModel
from netbox.models.features import CloningMixin, ExportTemplatesMixin, WebhooksMixin
2022-10-21 13:16:16 -04:00
from netbox.search import FieldTypes
from utilities import filters
from utilities.forms import (
2022-01-11 16:16:13 -05:00
CSVChoiceField, CSVMultipleChoiceField, DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField,
JSONField, LaxURLField, StaticSelectMultiple, StaticSelect, add_blank_choice,
)
from utilities.querysets import RestrictedQuerySet
from utilities.validators import validate_regex
2020-05-07 16:59:27 -04:00
__all__ = (
'CustomField',
'CustomFieldManager',
)
2022-10-21 13:16:16 -04:00
SEARCH_TYPES = {
CustomFieldTypeChoices.TYPE_TEXT: FieldTypes.STRING,
CustomFieldTypeChoices.TYPE_LONGTEXT: FieldTypes.STRING,
CustomFieldTypeChoices.TYPE_INTEGER: FieldTypes.INTEGER,
CustomFieldTypeChoices.TYPE_DECIMAL: FieldTypes.FLOAT,
CustomFieldTypeChoices.TYPE_DATE: FieldTypes.STRING,
CustomFieldTypeChoices.TYPE_URL: FieldTypes.STRING,
}
class CustomFieldManager(models.Manager.from_queryset(RestrictedQuerySet)):
2020-05-07 17:20:32 -04:00
use_in_migrations = True
def get_for_model(self, model):
"""
Return all CustomFields assigned to the given model.
"""
content_type = ContentType.objects.get_for_model(model._meta.concrete_model)
return self.get_queryset().filter(content_types=content_type)
2020-05-07 17:20:32 -04:00
class CustomField(CloningMixin, ExportTemplatesMixin, WebhooksMixin, ChangeLoggedModel):
content_types = models.ManyToManyField(
2020-05-07 16:59:27 -04:00
to=ContentType,
related_name='custom_fields',
limit_choices_to=FeatureQuery('custom_fields'),
help_text=_('The object(s) to which this field applies.')
2020-05-07 16:59:27 -04:00
)
type = models.CharField(
max_length=50,
choices=CustomFieldTypeChoices,
2021-12-30 17:03:41 -05:00
default=CustomFieldTypeChoices.TYPE_TEXT,
help_text=_('The type of data this custom field holds')
2020-05-07 16:59:27 -04:00
)
2021-12-30 17:03:41 -05:00
object_type = models.ForeignKey(
to=ContentType,
on_delete=models.PROTECT,
blank=True,
null=True,
help_text=_('The type of NetBox object this field maps to (for object fields)')
2021-12-30 17:03:41 -05:00
)
2020-05-07 16:59:27 -04:00
name = models.CharField(
max_length=50,
2020-11-10 10:21:18 -05:00
unique=True,
help_text=_('Internal field name'),
validators=(
RegexValidator(
regex=r'^[a-z0-9_]+$',
message="Only alphanumeric characters and underscores are allowed.",
flags=re.IGNORECASE
),
)
2020-05-07 16:59:27 -04:00
)
label = models.CharField(
max_length=50,
blank=True,
help_text=_('Name of the field as displayed to users (if not provided, '
'the field\'s name will be used)')
2020-05-07 16:59:27 -04:00
)
group_name = models.CharField(
max_length=50,
blank=True,
help_text=_("Custom fields within the same group will be displayed together")
)
2020-05-07 16:59:27 -04:00
description = models.CharField(
max_length=200,
blank=True
)
required = models.BooleanField(
default=False,
help_text=_('If true, this field is required when creating new objects '
'or editing an existing object.')
2020-05-07 16:59:27 -04:00
)
2022-10-21 13:16:16 -04:00
search_weight = models.PositiveSmallIntegerField(
default=1000,
help_text=_('Weighting for search. Lower values are considered more important. '
'Fields with a search weight of zero will be ignored.')
2022-10-21 13:16:16 -04:00
)
2020-05-07 16:59:27 -04:00
filter_logic = models.CharField(
max_length=50,
choices=CustomFieldFilterLogicChoices,
default=CustomFieldFilterLogicChoices.FILTER_LOOSE,
help_text=_('Loose matches any instance of a given string; exact '
'matches the entire field.')
2020-05-07 16:59:27 -04:00
)
default = models.JSONField(
2020-05-07 16:59:27 -04:00
blank=True,
null=True,
help_text=_('Default value for the field (must be a JSON value). Encapsulate '
'strings with double quotes (e.g. "Foo").')
2020-05-07 16:59:27 -04:00
)
weight = models.PositiveSmallIntegerField(
default=100,
2022-10-21 13:16:16 -04:00
verbose_name='Display weight',
help_text=_('Fields with higher weights appear lower in a form.')
2020-05-07 16:59:27 -04:00
)
validation_minimum = models.IntegerField(
blank=True,
null=True,
verbose_name='Minimum value',
help_text=_('Minimum allowed value (for numeric fields)')
)
validation_maximum = models.IntegerField(
blank=True,
null=True,
verbose_name='Maximum value',
help_text=_('Maximum allowed value (for numeric fields)')
)
validation_regex = models.CharField(
blank=True,
validators=[validate_regex],
max_length=500,
verbose_name='Validation regex',
help_text=_('Regular expression to enforce on text field values. Use ^ and $ to force matching of entire string. '
'For example, <code>^[A-Z]{3}$</code> will limit values to exactly three uppercase letters.')
)
choices = ArrayField(
base_field=models.CharField(max_length=100),
blank=True,
null=True,
help_text=_('Comma-separated list of available choices (for selection fields)')
)
ui_visibility = models.CharField(
max_length=50,
choices=CustomFieldVisibilityChoices,
default=CustomFieldVisibilityChoices.VISIBILITY_READ_WRITE,
2022-05-24 16:39:05 -04:00
verbose_name='UI visibility',
help_text=_('Specifies the visibility of custom field in the UI')
)
2020-05-07 17:20:32 -04:00
objects = CustomFieldManager()
clone_fields = (
2022-10-21 13:16:16 -04:00
'content_types', 'type', 'object_type', 'group_name', 'description', 'required', 'search_weight',
'filter_logic', 'default', 'weight', 'validation_minimum', 'validation_maximum', 'validation_regex', 'choices',
'ui_visibility',
)
2020-05-07 16:59:27 -04:00
class Meta:
ordering = ['group_name', 'weight', 'name']
2020-05-07 16:59:27 -04:00
def __str__(self):
return self.label or self.name.replace('_', ' ').capitalize()
2021-06-22 16:28:06 -04:00
def get_absolute_url(self):
return reverse('extras:customfield', args=[self.pk])
@property
def docs_url(self):
return f'{settings.STATIC_URL}docs/models/extras/customfield/'
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Cache instance's original name so we can check later whether it has changed
self._name = self.name
2022-10-21 13:16:16 -04:00
@property
def search_type(self):
return SEARCH_TYPES.get(self.type)
def populate_initial_data(self, content_types):
"""
Populate initial custom field data upon either a) the creation of a new CustomField, or
b) the assignment of an existing CustomField to new object types.
"""
for ct in content_types:
model = ct.model_class()
instances = model.objects.exclude(**{f'custom_field_data__contains': self.name})
for instance in instances:
instance.custom_field_data[self.name] = self.default
model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100)
def remove_stale_data(self, content_types):
"""
Delete custom field data which is no longer relevant (either because the CustomField is
no longer assigned to a model, or because it has been deleted).
"""
for ct in content_types:
model = ct.model_class()
instances = model.objects.filter(**{f'custom_field_data__{self.name}__isnull': False})
for instance in instances:
2022-08-01 09:16:58 -04:00
del instance.custom_field_data[self.name]
model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100)
def rename_object_data(self, old_name, new_name):
"""
Called when a CustomField has been renamed. Updates all assigned object data.
"""
for ct in self.content_types.all():
model = ct.model_class()
params = {f'custom_field_data__{old_name}__isnull': False}
instances = model.objects.filter(**params)
for instance in instances:
instance.custom_field_data[new_name] = instance.custom_field_data.pop(old_name)
model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100)
def clean(self):
super().clean()
# Validate the field's default value (if any)
if self.default is not None:
try:
if self.type in (CustomFieldTypeChoices.TYPE_TEXT, CustomFieldTypeChoices.TYPE_LONGTEXT):
default_value = str(self.default)
else:
default_value = self.default
self.validate(default_value)
except ValidationError as err:
raise ValidationError({
'default': f'Invalid default value "{self.default}": {err.message}'
})
# Minimum/maximum values can be set only for numeric fields
if self.type not in (CustomFieldTypeChoices.TYPE_INTEGER, CustomFieldTypeChoices.TYPE_DECIMAL):
if self.validation_minimum:
raise ValidationError({'validation_minimum': "A minimum value may be set only for numeric fields"})
if self.validation_maximum:
raise ValidationError({'validation_maximum': "A maximum value may be set only for numeric fields"})
# Regex validation can be set only for text fields
regex_types = (
CustomFieldTypeChoices.TYPE_TEXT,
CustomFieldTypeChoices.TYPE_LONGTEXT,
CustomFieldTypeChoices.TYPE_URL,
)
if self.validation_regex and self.type not in regex_types:
raise ValidationError({
'validation_regex': "Regular expression validation is supported only for text and URL fields"
})
# Choices can be set only on selection fields
if self.choices and self.type not in (
CustomFieldTypeChoices.TYPE_SELECT,
CustomFieldTypeChoices.TYPE_MULTISELECT
):
raise ValidationError({
'choices': "Choices may be set only for custom selection fields."
})
# Selection fields must have at least one choice defined
if self.type in (
CustomFieldTypeChoices.TYPE_SELECT,
CustomFieldTypeChoices.TYPE_MULTISELECT
) and not self.choices:
raise ValidationError({
'choices': "Selection fields must specify at least one choice."
})
# A selection field's default (if any) must be present in its available choices
if self.type == CustomFieldTypeChoices.TYPE_SELECT and self.default and self.default not in self.choices:
raise ValidationError({
'default': f"The specified default value ({self.default}) is not listed as an available choice."
})
2022-01-06 13:43:40 -05:00
# Object fields must define an object_type; other fields must not
if self.type in (CustomFieldTypeChoices.TYPE_OBJECT, CustomFieldTypeChoices.TYPE_MULTIOBJECT):
if not self.object_type:
raise ValidationError({
'object_type': "Object fields must define an object type."
})
elif self.object_type:
raise ValidationError({
'object_type': f"{self.get_type_display()} fields may not define an object type."
})
2021-12-30 17:03:41 -05:00
def serialize(self, value):
"""
Prepare a value for storage as JSON data.
"""
if value is None:
return value
2022-11-15 15:44:36 -05:00
if self.type == CustomFieldTypeChoices.TYPE_DATE and type(value) is date:
return value.isoformat()
if self.type == CustomFieldTypeChoices.TYPE_OBJECT:
2021-12-30 17:03:41 -05:00
return value.pk
if self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
2022-01-05 21:21:23 -05:00
return [obj.pk for obj in value] or None
2021-12-30 17:03:41 -05:00
return value
def deserialize(self, value):
"""
Convert JSON data to a Python object suitable for the field type.
"""
if value is None:
return value
2022-11-15 15:44:36 -05:00
if self.type == CustomFieldTypeChoices.TYPE_DATE:
try:
return date.fromisoformat(value)
except ValueError:
return value
if self.type == CustomFieldTypeChoices.TYPE_OBJECT:
2021-12-30 17:03:41 -05:00
model = self.object_type.model_class()
return model.objects.filter(pk=value).first()
if self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
model = self.object_type.model_class()
return model.objects.filter(pk__in=value)
2021-12-30 17:03:41 -05:00
return value
def to_form_field(self, set_initial=True, enforce_required=True, enforce_visibility=True, for_csv_import=False):
2020-05-07 16:59:27 -04:00
"""
Return a form field suitable for setting a CustomField's value for an object.
set_initial: Set initial data for the field. This should be False when generating a field for bulk editing.
2020-05-07 16:59:27 -04:00
enforce_required: Honor the value of CustomField.required. Set to False for filtering/bulk editing.
enforce_visibility: Honor the value of CustomField.ui_visibility. Set to False for filtering.
2020-05-07 16:59:27 -04:00
for_csv_import: Return a form field suitable for bulk import of objects in CSV format.
"""
initial = self.default if set_initial else None
required = self.required if enforce_required else False
# Integer
if self.type == CustomFieldTypeChoices.TYPE_INTEGER:
field = forms.IntegerField(
required=required,
initial=initial,
min_value=self.validation_minimum,
max_value=self.validation_maximum
)
2020-05-07 16:59:27 -04:00
# Decimal
elif self.type == CustomFieldTypeChoices.TYPE_DECIMAL:
field = forms.DecimalField(
required=required,
initial=initial,
max_digits=12,
decimal_places=4,
min_value=self.validation_minimum,
max_value=self.validation_maximum
)
2020-05-07 16:59:27 -04:00
# Boolean
elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
choices = (
(None, '---------'),
(True, 'True'),
(False, 'False'),
2020-05-07 16:59:27 -04:00
)
field = forms.NullBooleanField(
2021-07-17 21:24:20 -07:00
required=required, initial=initial, widget=StaticSelect(choices=choices)
2020-05-07 16:59:27 -04:00
)
# Date
elif self.type == CustomFieldTypeChoices.TYPE_DATE:
field = forms.DateField(required=required, initial=initial, widget=DatePicker())
# Select
elif self.type in (CustomFieldTypeChoices.TYPE_SELECT, CustomFieldTypeChoices.TYPE_MULTISELECT):
choices = [(c, c) for c in self.choices]
default_choice = self.default if self.default in self.choices else None
2020-05-07 16:59:27 -04:00
if not required or default_choice is None:
2020-05-07 16:59:27 -04:00
choices = add_blank_choice(choices)
# Set the initial value to the first available choice (if any)
if set_initial and default_choice:
initial = default_choice
2020-05-07 16:59:27 -04:00
if self.type == CustomFieldTypeChoices.TYPE_SELECT:
field_class = CSVChoiceField if for_csv_import else forms.ChoiceField
field = field_class(
2021-07-17 21:24:20 -07:00
choices=choices, required=required, initial=initial, widget=StaticSelect()
)
else:
field_class = CSVMultipleChoiceField if for_csv_import else forms.MultipleChoiceField
field = field_class(
2021-07-17 21:24:20 -07:00
choices=choices, required=required, initial=initial, widget=StaticSelectMultiple()
)
2020-05-07 16:59:27 -04:00
# URL
elif self.type == CustomFieldTypeChoices.TYPE_URL:
field = LaxURLField(required=required, initial=initial)
# JSON
elif self.type == CustomFieldTypeChoices.TYPE_JSON:
field = JSONField(required=required, initial=initial)
2021-12-30 17:03:41 -05:00
# Object
elif self.type == CustomFieldTypeChoices.TYPE_OBJECT:
model = self.object_type.model_class()
field = DynamicModelChoiceField(
queryset=model.objects.all(),
required=required,
initial=initial
)
# Multiple objects
elif self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
model = self.object_type.model_class()
field = DynamicModelMultipleChoiceField(
queryset=model.objects.all(),
required=required,
initial=initial
2021-12-30 17:03:41 -05:00
)
2020-05-07 16:59:27 -04:00
# Text
else:
widget = forms.Textarea if self.type == CustomFieldTypeChoices.TYPE_LONGTEXT else None
field = forms.CharField(required=required, initial=initial, widget=widget)
if self.validation_regex:
field.validators = [
RegexValidator(
regex=self.validation_regex,
message=mark_safe(f"Values must match this regex: <code>{self.validation_regex}</code>")
)
]
2020-05-07 16:59:27 -04:00
field.model = self
2020-08-25 13:42:47 -04:00
field.label = str(self)
2020-05-07 16:59:27 -04:00
if self.description:
field.help_text = escape(self.description)
2020-05-07 16:59:27 -04:00
# Annotate read-only fields
if enforce_visibility and self.ui_visibility == CustomFieldVisibilityChoices.VISIBILITY_READ_ONLY:
field.disabled = True
prepend = '<br />' if field.help_text else ''
field.help_text += f'{prepend}<i class="mdi mdi-alert-circle-outline"></i> Field is set to read-only.'
2020-05-07 16:59:27 -04:00
return field
def to_filter(self, lookup_expr=None):
2021-10-28 15:31:50 -04:00
"""
Return a django_filters Filter instance suitable for this field type.
:param lookup_expr: Custom lookup expression (optional)
2021-10-28 15:31:50 -04:00
"""
kwargs = {
'field_name': f'custom_field_data__{self.name}'
}
if lookup_expr is not None:
kwargs['lookup_expr'] = lookup_expr
2021-10-28 15:31:50 -04:00
# Text/URL
if self.type in (
CustomFieldTypeChoices.TYPE_TEXT,
CustomFieldTypeChoices.TYPE_LONGTEXT,
CustomFieldTypeChoices.TYPE_URL,
):
filter_class = filters.MultiValueCharFilter
2021-10-28 15:31:50 -04:00
if self.filter_logic == CustomFieldFilterLogicChoices.FILTER_LOOSE:
kwargs['lookup_expr'] = 'icontains'
# Integer
elif self.type == CustomFieldTypeChoices.TYPE_INTEGER:
filter_class = filters.MultiValueNumberFilter
2021-10-28 15:31:50 -04:00
# Decimal
elif self.type == CustomFieldTypeChoices.TYPE_DECIMAL:
filter_class = filters.MultiValueDecimalFilter
2021-10-28 15:31:50 -04:00
# Boolean
elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
filter_class = django_filters.BooleanFilter
2021-10-28 15:31:50 -04:00
# Date
elif self.type == CustomFieldTypeChoices.TYPE_DATE:
filter_class = filters.MultiValueDateFilter
2021-10-28 15:31:50 -04:00
# Select
elif self.type == CustomFieldTypeChoices.TYPE_SELECT:
filter_class = filters.MultiValueCharFilter
2021-10-28 15:31:50 -04:00
# Multiselect
elif self.type == CustomFieldTypeChoices.TYPE_MULTISELECT:
filter_class = filters.MultiValueCharFilter
2021-10-28 15:31:50 -04:00
kwargs['lookup_expr'] = 'has_key'
# Object
elif self.type == CustomFieldTypeChoices.TYPE_OBJECT:
filter_class = filters.MultiValueNumberFilter
# Multi-object
elif self.type == CustomFieldTypeChoices.TYPE_MULTIOBJECT:
filter_class = filters.MultiValueNumberFilter
kwargs['lookup_expr'] = 'contains'
2021-10-28 15:31:50 -04:00
# Unsupported custom field type
else:
return None
filter_instance = filter_class(**kwargs)
filter_instance.custom_field = self
return filter_instance
2021-10-28 15:31:50 -04:00
def validate(self, value):
"""
Validate a value according to the field's type validation rules.
"""
if value not in [None, '']:
# Validate text field
if self.type in (CustomFieldTypeChoices.TYPE_TEXT, CustomFieldTypeChoices.TYPE_LONGTEXT):
if type(value) is not str:
raise ValidationError(f"Value must be a string.")
if self.validation_regex and not re.match(self.validation_regex, value):
raise ValidationError(f"Value must match regex '{self.validation_regex}'")
# Validate integer
elif self.type == CustomFieldTypeChoices.TYPE_INTEGER:
if type(value) is not int:
raise ValidationError("Value must be an integer.")
if self.validation_minimum is not None and value < self.validation_minimum:
raise ValidationError(f"Value must be at least {self.validation_minimum}")
if self.validation_maximum is not None and value > self.validation_maximum:
raise ValidationError(f"Value must not exceed {self.validation_maximum}")
# Validate decimal
elif self.type == CustomFieldTypeChoices.TYPE_DECIMAL:
try:
decimal.Decimal(value)
except decimal.InvalidOperation:
raise ValidationError("Value must be a decimal.")
if self.validation_minimum is not None and value < self.validation_minimum:
raise ValidationError(f"Value must be at least {self.validation_minimum}")
if self.validation_maximum is not None and value > self.validation_maximum:
raise ValidationError(f"Value must not exceed {self.validation_maximum}")
# Validate boolean
elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN and value not in [True, False, 1, 0]:
raise ValidationError("Value must be true or false.")
# Validate date
elif self.type == CustomFieldTypeChoices.TYPE_DATE:
if type(value) is not date:
try:
datetime.strptime(value, '%Y-%m-%d')
except ValueError:
raise ValidationError("Date values must be in the format YYYY-MM-DD.")
# Validate selected choice
elif self.type == CustomFieldTypeChoices.TYPE_SELECT:
if value not in self.choices:
raise ValidationError(
f"Invalid choice ({value}). Available choices are: {', '.join(self.choices)}"
)
# Validate all selected choices
elif self.type == CustomFieldTypeChoices.TYPE_MULTISELECT:
if not set(value).issubset(self.choices):
raise ValidationError(
f"Invalid choice(s) ({', '.join(value)}). Available choices are: {', '.join(self.choices)}"
)
elif self.required:
raise ValidationError("Required field cannot be empty.")