From 176bd2396be49c9783b2a2e86a4acb3d6aeb4612 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Thu, 14 Oct 2021 14:48:00 -0400 Subject: [PATCH] Closes #6711: Add longtext custom field type with Markdown support --- docs/models/extras/customfield.md | 1 + docs/release-notes/version-3.1.md | 1 + netbox/extras/choices.py | 2 + netbox/extras/models/customfields.py | 21 ++++- netbox/extras/tests/test_customfields.py | 86 ++++++++++++++++--- netbox/extras/tests/test_forms.py | 3 + netbox/templates/inc/custom_fields_panel.html | 6 +- 7 files changed, 101 insertions(+), 19 deletions(-) diff --git a/docs/models/extras/customfield.md b/docs/models/extras/customfield.md index 52b8bab1e..7294fbd34 100644 --- a/docs/models/extras/customfield.md +++ b/docs/models/extras/customfield.md @@ -11,6 +11,7 @@ Within the database, custom fields are stored as JSON data directly alongside ea Custom fields may be created by navigating to Customization > Custom Fields. NetBox supports six types of custom field: * Text: Free-form text (up to 255 characters) +* Long text: Free-form of any length; supports Markdown rendering * Integer: A whole number (positive or negative) * Boolean: True or false * Date: A date in ISO 8601 format (YYYY-MM-DD) diff --git a/docs/release-notes/version-3.1.md b/docs/release-notes/version-3.1.md index 00a6e2fda..23567d68e 100644 --- a/docs/release-notes/version-3.1.md +++ b/docs/release-notes/version-3.1.md @@ -6,6 +6,7 @@ ### Enhancements * [#1337](https://github.com/netbox-community/netbox/issues/1337) - Add WWN field to interfaces +* [#6711](https://github.com/netbox-community/netbox/issues/6711) - Add `longtext` custom field type with Markdown support * [#6874](https://github.com/netbox-community/netbox/issues/6874) - Add tenant assignment for locations ### Other Changes diff --git a/netbox/extras/choices.py b/netbox/extras/choices.py index 4452b5aad..4f350fc9b 100644 --- a/netbox/extras/choices.py +++ b/netbox/extras/choices.py @@ -8,6 +8,7 @@ from utilities.choices import ChoiceSet class CustomFieldTypeChoices(ChoiceSet): TYPE_TEXT = 'text' + TYPE_LONGTEXT = 'longtext' TYPE_INTEGER = 'integer' TYPE_BOOLEAN = 'boolean' TYPE_DATE = 'date' @@ -17,6 +18,7 @@ class CustomFieldTypeChoices(ChoiceSet): CHOICES = ( (TYPE_TEXT, 'Text'), + (TYPE_LONGTEXT, 'Text (long)'), (TYPE_INTEGER, 'Integer'), (TYPE_BOOLEAN, 'Boolean (true/false)'), (TYPE_DATE, 'Date'), diff --git a/netbox/extras/models/customfields.py b/netbox/extras/models/customfields.py index d8e2e11c9..8c0193eaa 100644 --- a/netbox/extras/models/customfields.py +++ b/netbox/extras/models/customfields.py @@ -166,7 +166,10 @@ class CustomField(ChangeLoggedModel): # Validate the field's default value (if any) if self.default is not None: try: - default_value = str(self.default) if self.type == CustomFieldTypeChoices.TYPE_TEXT else self.default + 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({ @@ -184,7 +187,11 @@ class CustomField(ChangeLoggedModel): }) # Regex validation can be set only for text fields - regex_types = (CustomFieldTypeChoices.TYPE_TEXT, CustomFieldTypeChoices.TYPE_URL) + 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" @@ -275,7 +282,13 @@ class CustomField(ChangeLoggedModel): # Text else: - field = forms.CharField(max_length=255, required=required, initial=initial) + if self.type == CustomFieldTypeChoices.TYPE_LONGTEXT: + max_length = None + widget = forms.Textarea + else: + max_length = 255 + widget = None + field = forms.CharField(max_length=max_length, required=required, initial=initial, widget=widget) if self.validation_regex: field.validators = [ RegexValidator( @@ -298,7 +311,7 @@ class CustomField(ChangeLoggedModel): if value not in [None, '']: # Validate text field - if self.type == CustomFieldTypeChoices.TYPE_TEXT: + 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): diff --git a/netbox/extras/tests/test_customfields.py b/netbox/extras/tests/test_customfields.py index 32c473678..0a40aeba9 100644 --- a/netbox/extras/tests/test_customfields.py +++ b/netbox/extras/tests/test_customfields.py @@ -24,13 +24,46 @@ class CustomFieldTest(TestCase): def test_simple_fields(self): DATA = ( - {'field_type': CustomFieldTypeChoices.TYPE_TEXT, 'field_value': 'Foobar!', 'empty_value': ''}, - {'field_type': CustomFieldTypeChoices.TYPE_INTEGER, 'field_value': 0, 'empty_value': None}, - {'field_type': CustomFieldTypeChoices.TYPE_INTEGER, 'field_value': 42, 'empty_value': None}, - {'field_type': CustomFieldTypeChoices.TYPE_BOOLEAN, 'field_value': True, 'empty_value': None}, - {'field_type': CustomFieldTypeChoices.TYPE_BOOLEAN, 'field_value': False, 'empty_value': None}, - {'field_type': CustomFieldTypeChoices.TYPE_DATE, 'field_value': '2016-06-23', 'empty_value': None}, - {'field_type': CustomFieldTypeChoices.TYPE_URL, 'field_value': 'http://example.com/', 'empty_value': ''}, + { + 'field_type': CustomFieldTypeChoices.TYPE_TEXT, + 'field_value': 'Foobar!', + 'empty_value': '', + }, + { + 'field_type': CustomFieldTypeChoices.TYPE_LONGTEXT, + 'field_value': 'Text with **Markdown**', + 'empty_value': '', + }, + { + 'field_type': CustomFieldTypeChoices.TYPE_INTEGER, + 'field_value': 0, + 'empty_value': None, + }, + { + 'field_type': CustomFieldTypeChoices.TYPE_INTEGER, + 'field_value': 42, + 'empty_value': None, + }, + { + 'field_type': CustomFieldTypeChoices.TYPE_BOOLEAN, + 'field_value': True, + 'empty_value': None, + }, + { + 'field_type': CustomFieldTypeChoices.TYPE_BOOLEAN, + 'field_value': False, + 'empty_value': None, + }, + { + 'field_type': CustomFieldTypeChoices.TYPE_DATE, + 'field_value': '2016-06-23', + 'empty_value': None, + }, + { + 'field_type': CustomFieldTypeChoices.TYPE_URL, + 'field_value': 'http://example.com/', + 'empty_value': '', + }, ) obj_type = ContentType.objects.get_for_model(Site) @@ -149,6 +182,11 @@ class CustomFieldAPITest(APITestCase): cls.cf_text.save() cls.cf_text.content_types.set([content_type]) + # Long text custom field + cls.cf_longtext = CustomField(type=CustomFieldTypeChoices.TYPE_LONGTEXT, name='longtext_field', default='ABC') + cls.cf_longtext.save() + cls.cf_longtext.content_types.set([content_type]) + # Integer custom field cls.cf_integer = CustomField(type=CustomFieldTypeChoices.TYPE_INTEGER, name='number_field', default=123) cls.cf_integer.save() @@ -185,6 +223,7 @@ class CustomFieldAPITest(APITestCase): # Assign custom field values for site 2 cls.sites[1].custom_field_data = { cls.cf_text.name: 'bar', + cls.cf_longtext.name: 'DEF', cls.cf_integer.name: 456, cls.cf_boolean.name: True, cls.cf_date.name: '2020-01-02', @@ -204,6 +243,7 @@ class CustomFieldAPITest(APITestCase): self.assertEqual(response.data['name'], self.sites[0].name) self.assertEqual(response.data['custom_fields'], { 'text_field': None, + 'longtext_field': None, 'number_field': None, 'boolean_field': None, 'date_field': None, @@ -222,6 +262,7 @@ class CustomFieldAPITest(APITestCase): response = self.client.get(url, **self.header) self.assertEqual(response.data['name'], self.sites[1].name) self.assertEqual(response.data['custom_fields']['text_field'], site2_cfvs['text_field']) + self.assertEqual(response.data['custom_fields']['longtext_field'], site2_cfvs['longtext_field']) self.assertEqual(response.data['custom_fields']['number_field'], site2_cfvs['number_field']) self.assertEqual(response.data['custom_fields']['boolean_field'], site2_cfvs['boolean_field']) self.assertEqual(response.data['custom_fields']['date_field'], site2_cfvs['date_field']) @@ -245,6 +286,7 @@ class CustomFieldAPITest(APITestCase): # Validate response data response_cf = response.data['custom_fields'] self.assertEqual(response_cf['text_field'], self.cf_text.default) + self.assertEqual(response_cf['longtext_field'], self.cf_longtext.default) self.assertEqual(response_cf['number_field'], self.cf_integer.default) self.assertEqual(response_cf['boolean_field'], self.cf_boolean.default) self.assertEqual(response_cf['date_field'], self.cf_date.default) @@ -254,6 +296,7 @@ class CustomFieldAPITest(APITestCase): # Validate database data site = Site.objects.get(pk=response.data['id']) self.assertEqual(site.custom_field_data['text_field'], self.cf_text.default) + self.assertEqual(site.custom_field_data['longtext_field'], self.cf_longtext.default) self.assertEqual(site.custom_field_data['number_field'], self.cf_integer.default) self.assertEqual(site.custom_field_data['boolean_field'], self.cf_boolean.default) self.assertEqual(str(site.custom_field_data['date_field']), self.cf_date.default) @@ -269,6 +312,7 @@ class CustomFieldAPITest(APITestCase): 'slug': 'site-3', 'custom_fields': { 'text_field': 'bar', + 'longtext_field': 'blah blah blah', 'number_field': 456, 'boolean_field': True, 'date_field': '2020-01-02', @@ -286,6 +330,7 @@ class CustomFieldAPITest(APITestCase): response_cf = response.data['custom_fields'] data_cf = data['custom_fields'] self.assertEqual(response_cf['text_field'], data_cf['text_field']) + self.assertEqual(response_cf['longtext_field'], data_cf['longtext_field']) self.assertEqual(response_cf['number_field'], data_cf['number_field']) self.assertEqual(response_cf['boolean_field'], data_cf['boolean_field']) self.assertEqual(response_cf['date_field'], data_cf['date_field']) @@ -295,6 +340,7 @@ class CustomFieldAPITest(APITestCase): # Validate database data site = Site.objects.get(pk=response.data['id']) self.assertEqual(site.custom_field_data['text_field'], data_cf['text_field']) + self.assertEqual(site.custom_field_data['longtext_field'], data_cf['longtext_field']) self.assertEqual(site.custom_field_data['number_field'], data_cf['number_field']) self.assertEqual(site.custom_field_data['boolean_field'], data_cf['boolean_field']) self.assertEqual(str(site.custom_field_data['date_field']), data_cf['date_field']) @@ -332,6 +378,7 @@ class CustomFieldAPITest(APITestCase): # Validate response data response_cf = response.data[i]['custom_fields'] self.assertEqual(response_cf['text_field'], self.cf_text.default) + self.assertEqual(response_cf['longtext_field'], self.cf_longtext.default) self.assertEqual(response_cf['number_field'], self.cf_integer.default) self.assertEqual(response_cf['boolean_field'], self.cf_boolean.default) self.assertEqual(response_cf['date_field'], self.cf_date.default) @@ -341,6 +388,7 @@ class CustomFieldAPITest(APITestCase): # Validate database data site = Site.objects.get(pk=response.data[i]['id']) self.assertEqual(site.custom_field_data['text_field'], self.cf_text.default) + self.assertEqual(site.custom_field_data['longtext_field'], self.cf_longtext.default) self.assertEqual(site.custom_field_data['number_field'], self.cf_integer.default) self.assertEqual(site.custom_field_data['boolean_field'], self.cf_boolean.default) self.assertEqual(str(site.custom_field_data['date_field']), self.cf_date.default) @@ -353,6 +401,7 @@ class CustomFieldAPITest(APITestCase): """ custom_field_data = { 'text_field': 'bar', + 'longtext_field': 'abcdefghij', 'number_field': 456, 'boolean_field': True, 'date_field': '2020-01-02', @@ -388,6 +437,7 @@ class CustomFieldAPITest(APITestCase): # Validate response data response_cf = response.data[i]['custom_fields'] self.assertEqual(response_cf['text_field'], custom_field_data['text_field']) + self.assertEqual(response_cf['longtext_field'], custom_field_data['longtext_field']) self.assertEqual(response_cf['number_field'], custom_field_data['number_field']) self.assertEqual(response_cf['boolean_field'], custom_field_data['boolean_field']) self.assertEqual(response_cf['date_field'], custom_field_data['date_field']) @@ -397,6 +447,7 @@ class CustomFieldAPITest(APITestCase): # Validate database data site = Site.objects.get(pk=response.data[i]['id']) self.assertEqual(site.custom_field_data['text_field'], custom_field_data['text_field']) + self.assertEqual(site.custom_field_data['longtext_field'], custom_field_data['longtext_field']) self.assertEqual(site.custom_field_data['number_field'], custom_field_data['number_field']) self.assertEqual(site.custom_field_data['boolean_field'], custom_field_data['boolean_field']) self.assertEqual(str(site.custom_field_data['date_field']), custom_field_data['date_field']) @@ -426,6 +477,7 @@ class CustomFieldAPITest(APITestCase): response_cf = response.data['custom_fields'] self.assertEqual(response_cf['text_field'], data['custom_fields']['text_field']) self.assertEqual(response_cf['number_field'], data['custom_fields']['number_field']) + self.assertEqual(response_cf['longtext_field'], original_cfvs['longtext_field']) self.assertEqual(response_cf['boolean_field'], original_cfvs['boolean_field']) self.assertEqual(response_cf['date_field'], original_cfvs['date_field']) self.assertEqual(response_cf['url_field'], original_cfvs['url_field']) @@ -435,6 +487,7 @@ class CustomFieldAPITest(APITestCase): site.refresh_from_db() self.assertEqual(site.custom_field_data['text_field'], data['custom_fields']['text_field']) self.assertEqual(site.custom_field_data['number_field'], data['custom_fields']['number_field']) + self.assertEqual(site.custom_field_data['longtext_field'], original_cfvs['longtext_field']) self.assertEqual(site.custom_field_data['boolean_field'], original_cfvs['boolean_field']) self.assertEqual(site.custom_field_data['date_field'], original_cfvs['date_field']) self.assertEqual(site.custom_field_data['url_field'], original_cfvs['url_field']) @@ -491,11 +544,14 @@ class CustomFieldImportTest(TestCase): custom_fields = ( CustomField(name='text', type=CustomFieldTypeChoices.TYPE_TEXT), + CustomField(name='longtext', type=CustomFieldTypeChoices.TYPE_LONGTEXT), CustomField(name='integer', type=CustomFieldTypeChoices.TYPE_INTEGER), CustomField(name='boolean', type=CustomFieldTypeChoices.TYPE_BOOLEAN), CustomField(name='date', type=CustomFieldTypeChoices.TYPE_DATE), CustomField(name='url', type=CustomFieldTypeChoices.TYPE_URL), - CustomField(name='select', type=CustomFieldTypeChoices.TYPE_SELECT, choices=['Choice A', 'Choice B', 'Choice C']), + CustomField(name='select', type=CustomFieldTypeChoices.TYPE_SELECT, choices=[ + 'Choice A', 'Choice B', 'Choice C', + ]), ) for cf in custom_fields: cf.save() @@ -506,10 +562,10 @@ class CustomFieldImportTest(TestCase): Import a Site in CSV format, including a value for each CustomField. """ data = ( - ('name', 'slug', 'status', 'cf_text', 'cf_integer', 'cf_boolean', 'cf_date', 'cf_url', 'cf_select'), - ('Site 1', 'site-1', 'active', 'ABC', '123', 'True', '2020-01-01', 'http://example.com/1', 'Choice A'), - ('Site 2', 'site-2', 'active', 'DEF', '456', 'False', '2020-01-02', 'http://example.com/2', 'Choice B'), - ('Site 3', 'site-3', 'active', '', '', '', '', '', ''), + ('name', 'slug', 'status', 'cf_text', 'cf_longtext', 'cf_integer', 'cf_boolean', 'cf_date', 'cf_url', 'cf_select'), + ('Site 1', 'site-1', 'active', 'ABC', 'Foo', '123', 'True', '2020-01-01', 'http://example.com/1', 'Choice A'), + ('Site 2', 'site-2', 'active', 'DEF', 'Bar', '456', 'False', '2020-01-02', 'http://example.com/2', 'Choice B'), + ('Site 3', 'site-3', 'active', '', '', '', '', '', '', ''), ) csv_data = '\n'.join(','.join(row) for row in data) @@ -518,8 +574,9 @@ class CustomFieldImportTest(TestCase): # Validate data for site 1 site1 = Site.objects.get(name='Site 1') - self.assertEqual(len(site1.custom_field_data), 6) + self.assertEqual(len(site1.custom_field_data), 7) self.assertEqual(site1.custom_field_data['text'], 'ABC') + self.assertEqual(site1.custom_field_data['longtext'], 'Foo') self.assertEqual(site1.custom_field_data['integer'], 123) self.assertEqual(site1.custom_field_data['boolean'], True) self.assertEqual(site1.custom_field_data['date'], '2020-01-01') @@ -528,8 +585,9 @@ class CustomFieldImportTest(TestCase): # Validate data for site 2 site2 = Site.objects.get(name='Site 2') - self.assertEqual(len(site2.custom_field_data), 6) + self.assertEqual(len(site2.custom_field_data), 7) self.assertEqual(site2.custom_field_data['text'], 'DEF') + self.assertEqual(site2.custom_field_data['longtext'], 'Bar') self.assertEqual(site2.custom_field_data['integer'], 456) self.assertEqual(site2.custom_field_data['boolean'], False) self.assertEqual(site2.custom_field_data['date'], '2020-01-02') diff --git a/netbox/extras/tests/test_forms.py b/netbox/extras/tests/test_forms.py index cb0a9c081..1ccc2332b 100644 --- a/netbox/extras/tests/test_forms.py +++ b/netbox/extras/tests/test_forms.py @@ -17,6 +17,9 @@ class CustomFieldModelFormTest(TestCase): cf_text = CustomField.objects.create(name='text', type=CustomFieldTypeChoices.TYPE_TEXT) cf_text.content_types.set([obj_type]) + cf_longtext = CustomField.objects.create(name='longtext', type=CustomFieldTypeChoices.TYPE_LONGTEXT) + cf_longtext.content_types.set([obj_type]) + cf_integer = CustomField.objects.create(name='integer', type=CustomFieldTypeChoices.TYPE_INTEGER) cf_integer.content_types.set([obj_type]) diff --git a/netbox/templates/inc/custom_fields_panel.html b/netbox/templates/inc/custom_fields_panel.html index fd0379961..eb6e490e7 100644 --- a/netbox/templates/inc/custom_fields_panel.html +++ b/netbox/templates/inc/custom_fields_panel.html @@ -1,3 +1,5 @@ +{% load helpers %} + {% with custom_fields=object.get_custom_fields %} {% if custom_fields %}
@@ -10,7 +12,9 @@ {{ field }} - {% if field.type == 'boolean' and value == True %} + {% if field.type == 'longtext' and value %} + {{ value|render_markdown }} + {% elif field.type == 'boolean' and value == True %} {% elif field.type == 'boolean' and value == False %}