Fix symmetric doc links with target value None (#5187)

This commit is contained in:
shamoon 2023-12-31 07:56:26 -08:00 committed by GitHub
parent b93f655039
commit c5d622279c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -566,6 +566,9 @@ class CustomFieldInstanceSerializer(serializers.ModelSerializer):
value_document_ids=[document.id],
),
)
elif target_doc_field_instance.value is None:
target_doc_field_instance.value_document_ids = [document.id]
custom_field_instances_to_update.append(target_doc_field_instance)
elif document.id not in target_doc_field_instance.value:
target_doc_field_instance.value_document_ids.append(document.id)
custom_field_instances_to_update.append(target_doc_field_instance)

View File

@ -530,7 +530,7 @@ class TestCustomField(DirectoriesMixin, APITestCase):
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
def test_bidirectional_doclink_fields(self):
def test_symmetric_doclink_fields(self):
"""
GIVEN:
- Existing document
@ -637,3 +637,28 @@ class TestCustomField(DirectoriesMixin, APITestCase):
self.assertEqual(doc2.custom_fields.first().value, [])
self.assertEqual(doc3.custom_fields.first().value, [4])
self.assertEqual(doc4.custom_fields.first().value, [3])
# If field exists on target doc but value is None
doc5 = Document.objects.create(
title="WOW5",
content="the content4",
checksum="5",
mime_type="application/pdf",
)
CustomFieldInstance.objects.create(document=doc5, field=custom_field_doclink)
resp = self.client.patch(
f"/api/documents/{doc1.id}/",
data={
"custom_fields": [
{
"field": custom_field_doclink.id,
"value": [doc5.id],
},
],
},
format="json",
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
self.assertEqual(doc5.custom_fields.first().value, [1])