Fix post old format

This commit is contained in:
shamoon 2025-01-25 21:47:16 -08:00
parent 1566011f0b
commit 1ee7728fc5
2 changed files with 26 additions and 28 deletions

View File

@ -645,26 +645,10 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer):
custom_field.data_type,
)
api_version = int(
self.context.get("request").version
if self.context.get("request")
else settings.REST_FRAMEWORK["DEFAULT_VERSION"],
)
if custom_field.data_type == CustomField.FieldDataType.DOCUMENTLINK:
# prior to update so we can look for any docs that are going to be removed
self.reflect_doclinks(document, custom_field, validated_data["value"])
if (
custom_field.data_type == CustomField.FieldDataType.SELECT
and api_version < 7
):
# Convert the index of the option in the field.extra_data["select_options"] list
# to the actual value
validated_data["value"] = custom_field.extra_data["select_options"][
validated_data["value"]
]["id"]
# Actually update or create the instance, providing the value
# to fill in the correct attribute based on the type
instance, _ = CustomFieldInstance.objects.update_or_create(
@ -702,6 +686,12 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer):
"""
data = super().validate(data)
field: CustomField = data["field"]
api_version = int(
self.context.get("request").version
if self.context.get("request")
else settings.REST_FRAMEWORK["DEFAULT_VERSION"],
)
if "value" in data and data["value"] is not None:
if (
field.data_type == CustomField.FieldDataType.URL
@ -729,6 +719,14 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer):
MaxLengthValidator(limit_value=128)(data["value"])
elif field.data_type == CustomField.FieldDataType.SELECT:
select_options = field.extra_data["select_options"]
if api_version < 7:
# Convert the index of the option in the field.extra_data["select_options"]
# list to the options unique id
data["value"] = field.extra_data["select_options"][data["value"]][
"id"
]
try:
next(
option

View File

@ -356,23 +356,22 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
checksum="123",
mime_type="application/pdf",
)
CustomFieldInstance.objects.create(
document=doc,
field=custom_field_select,
value_select="abc-123",
)
resp = self.client.patch(
f"/api/documents/{doc.id}/",
headers={"Accept": "application/json; version=6"},
data={
data=json.dumps(
{
"custom_fields": [
{"field": custom_field_select.id, "value": 0},
{"field": custom_field_select.id, "value": 1},
],
},
),
content_type="application/json",
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertEqual(doc.custom_fields.first().value, "abc-123")
doc.refresh_from_db()
self.assertEqual(doc.custom_fields.first().value, "def-456")
resp = self.client.get(
f"/api/documents/{doc.id}/",
@ -381,7 +380,7 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
self.assertEqual(resp.status_code, status.HTTP_200_OK)
data = resp.json()
self.assertEqual(data["custom_fields"][0]["value"], 0)
self.assertEqual(data["custom_fields"][0]["value"], 1)
def test_create_custom_field_monetary_validation(self):
"""
@ -980,6 +979,7 @@ class TestCustomFieldsAPI(DirectoriesMixin, APITestCase):
resp = self.client.patch(
f"/api/documents/{doc.id}/",
headers={"Accept": "application/json; version=7"},
data={
"custom_fields": [
{"field": custom_field_select.id, "value": "not an option"},