customfields.py 17 KB

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