13983 fix escape handling

This commit is contained in:
Arthur 2023-12-13 08:34:26 -08:00
parent f49c310f23
commit e13dd5d905
3 changed files with 17 additions and 5 deletions

View File

@ -96,6 +96,8 @@ class CustomFieldChoiceSetImportForm(CSVModelForm):
for line in self.cleaned_data['extra_choices']: for line in self.cleaned_data['extra_choices']:
try: try:
value, label = re.split(r'(?<!\\):', line, maxsplit=1) value, label = re.split(r'(?<!\\):', line, maxsplit=1)
value = value.replace('\\:', ':')
label = label.replace('\\:', ':')
except ValueError: except ValueError:
value, label = line, line value, label = line, line
data.append((value, label)) data.append((value, label))

View File

@ -104,11 +104,25 @@ class CustomFieldChoiceSetForm(BootstrapMixin, forms.ModelForm):
model = CustomFieldChoiceSet model = CustomFieldChoiceSet
fields = ('name', 'description', 'base_choices', 'extra_choices', 'order_alphabetically') fields = ('name', 'description', 'base_choices', 'extra_choices', 'order_alphabetically')
def __init__(self, *args, initial=None, **kwargs):
super().__init__(*args, initial=initial, **kwargs)
# Escape colons in extra_choices
if 'extra_choices' in self.initial:
choices = []
for choice in self.initial['extra_choices']:
choice = (choice[0].replace(':', '\\:'), choice[1].replace(':', '\\:'))
choices.append(choice)
self.initial['extra_choices'] = choices
def clean_extra_choices(self): def clean_extra_choices(self):
data = [] data = []
for line in self.cleaned_data['extra_choices'].splitlines(): for line in self.cleaned_data['extra_choices'].splitlines():
try: try:
value, label = re.split(r'(?<!\\):', line, maxsplit=1) value, label = re.split(r'(?<!\\):', line, maxsplit=1)
value = value.replace('\\:', ':')
label = label.replace('\\:', ':')
except ValueError: except ValueError:
value, label = line, line value, label = line, line
data.append((value, label)) data.append((value, label))

View File

@ -748,11 +748,7 @@ class CustomFieldChoiceSet(CloningMixin, ExportTemplatesMixin, ChangeLoggedModel
if self.base_choices: if self.base_choices:
self._choices.extend(CHOICE_SETS.get(self.base_choices)) self._choices.extend(CHOICE_SETS.get(self.base_choices))
if self.extra_choices: if self.extra_choices:
extra_choices = [] self._choices.extend(self.extra_choices)
for choice in self.extra_choices:
choice = (choice[0], choice[1].replace('\\:', ':'))
extra_choices.append(choice)
self._choices.extend(extra_choices)
if self.order_alphabetically: if self.order_alphabetically:
self._choices = sorted(self._choices, key=lambda x: x[0]) self._choices = sorted(self._choices, key=lambda x: x[0])
return self._choices return self._choices