mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-08-12 00:19:48 +00:00
Feature: bulk edit custom field values (#8428)
This commit is contained in:
@@ -244,7 +244,9 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
|
||||
"documents": [self.doc1.id, self.doc3.id],
|
||||
"method": "modify_custom_fields",
|
||||
"parameters": {
|
||||
"add_custom_fields": [self.cf1.id],
|
||||
"add_custom_fields": [
|
||||
self.cf1.id,
|
||||
], # old format accepts list of IDs
|
||||
"remove_custom_fields": [self.cf2.id],
|
||||
},
|
||||
},
|
||||
@@ -258,6 +260,30 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(kwargs["add_custom_fields"], [self.cf1.id])
|
||||
self.assertEqual(kwargs["remove_custom_fields"], [self.cf2.id])
|
||||
|
||||
@mock.patch("documents.serialisers.bulk_edit.modify_custom_fields")
|
||||
def test_api_modify_custom_fields_with_values(self, m):
|
||||
self.setup_mock(m, "modify_custom_fields")
|
||||
response = self.client.post(
|
||||
"/api/documents/bulk_edit/",
|
||||
json.dumps(
|
||||
{
|
||||
"documents": [self.doc1.id, self.doc3.id],
|
||||
"method": "modify_custom_fields",
|
||||
"parameters": {
|
||||
"add_custom_fields": {self.cf1.id: "foo"},
|
||||
"remove_custom_fields": [self.cf2.id],
|
||||
},
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||
m.assert_called_once()
|
||||
args, kwargs = m.call_args
|
||||
self.assertListEqual(args[0], [self.doc1.id, self.doc3.id])
|
||||
self.assertEqual(kwargs["add_custom_fields"], {str(self.cf1.id): "foo"})
|
||||
self.assertEqual(kwargs["remove_custom_fields"], [self.cf2.id])
|
||||
|
||||
@mock.patch("documents.serialisers.bulk_edit.modify_custom_fields")
|
||||
def test_api_modify_custom_fields_invalid_params(self, m):
|
||||
"""
|
||||
@@ -322,7 +348,23 @@ class TestBulkEditAPI(DirectoriesMixin, APITestCase):
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
m.assert_not_called()
|
||||
|
||||
# Not a list of integers
|
||||
# Invalid dict
|
||||
response = self.client.post(
|
||||
"/api/documents/bulk_edit/",
|
||||
json.dumps(
|
||||
{
|
||||
"documents": [self.doc1.id, self.doc3.id],
|
||||
"method": "modify_custom_fields",
|
||||
"parameters": {
|
||||
"add_custom_fields": {"foo": 99},
|
||||
"remove_custom_fields": [self.cf2.id],
|
||||
},
|
||||
},
|
||||
),
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
|
||||
m.assert_not_called()
|
||||
|
||||
# Missing remove_custom_fields
|
||||
response = self.client.post(
|
||||
|
@@ -189,6 +189,15 @@ class TestBulkEdit(DirectoriesMixin, TestCase):
|
||||
self.assertCountEqual(kwargs["document_ids"], [self.doc2.id, self.doc3.id])
|
||||
|
||||
def test_modify_custom_fields(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- 2 documents with custom fields
|
||||
- 3 custom fields
|
||||
WHEN:
|
||||
- Custom fields are modified using old format (list of ids)
|
||||
THEN:
|
||||
- Custom fields are modified for the documents
|
||||
"""
|
||||
cf = CustomField.objects.create(
|
||||
name="cf1",
|
||||
data_type=CustomField.FieldDataType.STRING,
|
||||
@@ -235,6 +244,78 @@ class TestBulkEdit(DirectoriesMixin, TestCase):
|
||||
args, kwargs = self.async_task.call_args
|
||||
self.assertCountEqual(kwargs["document_ids"], [self.doc1.id, self.doc2.id])
|
||||
|
||||
def test_modify_custom_fields_with_values(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- 2 documents with custom fields
|
||||
- 3 custom fields
|
||||
WHEN:
|
||||
- Custom fields are modified using new format (dict)
|
||||
THEN:
|
||||
- Custom fields are modified for the documents
|
||||
"""
|
||||
cf = CustomField.objects.create(
|
||||
name="cf",
|
||||
data_type=CustomField.FieldDataType.STRING,
|
||||
)
|
||||
cf1 = CustomField.objects.create(
|
||||
name="cf1",
|
||||
data_type=CustomField.FieldDataType.STRING,
|
||||
)
|
||||
cf2 = CustomField.objects.create(
|
||||
name="cf2",
|
||||
data_type=CustomField.FieldDataType.MONETARY,
|
||||
)
|
||||
cf3 = CustomField.objects.create(
|
||||
name="cf3",
|
||||
data_type=CustomField.FieldDataType.STRING,
|
||||
)
|
||||
CustomFieldInstance.objects.create(
|
||||
document=self.doc2,
|
||||
field=cf,
|
||||
)
|
||||
CustomFieldInstance.objects.create(
|
||||
document=self.doc2,
|
||||
field=cf1,
|
||||
)
|
||||
CustomFieldInstance.objects.create(
|
||||
document=self.doc2,
|
||||
field=cf3,
|
||||
)
|
||||
bulk_edit.modify_custom_fields(
|
||||
[self.doc1.id, self.doc2.id],
|
||||
add_custom_fields={cf2.id: None, cf3.id: "value"},
|
||||
remove_custom_fields=[cf.id],
|
||||
)
|
||||
|
||||
self.doc1.refresh_from_db()
|
||||
self.doc2.refresh_from_db()
|
||||
|
||||
self.assertEqual(
|
||||
self.doc1.custom_fields.count(),
|
||||
2,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.doc1.custom_fields.get(field=cf2).value,
|
||||
None,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.doc1.custom_fields.get(field=cf3).value,
|
||||
"value",
|
||||
)
|
||||
self.assertEqual(
|
||||
self.doc2.custom_fields.count(),
|
||||
3,
|
||||
)
|
||||
self.assertEqual(
|
||||
self.doc2.custom_fields.get(field=cf3).value,
|
||||
"value",
|
||||
)
|
||||
|
||||
self.async_task.assert_called_once()
|
||||
args, kwargs = self.async_task.call_args
|
||||
self.assertCountEqual(kwargs["document_ids"], [self.doc1.id, self.doc2.id])
|
||||
|
||||
def test_delete(self):
|
||||
self.assertEqual(Document.objects.count(), 5)
|
||||
bulk_edit.delete([self.doc1.id, self.doc2.id])
|
||||
|
Reference in New Issue
Block a user