Skip to content

Commit

Permalink
Disallow multichoice choices with double quotes
Browse files Browse the repository at this point in the history
Changes
-------
- Styling for double quotes errors
- UDF level validation preventing multichoice
  datatypes from including double quotes

--

Connects to OpenTreeMap/otm-addons#1372
  • Loading branch information
RobinIsTheBird committed Apr 18, 2017
1 parent fffa34c commit 879f5ee
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions assets/css/sass/partials/pages/_admin.scss
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,11 @@
display: block;
}
}
&.no-double-quotes-error {
[data-item="no-double-quotes"] {
display: block;
}
}
&.reused-error {
[data-item="reused"] {
display: block;
Expand Down
23 changes: 23 additions & 0 deletions opentreemap/treemap/tests/test_udfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,29 @@ def test_default_values(self):
'default': 'anything'}],
iscollection=True)

def test_create_multiple_choice_udf(self):
UserDefinedFieldDefinition.objects.create(
instance=self.instance,
model_type='Plot',
datatype=json.dumps({
'type': 'multichoice',
'choices': ['a', 'b']
}),
iscollection=False,
name='a name')

def test_cannot_create_multiple_choice_udf_with_double_quotes(self):
with self.assertRaises(ValidationError):
UserDefinedFieldDefinition.objects.create(
instance=self.instance,
model_type='Plot',
datatype=json.dumps({
'type': 'multichoice',
'choices': ['a', 'b"']
}),
iscollection=False,
name='a name')

def test_invalid_names(self):
with self.assertRaises(ValidationError):
UserDefinedFieldDefinition.objects.create(
Expand Down
5 changes: 5 additions & 0 deletions opentreemap/treemap/udf.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,11 @@ def _validate_single_datatype(self, datatype):
if len(choices) != len(set(choices)):
raise ValidationError(_('Duplicate choices are not allowed'))

if datatype['type'] == 'multichoice':
if 0 < len([c for c in choices if '"' in c]):
raise ValidationError(_('Double quotes are not allowed'
'in multiple choice fields'))

if 'default' in datatype:
try:
self.clean_value(datatype['default'], datatype)
Expand Down

0 comments on commit 879f5ee

Please sign in to comment.