Frontend UI for creating select fields, alter extra_data format
This commit is contained in:
@@ -480,10 +480,14 @@ class CustomFieldSerializer(serializers.ModelSerializer):
|
||||
if (
|
||||
"data_type" in attrs
|
||||
and attrs["data_type"] == CustomField.FieldDataType.SELECT
|
||||
and ("extra_data" not in attrs or not isinstance(attrs["extra_data"], list))
|
||||
and (
|
||||
"extra_data" not in attrs
|
||||
or "select_options" not in attrs["extra_data"]
|
||||
or not isinstance(attrs["extra_data"]["select_options"], list)
|
||||
)
|
||||
):
|
||||
raise serializers.ValidationError(
|
||||
{"error": "extra_data must be a list"},
|
||||
{"error": "extra_data.select_options must be a list"},
|
||||
)
|
||||
return super().validate(attrs)
|
||||
|
||||
@@ -574,11 +578,12 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer):
|
||||
elif field.data_type == CustomField.FieldDataType.STRING:
|
||||
MaxLengthValidator(limit_value=128)(data["value"])
|
||||
elif field.data_type == CustomField.FieldDataType.SELECT:
|
||||
select_options = field.extra_data["select_options"]
|
||||
try:
|
||||
field.extra_data[data["value"]]
|
||||
select_options[data["value"]]
|
||||
except Exception:
|
||||
raise serializers.ValidationError(
|
||||
f"Value must be index of an element in {field.extra_data}",
|
||||
f"Value must be index of an element in {select_options}",
|
||||
)
|
||||
|
||||
return data
|
||||
|
||||
@@ -59,7 +59,9 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
{
|
||||
"data_type": "select",
|
||||
"name": "Select Field",
|
||||
"extra_data": ["Option 1", "Option 2"],
|
||||
"extra_data": {
|
||||
"select_options": ["Option 1", "Option 2"],
|
||||
},
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
@@ -68,7 +70,10 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
|
||||
data = resp.json()
|
||||
|
||||
self.assertCountEqual(data["extra_data"], ["Option 1", "Option 2"])
|
||||
self.assertCountEqual(
|
||||
data["extra_data"]["select_options"],
|
||||
["Option 1", "Option 2"],
|
||||
)
|
||||
|
||||
def test_create_custom_field_nonunique_name(self):
|
||||
"""
|
||||
@@ -93,6 +98,45 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_custom_field_select_invalid_options(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- Custom field does not exist
|
||||
WHEN:
|
||||
- API request to create custom field with invalid select options
|
||||
THEN:
|
||||
- HTTP 400 is returned
|
||||
"""
|
||||
|
||||
# Not a list
|
||||
resp = self.client.post(
|
||||
self.ENDPOINT,
|
||||
json.dumps(
|
||||
{
|
||||
"data_type": "select",
|
||||
"name": "Select Field",
|
||||
"extra_data": {
|
||||
"select_options": "not a list",
|
||||
},
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# No options
|
||||
resp = self.client.post(
|
||||
self.ENDPOINT,
|
||||
json.dumps(
|
||||
{
|
||||
"data_type": "select",
|
||||
"name": "Select Field",
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
def test_create_custom_field_instance(self):
|
||||
"""
|
||||
GIVEN:
|
||||
@@ -155,7 +199,9 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
custom_field_select = CustomField.objects.create(
|
||||
name="Test Custom Field Select",
|
||||
data_type=CustomField.FieldDataType.SELECT,
|
||||
extra_data=["Option 1", "Option 2"],
|
||||
extra_data={
|
||||
"select_options": ["Option 1", "Option 2"],
|
||||
},
|
||||
)
|
||||
|
||||
date_value = date.today()
|
||||
@@ -614,7 +660,9 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
|
||||
custom_field_select = CustomField.objects.create(
|
||||
name="Test Custom Field SELECT",
|
||||
data_type=CustomField.FieldDataType.SELECT,
|
||||
extra_data=["Option 1", "Option 2"],
|
||||
extra_data={
|
||||
"select_options": ["Option 1", "Option 2"],
|
||||
},
|
||||
)
|
||||
|
||||
resp = self.client.patch(
|
||||
|
||||
Reference in New Issue
Block a user