mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 01:41:22 -06:00
Closes #6711: Add longtext custom field type with Markdown support
This commit is contained in:
parent
3e7922e41e
commit
176bd2396b
@ -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:
|
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)
|
* 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)
|
* Integer: A whole number (positive or negative)
|
||||||
* Boolean: True or false
|
* Boolean: True or false
|
||||||
* Date: A date in ISO 8601 format (YYYY-MM-DD)
|
* Date: A date in ISO 8601 format (YYYY-MM-DD)
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
### Enhancements
|
### Enhancements
|
||||||
|
|
||||||
* [#1337](https://github.com/netbox-community/netbox/issues/1337) - Add WWN field to interfaces
|
* [#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
|
* [#6874](https://github.com/netbox-community/netbox/issues/6874) - Add tenant assignment for locations
|
||||||
|
|
||||||
### Other Changes
|
### Other Changes
|
||||||
|
@ -8,6 +8,7 @@ from utilities.choices import ChoiceSet
|
|||||||
class CustomFieldTypeChoices(ChoiceSet):
|
class CustomFieldTypeChoices(ChoiceSet):
|
||||||
|
|
||||||
TYPE_TEXT = 'text'
|
TYPE_TEXT = 'text'
|
||||||
|
TYPE_LONGTEXT = 'longtext'
|
||||||
TYPE_INTEGER = 'integer'
|
TYPE_INTEGER = 'integer'
|
||||||
TYPE_BOOLEAN = 'boolean'
|
TYPE_BOOLEAN = 'boolean'
|
||||||
TYPE_DATE = 'date'
|
TYPE_DATE = 'date'
|
||||||
@ -17,6 +18,7 @@ class CustomFieldTypeChoices(ChoiceSet):
|
|||||||
|
|
||||||
CHOICES = (
|
CHOICES = (
|
||||||
(TYPE_TEXT, 'Text'),
|
(TYPE_TEXT, 'Text'),
|
||||||
|
(TYPE_LONGTEXT, 'Text (long)'),
|
||||||
(TYPE_INTEGER, 'Integer'),
|
(TYPE_INTEGER, 'Integer'),
|
||||||
(TYPE_BOOLEAN, 'Boolean (true/false)'),
|
(TYPE_BOOLEAN, 'Boolean (true/false)'),
|
||||||
(TYPE_DATE, 'Date'),
|
(TYPE_DATE, 'Date'),
|
||||||
|
@ -166,7 +166,10 @@ class CustomField(ChangeLoggedModel):
|
|||||||
# Validate the field's default value (if any)
|
# Validate the field's default value (if any)
|
||||||
if self.default is not None:
|
if self.default is not None:
|
||||||
try:
|
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)
|
self.validate(default_value)
|
||||||
except ValidationError as err:
|
except ValidationError as err:
|
||||||
raise ValidationError({
|
raise ValidationError({
|
||||||
@ -184,7 +187,11 @@ class CustomField(ChangeLoggedModel):
|
|||||||
})
|
})
|
||||||
|
|
||||||
# Regex validation can be set only for text fields
|
# 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:
|
if self.validation_regex and self.type not in regex_types:
|
||||||
raise ValidationError({
|
raise ValidationError({
|
||||||
'validation_regex': "Regular expression validation is supported only for text and URL fields"
|
'validation_regex': "Regular expression validation is supported only for text and URL fields"
|
||||||
@ -275,7 +282,13 @@ class CustomField(ChangeLoggedModel):
|
|||||||
|
|
||||||
# Text
|
# Text
|
||||||
else:
|
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:
|
if self.validation_regex:
|
||||||
field.validators = [
|
field.validators = [
|
||||||
RegexValidator(
|
RegexValidator(
|
||||||
@ -298,7 +311,7 @@ class CustomField(ChangeLoggedModel):
|
|||||||
if value not in [None, '']:
|
if value not in [None, '']:
|
||||||
|
|
||||||
# Validate text field
|
# 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:
|
if type(value) is not str:
|
||||||
raise ValidationError(f"Value must be a string.")
|
raise ValidationError(f"Value must be a string.")
|
||||||
if self.validation_regex and not re.match(self.validation_regex, value):
|
if self.validation_regex and not re.match(self.validation_regex, value):
|
||||||
|
@ -24,13 +24,46 @@ class CustomFieldTest(TestCase):
|
|||||||
|
|
||||||
def test_simple_fields(self):
|
def test_simple_fields(self):
|
||||||
DATA = (
|
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_TEXT,
|
||||||
{'field_type': CustomFieldTypeChoices.TYPE_INTEGER, 'field_value': 42, 'empty_value': None},
|
'field_value': 'Foobar!',
|
||||||
{'field_type': CustomFieldTypeChoices.TYPE_BOOLEAN, 'field_value': True, 'empty_value': None},
|
'empty_value': '',
|
||||||
{'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_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)
|
obj_type = ContentType.objects.get_for_model(Site)
|
||||||
@ -149,6 +182,11 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
cls.cf_text.save()
|
cls.cf_text.save()
|
||||||
cls.cf_text.content_types.set([content_type])
|
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
|
# Integer custom field
|
||||||
cls.cf_integer = CustomField(type=CustomFieldTypeChoices.TYPE_INTEGER, name='number_field', default=123)
|
cls.cf_integer = CustomField(type=CustomFieldTypeChoices.TYPE_INTEGER, name='number_field', default=123)
|
||||||
cls.cf_integer.save()
|
cls.cf_integer.save()
|
||||||
@ -185,6 +223,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
# Assign custom field values for site 2
|
# Assign custom field values for site 2
|
||||||
cls.sites[1].custom_field_data = {
|
cls.sites[1].custom_field_data = {
|
||||||
cls.cf_text.name: 'bar',
|
cls.cf_text.name: 'bar',
|
||||||
|
cls.cf_longtext.name: 'DEF',
|
||||||
cls.cf_integer.name: 456,
|
cls.cf_integer.name: 456,
|
||||||
cls.cf_boolean.name: True,
|
cls.cf_boolean.name: True,
|
||||||
cls.cf_date.name: '2020-01-02',
|
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['name'], self.sites[0].name)
|
||||||
self.assertEqual(response.data['custom_fields'], {
|
self.assertEqual(response.data['custom_fields'], {
|
||||||
'text_field': None,
|
'text_field': None,
|
||||||
|
'longtext_field': None,
|
||||||
'number_field': None,
|
'number_field': None,
|
||||||
'boolean_field': None,
|
'boolean_field': None,
|
||||||
'date_field': None,
|
'date_field': None,
|
||||||
@ -222,6 +262,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
response = self.client.get(url, **self.header)
|
response = self.client.get(url, **self.header)
|
||||||
self.assertEqual(response.data['name'], self.sites[1].name)
|
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']['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']['number_field'], site2_cfvs['number_field'])
|
||||||
self.assertEqual(response.data['custom_fields']['boolean_field'], site2_cfvs['boolean_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'])
|
self.assertEqual(response.data['custom_fields']['date_field'], site2_cfvs['date_field'])
|
||||||
@ -245,6 +286,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
# Validate response data
|
# Validate response data
|
||||||
response_cf = response.data['custom_fields']
|
response_cf = response.data['custom_fields']
|
||||||
self.assertEqual(response_cf['text_field'], self.cf_text.default)
|
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['number_field'], self.cf_integer.default)
|
||||||
self.assertEqual(response_cf['boolean_field'], self.cf_boolean.default)
|
self.assertEqual(response_cf['boolean_field'], self.cf_boolean.default)
|
||||||
self.assertEqual(response_cf['date_field'], self.cf_date.default)
|
self.assertEqual(response_cf['date_field'], self.cf_date.default)
|
||||||
@ -254,6 +296,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
# Validate database data
|
# Validate database data
|
||||||
site = Site.objects.get(pk=response.data['id'])
|
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['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['number_field'], self.cf_integer.default)
|
||||||
self.assertEqual(site.custom_field_data['boolean_field'], self.cf_boolean.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)
|
self.assertEqual(str(site.custom_field_data['date_field']), self.cf_date.default)
|
||||||
@ -269,6 +312,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
'slug': 'site-3',
|
'slug': 'site-3',
|
||||||
'custom_fields': {
|
'custom_fields': {
|
||||||
'text_field': 'bar',
|
'text_field': 'bar',
|
||||||
|
'longtext_field': 'blah blah blah',
|
||||||
'number_field': 456,
|
'number_field': 456,
|
||||||
'boolean_field': True,
|
'boolean_field': True,
|
||||||
'date_field': '2020-01-02',
|
'date_field': '2020-01-02',
|
||||||
@ -286,6 +330,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
response_cf = response.data['custom_fields']
|
response_cf = response.data['custom_fields']
|
||||||
data_cf = data['custom_fields']
|
data_cf = data['custom_fields']
|
||||||
self.assertEqual(response_cf['text_field'], data_cf['text_field'])
|
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['number_field'], data_cf['number_field'])
|
||||||
self.assertEqual(response_cf['boolean_field'], data_cf['boolean_field'])
|
self.assertEqual(response_cf['boolean_field'], data_cf['boolean_field'])
|
||||||
self.assertEqual(response_cf['date_field'], data_cf['date_field'])
|
self.assertEqual(response_cf['date_field'], data_cf['date_field'])
|
||||||
@ -295,6 +340,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
# Validate database data
|
# Validate database data
|
||||||
site = Site.objects.get(pk=response.data['id'])
|
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['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['number_field'], data_cf['number_field'])
|
||||||
self.assertEqual(site.custom_field_data['boolean_field'], data_cf['boolean_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'])
|
self.assertEqual(str(site.custom_field_data['date_field']), data_cf['date_field'])
|
||||||
@ -332,6 +378,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
# Validate response data
|
# Validate response data
|
||||||
response_cf = response.data[i]['custom_fields']
|
response_cf = response.data[i]['custom_fields']
|
||||||
self.assertEqual(response_cf['text_field'], self.cf_text.default)
|
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['number_field'], self.cf_integer.default)
|
||||||
self.assertEqual(response_cf['boolean_field'], self.cf_boolean.default)
|
self.assertEqual(response_cf['boolean_field'], self.cf_boolean.default)
|
||||||
self.assertEqual(response_cf['date_field'], self.cf_date.default)
|
self.assertEqual(response_cf['date_field'], self.cf_date.default)
|
||||||
@ -341,6 +388,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
# Validate database data
|
# Validate database data
|
||||||
site = Site.objects.get(pk=response.data[i]['id'])
|
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['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['number_field'], self.cf_integer.default)
|
||||||
self.assertEqual(site.custom_field_data['boolean_field'], self.cf_boolean.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)
|
self.assertEqual(str(site.custom_field_data['date_field']), self.cf_date.default)
|
||||||
@ -353,6 +401,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
"""
|
"""
|
||||||
custom_field_data = {
|
custom_field_data = {
|
||||||
'text_field': 'bar',
|
'text_field': 'bar',
|
||||||
|
'longtext_field': 'abcdefghij',
|
||||||
'number_field': 456,
|
'number_field': 456,
|
||||||
'boolean_field': True,
|
'boolean_field': True,
|
||||||
'date_field': '2020-01-02',
|
'date_field': '2020-01-02',
|
||||||
@ -388,6 +437,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
# Validate response data
|
# Validate response data
|
||||||
response_cf = response.data[i]['custom_fields']
|
response_cf = response.data[i]['custom_fields']
|
||||||
self.assertEqual(response_cf['text_field'], custom_field_data['text_field'])
|
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['number_field'], custom_field_data['number_field'])
|
||||||
self.assertEqual(response_cf['boolean_field'], custom_field_data['boolean_field'])
|
self.assertEqual(response_cf['boolean_field'], custom_field_data['boolean_field'])
|
||||||
self.assertEqual(response_cf['date_field'], custom_field_data['date_field'])
|
self.assertEqual(response_cf['date_field'], custom_field_data['date_field'])
|
||||||
@ -397,6 +447,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
# Validate database data
|
# Validate database data
|
||||||
site = Site.objects.get(pk=response.data[i]['id'])
|
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['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['number_field'], custom_field_data['number_field'])
|
||||||
self.assertEqual(site.custom_field_data['boolean_field'], custom_field_data['boolean_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'])
|
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']
|
response_cf = response.data['custom_fields']
|
||||||
self.assertEqual(response_cf['text_field'], data['custom_fields']['text_field'])
|
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['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['boolean_field'], original_cfvs['boolean_field'])
|
||||||
self.assertEqual(response_cf['date_field'], original_cfvs['date_field'])
|
self.assertEqual(response_cf['date_field'], original_cfvs['date_field'])
|
||||||
self.assertEqual(response_cf['url_field'], original_cfvs['url_field'])
|
self.assertEqual(response_cf['url_field'], original_cfvs['url_field'])
|
||||||
@ -435,6 +487,7 @@ class CustomFieldAPITest(APITestCase):
|
|||||||
site.refresh_from_db()
|
site.refresh_from_db()
|
||||||
self.assertEqual(site.custom_field_data['text_field'], data['custom_fields']['text_field'])
|
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['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['boolean_field'], original_cfvs['boolean_field'])
|
||||||
self.assertEqual(site.custom_field_data['date_field'], original_cfvs['date_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'])
|
self.assertEqual(site.custom_field_data['url_field'], original_cfvs['url_field'])
|
||||||
@ -491,11 +544,14 @@ class CustomFieldImportTest(TestCase):
|
|||||||
|
|
||||||
custom_fields = (
|
custom_fields = (
|
||||||
CustomField(name='text', type=CustomFieldTypeChoices.TYPE_TEXT),
|
CustomField(name='text', type=CustomFieldTypeChoices.TYPE_TEXT),
|
||||||
|
CustomField(name='longtext', type=CustomFieldTypeChoices.TYPE_LONGTEXT),
|
||||||
CustomField(name='integer', type=CustomFieldTypeChoices.TYPE_INTEGER),
|
CustomField(name='integer', type=CustomFieldTypeChoices.TYPE_INTEGER),
|
||||||
CustomField(name='boolean', type=CustomFieldTypeChoices.TYPE_BOOLEAN),
|
CustomField(name='boolean', type=CustomFieldTypeChoices.TYPE_BOOLEAN),
|
||||||
CustomField(name='date', type=CustomFieldTypeChoices.TYPE_DATE),
|
CustomField(name='date', type=CustomFieldTypeChoices.TYPE_DATE),
|
||||||
CustomField(name='url', type=CustomFieldTypeChoices.TYPE_URL),
|
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:
|
for cf in custom_fields:
|
||||||
cf.save()
|
cf.save()
|
||||||
@ -506,10 +562,10 @@ class CustomFieldImportTest(TestCase):
|
|||||||
Import a Site in CSV format, including a value for each CustomField.
|
Import a Site in CSV format, including a value for each CustomField.
|
||||||
"""
|
"""
|
||||||
data = (
|
data = (
|
||||||
('name', 'slug', 'status', 'cf_text', 'cf_integer', 'cf_boolean', 'cf_date', 'cf_url', 'cf_select'),
|
('name', 'slug', 'status', 'cf_text', 'cf_longtext', '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 1', 'site-1', 'active', 'ABC', 'Foo', '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 2', 'site-2', 'active', 'DEF', 'Bar', '456', 'False', '2020-01-02', 'http://example.com/2', 'Choice B'),
|
||||||
('Site 3', 'site-3', 'active', '', '', '', '', '', ''),
|
('Site 3', 'site-3', 'active', '', '', '', '', '', '', ''),
|
||||||
)
|
)
|
||||||
csv_data = '\n'.join(','.join(row) for row in data)
|
csv_data = '\n'.join(','.join(row) for row in data)
|
||||||
|
|
||||||
@ -518,8 +574,9 @@ class CustomFieldImportTest(TestCase):
|
|||||||
|
|
||||||
# Validate data for site 1
|
# Validate data for site 1
|
||||||
site1 = Site.objects.get(name='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['text'], 'ABC')
|
||||||
|
self.assertEqual(site1.custom_field_data['longtext'], 'Foo')
|
||||||
self.assertEqual(site1.custom_field_data['integer'], 123)
|
self.assertEqual(site1.custom_field_data['integer'], 123)
|
||||||
self.assertEqual(site1.custom_field_data['boolean'], True)
|
self.assertEqual(site1.custom_field_data['boolean'], True)
|
||||||
self.assertEqual(site1.custom_field_data['date'], '2020-01-01')
|
self.assertEqual(site1.custom_field_data['date'], '2020-01-01')
|
||||||
@ -528,8 +585,9 @@ class CustomFieldImportTest(TestCase):
|
|||||||
|
|
||||||
# Validate data for site 2
|
# Validate data for site 2
|
||||||
site2 = Site.objects.get(name='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['text'], 'DEF')
|
||||||
|
self.assertEqual(site2.custom_field_data['longtext'], 'Bar')
|
||||||
self.assertEqual(site2.custom_field_data['integer'], 456)
|
self.assertEqual(site2.custom_field_data['integer'], 456)
|
||||||
self.assertEqual(site2.custom_field_data['boolean'], False)
|
self.assertEqual(site2.custom_field_data['boolean'], False)
|
||||||
self.assertEqual(site2.custom_field_data['date'], '2020-01-02')
|
self.assertEqual(site2.custom_field_data['date'], '2020-01-02')
|
||||||
|
@ -17,6 +17,9 @@ class CustomFieldModelFormTest(TestCase):
|
|||||||
cf_text = CustomField.objects.create(name='text', type=CustomFieldTypeChoices.TYPE_TEXT)
|
cf_text = CustomField.objects.create(name='text', type=CustomFieldTypeChoices.TYPE_TEXT)
|
||||||
cf_text.content_types.set([obj_type])
|
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 = CustomField.objects.create(name='integer', type=CustomFieldTypeChoices.TYPE_INTEGER)
|
||||||
cf_integer.content_types.set([obj_type])
|
cf_integer.content_types.set([obj_type])
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
{% load helpers %}
|
||||||
|
|
||||||
{% with custom_fields=object.get_custom_fields %}
|
{% with custom_fields=object.get_custom_fields %}
|
||||||
{% if custom_fields %}
|
{% if custom_fields %}
|
||||||
<div class="card">
|
<div class="card">
|
||||||
@ -10,7 +12,9 @@
|
|||||||
<tr>
|
<tr>
|
||||||
<td><span title="{{ field.description }}">{{ field }}</span></td>
|
<td><span title="{{ field.description }}">{{ field }}</span></td>
|
||||||
<td>
|
<td>
|
||||||
{% if field.type == 'boolean' and value == True %}
|
{% if field.type == 'longtext' and value %}
|
||||||
|
{{ value|render_markdown }}
|
||||||
|
{% elif field.type == 'boolean' and value == True %}
|
||||||
<i class="mdi mdi-check-bold text-success" title="True"></i>
|
<i class="mdi mdi-check-bold text-success" title="True"></i>
|
||||||
{% elif field.type == 'boolean' and value == False %}
|
{% elif field.type == 'boolean' and value == False %}
|
||||||
<i class="mdi mdi-close-thick text-danger" title="False"></i>
|
<i class="mdi mdi-close-thick text-danger" title="False"></i>
|
||||||
|
Loading…
Reference in New Issue
Block a user