From 69bf1472d2e80bc2916020b06cb71d87eb9ccfaa Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Thu, 14 Dec 2023 12:18:56 -0800 Subject: [PATCH] 13983 Add nested arrays for extra_choices in CustomFieldChoiceSet (#14470) * 13983 split array fields in CSV data for CustomFieldChoices * 13983 fix help text * 13983 update tests * 13983 use re for split * 13983 replace escaped chars * 13983 fix escape handling * 13983 fix escape handling * 13983 fix escape handling --- netbox/extras/forms/bulk_import.py | 20 +++++++++++++++++++- netbox/extras/forms/model_forms.py | 14 ++++++++++++++ netbox/extras/tests/test_views.py | 12 +++++++++--- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/netbox/extras/forms/bulk_import.py b/netbox/extras/forms/bulk_import.py index 79023a74d..745268f33 100644 --- a/netbox/extras/forms/bulk_import.py +++ b/netbox/extras/forms/bulk_import.py @@ -1,3 +1,5 @@ +import re + from django import forms from django.contrib.contenttypes.models import ContentType from django.contrib.postgres.forms import SimpleArrayField @@ -76,7 +78,10 @@ class CustomFieldChoiceSetImportForm(CSVModelForm): extra_choices = SimpleArrayField( base_field=forms.CharField(), required=False, - help_text=_('Comma-separated list of field choices') + help_text=_( + 'Quoted string of comma-separated field choices with optional labels separated by colon: ' + '"choice1:First Choice,choice2:Second Choice"' + ) ) class Meta: @@ -85,6 +90,19 @@ class CustomFieldChoiceSetImportForm(CSVModelForm): 'name', 'description', 'extra_choices', 'order_alphabetically', ) + def clean_extra_choices(self): + if isinstance(self.cleaned_data['extra_choices'], list): + data = [] + for line in self.cleaned_data['extra_choices']: + try: + value, label = re.split(r'(?