customfields.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import re
  2. from datetime import datetime, date
  3. import django_filters
  4. from django import forms
  5. from django.contrib.contenttypes.models import ContentType
  6. from django.contrib.postgres.fields import ArrayField
  7. from django.core.validators import RegexValidator, ValidationError
  8. from django.db import models
  9. from django.urls import reverse
  10. from django.utils.safestring import mark_safe
  11. from extras.choices import *
  12. from extras.utils import FeatureQuery, extras_features
  13. from netbox.models import ChangeLoggedModel
  14. from utilities import filters
  15. from utilities.forms import (
  16. CSVChoiceField, DatePicker, LaxURLField, StaticSelectMultiple, StaticSelect, add_blank_choice,
  17. )
  18. from utilities.querysets import RestrictedQuerySet
  19. from utilities.validators import validate_regex
  20. class CustomFieldManager(models.Manager.from_queryset(RestrictedQuerySet)):
  21. use_in_migrations = True
  22. def get_for_model(self, model):
  23. """
  24. Return all CustomFields assigned to the given model.
  25. """
  26. content_type = ContentType.objects.get_for_model(model._meta.concrete_model)
  27. return self.get_queryset().filter(content_types=content_type)
  28. @extras_features('webhooks')
  29. class CustomField(ChangeLoggedModel):
  30. content_types = models.ManyToManyField(
  31. to=ContentType,
  32. related_name='custom_fields',
  33. limit_choices_to=FeatureQuery('custom_fields'),
  34. help_text='The object(s) to which this field applies.'
  35. )
  36. type = models.CharField(
  37. max_length=50,
  38. choices=CustomFieldTypeChoices,
  39. default=CustomFieldTypeChoices.TYPE_TEXT
  40. )
  41. name = models.CharField(
  42. max_length=50,
  43. unique=True,
  44. help_text='Internal field name'
  45. )
  46. label = models.CharField(
  47. max_length=50,
  48. blank=True,
  49. help_text='Name of the field as displayed to users (if not provided, '
  50. 'the field\'s name will be used)'
  51. )
  52. description = models.CharField(
  53. max_length=200,
  54. blank=True
  55. )
  56. required = models.BooleanField(
  57. default=False,
  58. help_text='If true, this field is required when creating new objects '
  59. 'or editing an existing object.'
  60. )
  61. filter_logic = models.CharField(
  62. max_length=50,
  63. choices=CustomFieldFilterLogicChoices,
  64. default=CustomFieldFilterLogicChoices.FILTER_LOOSE,
  65. help_text='Loose matches any instance of a given string; exact '
  66. 'matches the entire field.'
  67. )
  68. default = models.JSONField(
  69. blank=True,
  70. null=True,
  71. help_text='Default value for the field (must be a JSON value). Encapsulate '
  72. 'strings with double quotes (e.g. "Foo").'
  73. )
  74. weight = models.PositiveSmallIntegerField(
  75. default=100,
  76. help_text='Fields with higher weights appear lower in a form.'
  77. )
  78. validation_minimum = models.PositiveIntegerField(
  79. blank=True,
  80. null=True,
  81. verbose_name='Minimum value',
  82. help_text='Minimum allowed value (for numeric fields)'
  83. )
  84. validation_maximum = models.PositiveIntegerField(
  85. blank=True,
  86. null=True,
  87. verbose_name='Maximum value',
  88. help_text='Maximum allowed value (for numeric fields)'
  89. )
  90. validation_regex = models.CharField(
  91. blank=True,
  92. validators=[validate_regex],
  93. max_length=500,
  94. verbose_name='Validation regex',
  95. help_text='Regular expression to enforce on text field values. Use ^ and $ to force matching of entire string. '
  96. 'For example, <code>^[A-Z]{3}$</code> will limit values to exactly three uppercase letters.'
  97. )
  98. choices = ArrayField(
  99. base_field=models.CharField(max_length=100),
  100. blank=True,
  101. null=True,
  102. help_text='Comma-separated list of available choices (for selection fields)'
  103. )
  104. objects = CustomFieldManager()
  105. class Meta:
  106. ordering = ['weight', 'name']
  107. def __str__(self):
  108. return self.label or self.name.replace('_', ' ').capitalize()
  109. def get_absolute_url(self):
  110. return reverse('extras:customfield', args=[self.pk])
  111. def __init__(self, *args, **kwargs):
  112. super().__init__(*args, **kwargs)
  113. # Cache instance's original name so we can check later whether it has changed
  114. self._name = self.name
  115. def populate_initial_data(self, content_types):
  116. """
  117. Populate initial custom field data upon either a) the creation of a new CustomField, or
  118. b) the assignment of an existing CustomField to new object types.
  119. """
  120. for ct in content_types:
  121. model = ct.model_class()
  122. instances = model.objects.exclude(**{f'custom_field_data__contains': self.name})
  123. for instance in instances:
  124. instance.custom_field_data[self.name] = self.default
  125. model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100)
  126. def remove_stale_data(self, content_types):
  127. """
  128. Delete custom field data which is no longer relevant (either because the CustomField is
  129. no longer assigned to a model, or because it has been deleted).
  130. """
  131. for ct in content_types:
  132. model = ct.model_class()
  133. instances = model.objects.filter(**{f'custom_field_data__{self.name}__isnull': False})
  134. for instance in instances:
  135. del(instance.custom_field_data[self.name])
  136. model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100)
  137. def rename_object_data(self, old_name, new_name):
  138. """
  139. Called when a CustomField has been renamed. Updates all assigned object data.
  140. """
  141. for ct in self.content_types.all():
  142. model = ct.model_class()
  143. params = {f'custom_field_data__{old_name}__isnull': False}
  144. instances = model.objects.filter(**params)
  145. for instance in instances:
  146. instance.custom_field_data[new_name] = instance.custom_field_data.pop(old_name)
  147. model.objects.bulk_update(instances, ['custom_field_data'], batch_size=100)
  148. def clean(self):
  149. super().clean()
  150. # Validate the field's default value (if any)
  151. if self.default is not None:
  152. try:
  153. if self.type in (CustomFieldTypeChoices.TYPE_TEXT, CustomFieldTypeChoices.TYPE_LONGTEXT):
  154. default_value = str(self.default)
  155. else:
  156. default_value = self.default
  157. self.validate(default_value)
  158. except ValidationError as err:
  159. raise ValidationError({
  160. 'default': f'Invalid default value "{self.default}": {err.message}'
  161. })
  162. # Minimum/maximum values can be set only for numeric fields
  163. if self.validation_minimum is not None and self.type != CustomFieldTypeChoices.TYPE_INTEGER:
  164. raise ValidationError({
  165. 'validation_minimum': "A minimum value may be set only for numeric fields"
  166. })
  167. if self.validation_maximum is not None and self.type != CustomFieldTypeChoices.TYPE_INTEGER:
  168. raise ValidationError({
  169. 'validation_maximum': "A maximum value may be set only for numeric fields"
  170. })
  171. # Regex validation can be set only for text fields
  172. regex_types = (
  173. CustomFieldTypeChoices.TYPE_TEXT,
  174. CustomFieldTypeChoices.TYPE_LONGTEXT,
  175. CustomFieldTypeChoices.TYPE_URL,
  176. )
  177. if self.validation_regex and self.type not in regex_types:
  178. raise ValidationError({
  179. 'validation_regex': "Regular expression validation is supported only for text and URL fields"
  180. })
  181. # Choices can be set only on selection fields
  182. if self.choices and self.type not in (
  183. CustomFieldTypeChoices.TYPE_SELECT,
  184. CustomFieldTypeChoices.TYPE_MULTISELECT
  185. ):
  186. raise ValidationError({
  187. 'choices': "Choices may be set only for custom selection fields."
  188. })
  189. # A selection field must have at least two choices defined
  190. if self.type == CustomFieldTypeChoices.TYPE_SELECT and self.choices and len(self.choices) < 2:
  191. raise ValidationError({
  192. 'choices': "Selection fields must specify at least two choices."
  193. })
  194. # A selection field's default (if any) must be present in its available choices
  195. if self.type == CustomFieldTypeChoices.TYPE_SELECT and self.default and self.default not in self.choices:
  196. raise ValidationError({
  197. 'default': f"The specified default value ({self.default}) is not listed as an available choice."
  198. })
  199. def to_form_field(self, set_initial=True, enforce_required=True, for_csv_import=False):
  200. """
  201. Return a form field suitable for setting a CustomField's value for an object.
  202. set_initial: Set initial date for the field. This should be False when generating a field for bulk editing.
  203. enforce_required: Honor the value of CustomField.required. Set to False for filtering/bulk editing.
  204. for_csv_import: Return a form field suitable for bulk import of objects in CSV format.
  205. """
  206. initial = self.default if set_initial else None
  207. required = self.required if enforce_required else False
  208. # Integer
  209. if self.type == CustomFieldTypeChoices.TYPE_INTEGER:
  210. field = forms.IntegerField(
  211. required=required,
  212. initial=initial,
  213. min_value=self.validation_minimum,
  214. max_value=self.validation_maximum
  215. )
  216. # Boolean
  217. elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
  218. choices = (
  219. (None, '---------'),
  220. (True, 'True'),
  221. (False, 'False'),
  222. )
  223. field = forms.NullBooleanField(
  224. required=required, initial=initial, widget=StaticSelect(choices=choices)
  225. )
  226. # Date
  227. elif self.type == CustomFieldTypeChoices.TYPE_DATE:
  228. field = forms.DateField(required=required, initial=initial, widget=DatePicker())
  229. # Select
  230. elif self.type in (CustomFieldTypeChoices.TYPE_SELECT, CustomFieldTypeChoices.TYPE_MULTISELECT):
  231. choices = [(c, c) for c in self.choices]
  232. default_choice = self.default if self.default in self.choices else None
  233. if not required or default_choice is None:
  234. choices = add_blank_choice(choices)
  235. # Set the initial value to the first available choice (if any)
  236. if set_initial and default_choice:
  237. initial = default_choice
  238. if self.type == CustomFieldTypeChoices.TYPE_SELECT:
  239. field_class = CSVChoiceField if for_csv_import else forms.ChoiceField
  240. field = field_class(
  241. choices=choices, required=required, initial=initial, widget=StaticSelect()
  242. )
  243. else:
  244. field_class = CSVChoiceField if for_csv_import else forms.MultipleChoiceField
  245. field = field_class(
  246. choices=choices, required=required, initial=initial, widget=StaticSelectMultiple()
  247. )
  248. # URL
  249. elif self.type == CustomFieldTypeChoices.TYPE_URL:
  250. field = LaxURLField(required=required, initial=initial)
  251. # JSON
  252. elif self.type == CustomFieldTypeChoices.TYPE_JSON:
  253. field = forms.JSONField(required=required, initial=initial)
  254. # Text
  255. else:
  256. if self.type == CustomFieldTypeChoices.TYPE_LONGTEXT:
  257. max_length = None
  258. widget = forms.Textarea
  259. else:
  260. max_length = 255
  261. widget = None
  262. field = forms.CharField(max_length=max_length, required=required, initial=initial, widget=widget)
  263. if self.validation_regex:
  264. field.validators = [
  265. RegexValidator(
  266. regex=self.validation_regex,
  267. message=mark_safe(f"Values must match this regex: <code>{self.validation_regex}</code>")
  268. )
  269. ]
  270. field.model = self
  271. field.label = str(self)
  272. if self.description:
  273. field.help_text = self.description
  274. return field
  275. def to_filter(self, lookup_expr=None):
  276. """
  277. Return a django_filters Filter instance suitable for this field type.
  278. :param lookup_expr: Custom lookup expression (optional)
  279. """
  280. kwargs = {
  281. 'field_name': f'custom_field_data__{self.name}'
  282. }
  283. if lookup_expr is not None:
  284. kwargs['lookup_expr'] = lookup_expr
  285. # Text/URL
  286. if self.type in (
  287. CustomFieldTypeChoices.TYPE_TEXT,
  288. CustomFieldTypeChoices.TYPE_LONGTEXT,
  289. CustomFieldTypeChoices.TYPE_URL,
  290. ):
  291. filter_class = filters.MultiValueCharFilter
  292. if self.filter_logic == CustomFieldFilterLogicChoices.FILTER_LOOSE:
  293. kwargs['lookup_expr'] = 'icontains'
  294. # Integer
  295. elif self.type == CustomFieldTypeChoices.TYPE_INTEGER:
  296. filter_class = filters.MultiValueNumberFilter
  297. # Boolean
  298. elif self.type == CustomFieldTypeChoices.TYPE_BOOLEAN:
  299. filter_class = django_filters.BooleanFilter
  300. # Date
  301. elif self.type == CustomFieldTypeChoices.TYPE_DATE:
  302. filter_class = filters.MultiValueDateFilter
  303. # Select
  304. elif self.type == CustomFieldTypeChoices.TYPE_SELECT:
  305. filter_class = filters.MultiValueCharFilter
  306. # Multiselect
  307. elif self.type == CustomFieldTypeChoices.TYPE_MULTISELECT:
  308. filter_class = filters.MultiValueCharFilter
  309. kwargs['lookup_expr'] = 'has_key'
  310. # Unsupported custom field type
  311. else:
  312. return None
  313. filter_instance = filter_class(**kwargs)
  314. filter_instance.custom_field = self
  315. return filter_instance
  316. def validate(self, value):
  317. """
  318. Validate a value according to the field's type validation rules.
  319. """
  320. if value not in [None, '']:
  321. # Validate text field
  322. if self.type in (CustomFieldTypeChoices.TYPE_TEXT, CustomFieldTypeChoices.TYPE_LONGTEXT):
  323. if type(value) is not str:
  324. raise ValidationError(f"Value must be a string.")
  325. if self.validation_regex and not re.match(self.validation_regex, value):
  326. raise ValidationError(f"Value must match regex '{self.validation_regex}'")
  327. # Validate integer
  328. if self.type == CustomFieldTypeChoices.TYPE_INTEGER:
  329. if type(value) is not int:
  330. raise ValidationError("Value must be an integer.")
  331. if self.validation_minimum is not None and value < self.validation_minimum:
  332. raise ValidationError(f"Value must be at least {self.validation_minimum}")
  333. if self.validation_maximum is not None and value > self.validation_maximum:
  334. raise ValidationError(f"Value must not exceed {self.validation_maximum}")
  335. # Validate boolean
  336. if self.type == CustomFieldTypeChoices.TYPE_BOOLEAN and value not in [True, False, 1, 0]:
  337. raise ValidationError("Value must be true or false.")
  338. # Validate date
  339. if self.type == CustomFieldTypeChoices.TYPE_DATE:
  340. if type(value) is not date:
  341. try:
  342. datetime.strptime(value, '%Y-%m-%d')
  343. except ValueError:
  344. raise ValidationError("Date values must be in the format YYYY-MM-DD.")
  345. # Validate selected choice
  346. if self.type == CustomFieldTypeChoices.TYPE_SELECT:
  347. if value not in self.choices:
  348. raise ValidationError(
  349. f"Invalid choice ({value}). Available choices are: {', '.join(self.choices)}"
  350. )
  351. # Validate all selected choices
  352. if self.type == CustomFieldTypeChoices.TYPE_MULTISELECT:
  353. if not set(value).issubset(self.choices):
  354. raise ValidationError(
  355. f"Invalid choice(s) ({', '.join(value)}). Available choices are: {', '.join(self.choices)}"
  356. )
  357. elif self.required:
  358. raise ValidationError("Required field cannot be empty.")