Fix YAML quoting issues.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2018-08-20 02:49:10 -04:00
parent 04868e66bc
commit 958f85eb1e
2 changed files with 24 additions and 17 deletions

View File

@@ -15,18 +15,28 @@ class SettingForm(forms.Form):
def __init__(self, *args, **kwargs):
super(SettingForm, self).__init__(*args, **kwargs)
self.fields['value'].help_text = self.initial['setting'].help_text
self.fields['value'].initial = self.initial['setting'].serialized_value
self.setting = self.initial['setting']
self.fields['value'].help_text = self.setting.help_text
self.fields['value'].initial = self.setting.serialized_value
def clean(self):
quotes = ['"', "'"]
if self.setting.quoted:
stripped = self.cleaned_data['value'].strip()
if stripped[0] not in quotes or stripped[-1] not in quotes:
raise ValidationError(
_(
'Value must be properly quoted.'
)
)
try:
yaml.safe_load(self.cleaned_data['value'])
except yaml.YAMLError as exception:
try:
yaml.safe_load('{}'.format(self.cleaned_data['value']))
except yaml.YAMLError as exception:
raise ValidationError(
_(
'"%s" not a valid entry.'
) % self.cleaned_data['value']
)
raise ValidationError(
_(
'"%s" not a valid entry.'
) % self.cleaned_data['value']
)